fun main(args: Array<String>){
    val(k,n) = readLine()!!.split(" ").map{it.toInt()}
    val distance = readLine()!!.split(" ").map{it.toInt()}

    var max = 0
    for(i in 0 until distance.size - 1){
        var between = distance.elementAt(i+1) - distance.elementAt(i)
        if(max < between){
            max = between
        }
    }
    val lastBetween = k + distance.first() - distance.last()
    if(max < lastBetween){
        max = lastBetween
    }
    println(k - max)
}