fun main(arg: Array<String>) {
    val (K, N) = listOfInt()
    val A = listOfInt().toMutableList()
    A.add(A[0] + K) //先頭要素の2周目
    var max = 0
    for (n in 0 until N) max = Math.max(max, A[n + 1] - A[n])
    val ans = K - max
    println(ans)
}
