"use strict"
function main(arg) {
    var k = arg.trim().split("\n")[0].split(" ").map(Number)[0]
    var a = arg.trim().split("\n")[1].split(" ").map(Number).sort((a,b) => {
        return a-b
    })
    var sum = 0
    var max = 0
    for (var i = 0; i < a.length; i++) {
        var last_index = i - 1
        var x = 0
        var y = 0
        x = a[i]
        if (i === 0) {
            last_index = a.length - 1
            y = a[last_index]
            x = k + a[0]
        } else {
            y = a[last_index]
        }
        var diff = Math.abs(x - y)
        // console.log([x,y])
        // console.log(diff)
        max = Math.max(diff, max)
        // sum += 
        sum += diff
    }
    console.log(sum - max)
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));