fun main(args: Array<String>) {
    val (k, n) = readLine()!!.split(" ").map { it.toInt() }
    val list = readLine()!!.split(" ").map { it.toInt() }
    val diffList = (0 until (n - 1)).map { list[it + 1] - list[it] }.plus((list[0] + k) - list[n - 1])
    val maxLength = diffList.max()!!
    val result = k - maxLength
    println(result)
}