'use strict';

const lines = [];
require('readline').createInterface({ input: process.stdin })
.on('line', function(line) { lines.push(line) })
.on('close', function() {
  function numeric(v) { return +v };
  const k = lines.shift().split(' ').map(numeric)[0];
  const a = lines.shift().split(' ').map(numeric);
  
  var max = k + a[0] - a[a.length - 1];
  for (var i = 0; i < a.length - 1; i++) {
    max = Math.max(max, a[i + 1] - a[i]);
  }
  
  console.log(k - max);
});