fun main(arr:Array<String>) {
    val (k,n) = readLine()!!.split(" ").map { it.toInt() }
    val houses = readLine()!!.split(" ").map { it.toInt() }
    val space = mutableListOf(houses.first() + k - houses.last())
    space.addAll((1 until n).map { houses[it] - houses[it-1] })
    val ans = k - space.max()!!
    println(ans)
}
