fun main(A: Array<String>) {
    abc000X()
}

fun abc000X() {
    val (k, n) = readLine()!!.split(" ").map { it.toInt() }
    val a = readLine()!!.split(" ").map { it.toInt() }

    a.indices.map{
        val d=Math.abs(a[it]-a[(it+1)%n])
        Math.max(d,k-d)
    }.min().let(::println)
}
