const input = require("fs").readFileSync("/dev/stdin", "utf8")

;(input => {

    const lines = input.split('\n')
    const [K, N] = lines[0].split(' ').map(Number)
    const A = lines[1].split(' ').map(Number)

    const diff = []
    diff[0] = A[0] + K - A[N - 1]
    for (let i = 1; i < N; i++) {
        diff[i] = A[i] - A[i - 1]
    }
    console.log(K - Math.max(...diff))

})(input)