fun main(args: Array<String>) = println(run {
    val (k, n) = readLine()!!.split(" ").map { it.toInt() }
    val a = readLine()!!.split(" ").map { it.toInt() }

    val d = IntArray(n)
    d[0] = a[0] + k - a.last()
    for (i in 1..a.lastIndex) {
        d[i] = a[i] - a[i-1]
    }

    k - d.max()!!
})