var inp = require('fs').readFileSync('/dev/stdin', 'utf8');
var a = inp.split('\n');
var k = a[0].split(' ').map(v => parseInt(v))[0];
var x = a[1].split(' ').map(v => parseInt(v));

var max = 0;
for (var i = 0; i < x.length; i++) {
  var d = 0;
  if (i === x.length - 1) {
    d = k - x[i] + x[0];
  } else {
    d = x[i + 1] - x[i];
  }
  max = Math.max(max, d);
}
console.log(k - max);