process.stdin.resume();
process.stdin.setEncoding('utf8');

var lines = []
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});

reader.on('line', (line) => {
  lines.push(line);
});

reader.on('close', () => {
 var kandn = lines[0].split(' ');
 var k = Number(kandn[0]);
 var n = Number(kandn[1]);
 var a = lines[1].split(' ');
 var sa = [];

 var a = a.map(function(str) { return Number(str)});
 if(a === 0){
   a = k;
 }
 for(i=1; i<=a.length; i++){
   if(i === a.length){
     if(a[0] === 0){
       a[0] = k;
     }
     sa.push(Math.abs(a[i-1] - a[0]));
     break;
   }
   sa.push(Math.abs(a[i-1] - a[i]));
 }

 var max = Math.max(...sa);
//  console.log(sa);
 var index = sa.indexOf(max);
//  console.log(index);
sa.splice(index+1,index+1);

// console.log(sa)

 var ans = sa.reduce(function(a, x){return a + x;});
 console.log(ans);

 
});