fun main(args: Array<String>) {
    val (K, N) = readLine()!!.split(" ").map(String::toInt)
    val AN = readLine()!!.split(" ").map(String::toInt)

    var ans = K
    for (i in 0 until N) {
        val start = AN[i]
        val goal = AN[(N - 1 + i) % N].let {
            if (it < start) {
                it + K
            } else {
                it
            }
        }

        ans = Math.min(ans, goal - start)
    }
    println(ans)

}



