function Main(input) {
    input = input.split("\n");
  	var K = input[0].split(" ")[0];
  	var N = input[0].split(" ")[1];
  	var list = input[1].split(" ").map(v=>v-0);
  	var max = 0;
  	list.forEach((v,i)=>{
  	    if(i>0) max = Math.max(list[i]-list[i-1]);
  	    else max = Math.max(list[N-1] + (K-list[i]));
  	});
  	
  	console.log(K-max);
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));