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