'use strict'
function main(inp) {
  let info = inp.trim().split('\n')
  let input1 = info[0].split(' ');
  let input2 = info[1];
  let K = input1[0]*1;
  let N = input1[1]*1;
  let A = input2.split(' ').map(x => x*1);
  A[N] = K + A[0];
  let ans = 0;
  let max = 0;
  for(let i = 0; i < N - 1; i++){
    ans += A[i+1] - A[i];
    if(max < A[i+1] - A[i]){
    	max = A[i+1] - A[i];
    }
  }
  console.log(ans);
  
}

main(require('fs').readFileSync('/dev/stdin', 'utf8'));
