fun main(args: Array<String>) {
    val line1 = readLine()!!.trim()
    val (k: Int, n: Int) = line1.split(" ").map {it.toInt()}
    val line2 = readLine()!!.trim()
    val homes: List<Int> = line2.split(" ").map {it.toInt()}
    var maxDistance: Int = homes[0] + k - homes[n - 1]

    var i = 0
    while (i < n - 1) {
        val distance = homes[i + 1] - homes[i]
        if (maxDistance < distance) {
            maxDistance = distance
        }

        i += 1
    }

    val result = k - maxDistance

    println(result)
}
