"use strict";

const main = arg => {
    const input = arg.trim().split("\n");
    const K = parseInt(input[0].split(" ")[0])
    const N = parseInt(input[0].split(" ")[1]) 
    const A = input[1].split(" ").map(n => parseInt(n));
    A.sort((a,b) => a - b)
    let min = Number.POSITIVE_INFINITY
    for(let i = 1; i<N-1; i++){
        let d1 = K - (A[i] - A[i-1])
        let d2 = K - (A[i+1] - A[i])
        min = Math.min(min, d1)
        min = Math.min(min, d2)
    }
    // i = 0
    min = Math.min(min, A[N-1]-A[0])
    console.log(min)
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));  