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

    var diffMax = 0

    for (i in 1..n-1) {
        val diff = aa[i] - aa[i-1]
        if (diff > diffMax) {
            diffMax = diff
        }
    }

    val diff = k - aa.last() + aa.first()
    if (diff > diffMax) {
        diffMax = diff
    }

    println(k-diffMax)
}