"use strict";

const fs = require("fs");

const lines = fs.readFileSync("/dev/stdin", "utf-8").split("\n");
lines.pop();

const k = Number(lines[0].split(" ")[0]);
const n = Number(lines[0].split(" ")[1]);

const aa = lines[1].split(" ").map(x => Number(x));

const ad = [];
let res = 0;
let max = -1;
aa.forEach((a, i) => {
  let dist;

  if (i < n - 1) {
    dist = aa[i + 1] - a;
    ad.push(dist);
  } else {
    dist = k - a + aa[0];
    ad.push(dist);
  }

  if (max < dist) max = dist;
  res += dist;
});

console.log(res - max);
