function getLines(input){
	return input.split("\n").map((s)=>s.trim())
}
function parseLine(line){
	return line.split(" ").map((s)=>s.trim());
}
function parseLineToInt(line){
	return line.split(" ").map((s)=>parseInt(s.trim()));
}
// parameter "input" gets all data
function Main(input) {
	// the first line is assigned to input[0], the second line is assigned to input[1] similarly.	
	input = getLines(input).map(parseLineToInt);
	var K = input[0][0];
	var N = input[0][1];
	var a = input[1];
	var ans = -1;
	for (var i = 0; i < N; i++) {
		if(ans==-1)
			ans = a[N-1]-a[0];
		else
			ans = Math.min(ans,K-a[i]+a[i-1]);
	}
	console.log(ans);
}
// Don't edit this line!
//Main(require("fs").readFileSync(__dirname+'/in.txt', "utf8"));
Main(require('fs').readFileSync('/dev/stdin', 'utf8'));
