import java.util.*;
import java.io.*;
import java.math.BigInteger; 

 public class Main{
// taking inputs
static BufferedReader s1;
static BufferedWriter out;
static String read() throws IOException{String line="";while(line.length()==0){line=s1.readLine();continue;}return line;}
static int int_v (String s1){return Integer.parseInt(s1);}
static long long_v(String s1){return Long.parseLong(s1);}
static int[] int_arr() throws IOException{String[] a=read().split(" ");int[] b=new int[a.length];for(int i=0;i<a.length;i++){b[i]=int_v(a[i]);}return b;}
static long[] long_arr() throws IOException{String[] a=read().split(" ");long[] b=new long[a.length];for(int i=0;i<a.length;i++){b[i]=long_v(a[i]);}return b;}
static void assign(){s1=new BufferedReader(new InputStreamReader(System.in));out=new BufferedWriter(new OutputStreamWriter(System.out));}
// ends

//......................................@uthor_Alx..............................................


//  (Binary lifting)LCS implementation <O(NlogN,logN)>...<preprocessing,query>
//static int l,time;
//static int[][] f; // f[n][l]..
//static int[] tin, tout;
//static List<Integer>[] adj;
/*	return tin[u]<=tin[v]&&tout[u]>=tout[v]; 
}
static void dfs(int u, int p){
	tin[u]=time++;
	f[u][0]=p;
	for(int i=1;i<=l;i++){
		f[u][i]= f[f[u][i-1]][i-1];
	}

	for(int v: adj[u]){
		if(v!=p) dfs(v,u);
	}
	tout[u]=time++;
}
static int lca(int u, int v){
	if(ok(u,v)){return u;}
	if(ok(v,u)){return v;}

	for(int i=l;i>=0;i--){
		if(!ok(f[u][i],v)){
			u=f[u][i];
		}
	}
	return f[u][0];
}*/

public static void main(String[] args) throws  IOException{
	        		  assign();
              int[] arr1=int_arr();
              int k=arr1[0],n=arr1[1];
              int[] arr=int_arr();
              int max=0;
              for(int i=1;i<n;i++){
              	max=Math.max(max,Math.abs(arr[i]-arr[i-1]));
              }
              int x=k-arr[n-1]+arr[0];
              max=Math.max(max,x);
              out.write(k-max+"");
              out.flush();
	        		 
	        	}
	       
	          
	        }

	    

	
  


 import java.util.*; util.* java import java.io.*; io.* java import java.math.BigInteger; BigInteger java.math public class Main{
// taking inputs
static BufferedReader s1;
static BufferedWriter out;
static String read() throws IOException{String line="";while(line.length()==0){line=s1.readLine();continue;}return line;}
static int int_v (String s1){return Integer.parseInt(s1);}
static long long_v(String s1){return Long.parseLong(s1);}
static int[] int_arr() throws IOException{String[] a=read().split(" ");int[] b=new int[a.length];for(int i=0;i<a.length;i++){b[i]=int_v(a[i]);}return b;}
static long[] long_arr() throws IOException{String[] a=read().split(" ");long[] b=new long[a.length];for(int i=0;i<a.length;i++){b[i]=long_v(a[i]);}return b;}
static void assign(){s1=new BufferedReader(new InputStreamReader(System.in));out=new BufferedWriter(new OutputStreamWriter(System.out));}
// ends

//......................................@uthor_Alx..............................................


//  (Binary lifting)LCS implementation <O(NlogN,logN)>...<preprocessing,query>
//static int l,time;
//static int[][] f; // f[n][l]..
//static int[] tin, tout;
//static List<Integer>[] adj;
/*	return tin[u]<=tin[v]&&tout[u]>=tout[v]; 
}
static void dfs(int u, int p){
	tin[u]=time++;
	f[u][0]=p;
	for(int i=1;i<=l;i++){
		f[u][i]= f[f[u][i-1]][i-1];
	}

	for(int v: adj[u]){
		if(v!=p) dfs(v,u);
	}
	tout[u]=time++;
}
static int lca(int u, int v){
	if(ok(u,v)){return u;}
	if(ok(v,u)){return v;}

	for(int i=l;i>=0;i--){
		if(!ok(f[u][i],v)){
			u=f[u][i];
		}
	}
	return f[u][0];
}*/

public static void main(String[] args) throws  IOException{
	        		  assign();
              int[] arr1=int_arr();
              int k=arr1[0],n=arr1[1];
              int[] arr=int_arr();
              int max=0;
              for(int i=1;i<n;i++){
              	max=Math.max(max,Math.abs(arr[i]-arr[i-1]));
              }
              int x=k-arr[n-1]+arr[0];
              max=Math.max(max,x);
              out.write(k-max+"");
              out.flush();
	        		 
	        	}
	       
	          
	        } Main // taking inputs
static BufferedReader s1; s1 static BufferedWriter out; out static String read() throws IOException{String line="";while(line.length()==0){line=s1.readLine();continue;}return line;} read {String line="";while(line.length()==0){line=s1.readLine();continue;}return line;} String line=""; line "" while(line.length()==0){line=s1.readLine();continue;} line.length()==0 line.length() line.length line 0 {line=s1.readLine();continue;} line=s1.readLine() line s1.readLine() s1.readLine s1 continue; return line; line static int int_v (String s1){return Integer.parseInt(s1);} int_v {return Integer.parseInt(s1);} return Integer.parseInt(s1); Integer.parseInt(s1) Integer.parseInt Integer s1 String s1 s1 static long long_v(String s1){return Long.parseLong(s1);} long_v {return Long.parseLong(s1);} return Long.parseLong(s1); Long.parseLong(s1) Long.parseLong Long s1 String s1 s1 static int[] int_arr() throws IOException{String[] a=read().split(" ");int[] b=new int[a.length];for(int i=0;i<a.length;i++){b[i]=int_v(a[i]);}return b;} int_arr {String[] a=read().split(" ");int[] b=new int[a.length];for(int i=0;i<a.length;i++){b[i]=int_v(a[i]);}return b;} String[] a=read().split(" "); a read().split(" ") read().split read() read " " int[] b=new int[a.length]; b new int[a.length] a.length a a.length for(int i=0;i<a.length;i++){b[i]=int_v(a[i]);} int i=0; int i=0; i 0 i<a.length i a.length a a.length i++ i++ i {b[i]=int_v(a[i]);} {b[i]=int_v(a[i]);} b[i]=int_v(a[i]) b[i] b i int_v(a[i]) int_v a[i] a i return b; b static long[] long_arr() throws IOException{String[] a=read().split(" ");long[] b=new long[a.length];for(int i=0;i<a.length;i++){b[i]=long_v(a[i]);}return b;} long_arr {String[] a=read().split(" ");long[] b=new long[a.length];for(int i=0;i<a.length;i++){b[i]=long_v(a[i]);}return b;} String[] a=read().split(" "); a read().split(" ") read().split read() read " " long[] b=new long[a.length]; b new long[a.length] a.length a a.length for(int i=0;i<a.length;i++){b[i]=long_v(a[i]);} int i=0; int i=0; i 0 i<a.length i a.length a a.length i++ i++ i {b[i]=long_v(a[i]);} {b[i]=long_v(a[i]);} b[i]=long_v(a[i]) b[i] b i long_v(a[i]) long_v a[i] a i return b; b static void assign(){s1=new BufferedReader(new InputStreamReader(System.in));out=new BufferedWriter(new OutputStreamWriter(System.out));} assign {s1=new BufferedReader(new InputStreamReader(System.in));out=new BufferedWriter(new OutputStreamWriter(System.out));} s1=new BufferedReader(new InputStreamReader(System.in)) s1 new BufferedReader(new InputStreamReader(System.in)) out=new BufferedWriter(new OutputStreamWriter(System.out)) out new BufferedWriter(new OutputStreamWriter(System.out)) public static void main(String[] args) throws  IOException{
	        		  assign();
              int[] arr1=int_arr();
              int k=arr1[0],n=arr1[1];
              int[] arr=int_arr();
              int max=0;
              for(int i=1;i<n;i++){
              	max=Math.max(max,Math.abs(arr[i]-arr[i-1]));
              }
              int x=k-arr[n-1]+arr[0];
              max=Math.max(max,x);
              out.write(k-max+"");
              out.flush();
	        		 
	        	} main {
	        		  assign();
              int[] arr1=int_arr();
              int k=arr1[0],n=arr1[1];
              int[] arr=int_arr();
              int max=0;
              for(int i=1;i<n;i++){
              	max=Math.max(max,Math.abs(arr[i]-arr[i-1]));
              }
              int x=k-arr[n-1]+arr[0];
              max=Math.max(max,x);
              out.write(k-max+"");
              out.flush();
	        		 
	        	} assign() assign int[] arr1=int_arr(); arr1 int_arr() int_arr int k=arr1[0] k arr1[0] arr1 0 n=arr1[1]; n arr1[1] arr1 1 int[] arr=int_arr(); arr int_arr() int_arr int max=0; max 0 for(int i=1;i<n;i++){
              	max=Math.max(max,Math.abs(arr[i]-arr[i-1]));
              } int i=1; int i=1; i 1 i<n i n i++ i++ i {
              	max=Math.max(max,Math.abs(arr[i]-arr[i-1]));
              } {
              	max=Math.max(max,Math.abs(arr[i]-arr[i-1]));
              } max=Math.max(max,Math.abs(arr[i]-arr[i-1])) max Math.max(max,Math.abs(arr[i]-arr[i-1])) Math.max Math max Math.abs(arr[i]-arr[i-1]) Math.abs Math arr[i]-arr[i-1] arr[i] arr i arr[i-1] arr i-1 i 1 int x=k-arr[n-1]+arr[0]; x k-arr[n-1]+arr[0] k-arr[n-1] k arr[n-1] arr n-1 n 1 arr[0] arr 0 max=Math.max(max,x) max Math.max(max,x) Math.max Math max x out.write(k-max+"") out.write out k-max+"" k-max k max "" out.flush() out.flush out String[] args args public class Main{
// taking inputs
static BufferedReader s1;
static BufferedWriter out;
static String read() throws IOException{String line="";while(line.length()==0){line=s1.readLine();continue;}return line;}
static int int_v (String s1){return Integer.parseInt(s1);}
static long long_v(String s1){return Long.parseLong(s1);}
static int[] int_arr() throws IOException{String[] a=read().split(" ");int[] b=new int[a.length];for(int i=0;i<a.length;i++){b[i]=int_v(a[i]);}return b;}
static long[] long_arr() throws IOException{String[] a=read().split(" ");long[] b=new long[a.length];for(int i=0;i<a.length;i++){b[i]=long_v(a[i]);}return b;}
static void assign(){s1=new BufferedReader(new InputStreamReader(System.in));out=new BufferedWriter(new OutputStreamWriter(System.out));}
// ends

//......................................@uthor_Alx..............................................


//  (Binary lifting)LCS implementation <O(NlogN,logN)>...<preprocessing,query>
//static int l,time;
//static int[][] f; // f[n][l]..
//static int[] tin, tout;
//static List<Integer>[] adj;
/*	return tin[u]<=tin[v]&&tout[u]>=tout[v]; 
}
static void dfs(int u, int p){
	tin[u]=time++;
	f[u][0]=p;
	for(int i=1;i<=l;i++){
		f[u][i]= f[f[u][i-1]][i-1];
	}

	for(int v: adj[u]){
		if(v!=p) dfs(v,u);
	}
	tout[u]=time++;
}
static int lca(int u, int v){
	if(ok(u,v)){return u;}
	if(ok(v,u)){return v;}

	for(int i=l;i>=0;i--){
		if(!ok(f[u][i],v)){
			u=f[u][i];
		}
	}
	return f[u][0];
}*/

public static void main(String[] args) throws  IOException{
	        		  assign();
              int[] arr1=int_arr();
              int k=arr1[0],n=arr1[1];
              int[] arr=int_arr();
              int max=0;
              for(int i=1;i<n;i++){
              	max=Math.max(max,Math.abs(arr[i]-arr[i-1]));
              }
              int x=k-arr[n-1]+arr[0];
              max=Math.max(max,x);
              out.write(k-max+"");
              out.flush();
	        		 
	        	}
	       
	          
	        } public class Main{
// taking inputs
static BufferedReader s1;
static BufferedWriter out;
static String read() throws IOException{String line="";while(line.length()==0){line=s1.readLine();continue;}return line;}
static int int_v (String s1){return Integer.parseInt(s1);}
static long long_v(String s1){return Long.parseLong(s1);}
static int[] int_arr() throws IOException{String[] a=read().split(" ");int[] b=new int[a.length];for(int i=0;i<a.length;i++){b[i]=int_v(a[i]);}return b;}
static long[] long_arr() throws IOException{String[] a=read().split(" ");long[] b=new long[a.length];for(int i=0;i<a.length;i++){b[i]=long_v(a[i]);}return b;}
static void assign(){s1=new BufferedReader(new InputStreamReader(System.in));out=new BufferedWriter(new OutputStreamWriter(System.out));}
// ends

//......................................@uthor_Alx..............................................


//  (Binary lifting)LCS implementation <O(NlogN,logN)>...<preprocessing,query>
//static int l,time;
//static int[][] f; // f[n][l]..
//static int[] tin, tout;
//static List<Integer>[] adj;
/*	return tin[u]<=tin[v]&&tout[u]>=tout[v]; 
}
static void dfs(int u, int p){
	tin[u]=time++;
	f[u][0]=p;
	for(int i=1;i<=l;i++){
		f[u][i]= f[f[u][i-1]][i-1];
	}

	for(int v: adj[u]){
		if(v!=p) dfs(v,u);
	}
	tout[u]=time++;
}
static int lca(int u, int v){
	if(ok(u,v)){return u;}
	if(ok(v,u)){return v;}

	for(int i=l;i>=0;i--){
		if(!ok(f[u][i],v)){
			u=f[u][i];
		}
	}
	return f[u][0];
}*/

public static void main(String[] args) throws  IOException{
	        		  assign();
              int[] arr1=int_arr();
              int k=arr1[0],n=arr1[1];
              int[] arr=int_arr();
              int max=0;
              for(int i=1;i<n;i++){
              	max=Math.max(max,Math.abs(arr[i]-arr[i-1]));
              }
              int x=k-arr[n-1]+arr[0];
              max=Math.max(max,x);
              out.write(k-max+"");
              out.flush();
	        		 
	        	}
	       
	          
	        } Main
import java.util.*;
import java.io.*;
import java.math.BigInteger; 

 public class Main{
// taking inputs
static BufferedReader s1;
static BufferedWriter out;
static String read() throws IOException{String line="";while(line.length()==0){line=s1.readLine();continue;}return line;}
static int int_v (String s1){return Integer.parseInt(s1);}
static long long_v(String s1){return Long.parseLong(s1);}
static int[] int_arr() throws IOException{String[] a=read().split(" ");int[] b=new int[a.length];for(int i=0;i<a.length;i++){b[i]=int_v(a[i]);}return b;}
static long[] long_arr() throws IOException{String[] a=read().split(" ");long[] b=new long[a.length];for(int i=0;i<a.length;i++){b[i]=long_v(a[i]);}return b;}
static void assign(){s1=new BufferedReader(new InputStreamReader(System.in));out=new BufferedWriter(new OutputStreamWriter(System.out));}
// ends

//......................................@uthor_Alx..............................................


//  (Binary lifting)LCS implementation <O(NlogN,logN)>...<preprocessing,query>
//static int l,time;
//static int[][] f; // f[n][l]..
//static int[] tin, tout;
//static List<Integer>[] adj;
/*	return tin[u]<=tin[v]&&tout[u]>=tout[v]; 
}
static void dfs(int u, int p){
	tin[u]=time++;
	f[u][0]=p;
	for(int i=1;i<=l;i++){
		f[u][i]= f[f[u][i-1]][i-1];
	}

	for(int v: adj[u]){
		if(v!=p) dfs(v,u);
	}
	tout[u]=time++;
}
static int lca(int u, int v){
	if(ok(u,v)){return u;}
	if(ok(v,u)){return v;}

	for(int i=l;i>=0;i--){
		if(!ok(f[u][i],v)){
			u=f[u][i];
		}
	}
	return f[u][0];
}*/

public static void main(String[] args) throws  IOException{
	        		  assign();
              int[] arr1=int_arr();
              int k=arr1[0],n=arr1[1];
              int[] arr=int_arr();
              int max=0;
              for(int i=1;i<n;i++){
              	max=Math.max(max,Math.abs(arr[i]-arr[i-1]));
              }
              int x=k-arr[n-1]+arr[0];
              max=Math.max(max,x);
              out.write(k-max+"");
              out.flush();
	        		 
	        	}
	       
	          
	        }

	    

	
  



