fun main(args: Array<String>) {
    var (K,N) = readLine()!!.split(" ").map { it.toInt() }
    var a = readLine()!!.split(" ").map { it.toInt() }

    val d = Array(N){i: Int -> a[(i+1)%N] - a[i]}
    d[N-1] += K
    println(K-d.max()!!)
}