fun main(args: Array<String>) {
    var (k, n) = readLine()!!.split(" ").map{ it.toInt() }
    var a = readLine()!!.split(" ").map{ it.toInt() }
    var diff_max = k - a[n-1] + a[0]
    var old = a[0]
    for (ai in a.slice(1..n-1)) {
        diff_max = Math.max(diff_max, ai - old)
        old = ai
    }
    println(k-diff_max)
}
