const main = (inputs) => {
  var array1 = inputs[0].split(" ");
  var array2 = inputs[1].split(" ");
  var K = Number(array1[0]);
  var N = array1[1];
  var root = [];
  var root2 = [];

  array2.forEach(element => {
    if (Number(element) <= K / 2) {
      root.push(Number(element));
    } else {
      root.push(Number(element) - K);
    }
  });
  root.sort(
    function (a, b) {
      return (a < b ? -1 : 1);
    }
  );
  var sum1 = 0;
  for (var i = 0; i < root.length - 1; i++) {
    sum1 += root[i + 1] - root[i];
  }


  array2.forEach(element => {
    root2.push(Number(element));
  });
  root2.sort(
    function (a, b) {
      return (a < b ? -1 : 1);
    }
  );
  var sum2 = 0;
  for (var i = 0; i < root2.length - 1; i++) {
    sum2 += root2[i + 1] - root2[i] - 0;
  }


  console.log(sum1 >= sum2 ? sum2 : sum1);
}
main(require('fs').readFileSync('/dev/stdin', 'UTF-8').trim().split("\n"));