'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 halfK = Math.floor(K/2);
  let N = input1[1]*1;
  let A = input2.split(' ').map(x => x*1);
  A[N-1] = K - A[N-1];
  let ans = 0;
  let newA = A.map(x => {return x % halfK});
  for(let i = 0; i < N-2; i++){
  	ans += Math.abs(newA[i+1] - newA[i]);
  }
  ans += newA[N-1];
  console.log(ans);
  
}

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