import java.util.Scanner;

public class Main {
	private static Scanner sc = new Scanner(System.in);
	static void p(String ans) {System.out.println(ans);};
	static void p(int ans) {System.out.println(ans);};
	static void p(long ans) {System.out.println(ans);};
	public static void main(String[] args) {
		int K = sc.nextInt();
		int N = sc.nextInt();
		int max=0;int f1=0;int past1=0;
		
		
		
		for(int i =0;i!=N;i++) {
			int dis = sc.nextInt();
			if(i==0) f1=dis;
			if(dis-past1>max) max=dis-past1;
			past1=dis;
			if(i==N-1) {dis=K-dis+f1;if(dis>max)max=dis;}
		}
		p(K-max);
	}

} import java.util.Scanner; Scanner java.util public class Main {
	private static Scanner sc = new Scanner(System.in);
	static void p(String ans) {System.out.println(ans);};
	static void p(int ans) {System.out.println(ans);};
	static void p(long ans) {System.out.println(ans);};
	public static void main(String[] args) {
		int K = sc.nextInt();
		int N = sc.nextInt();
		int max=0;int f1=0;int past1=0;
		
		
		
		for(int i =0;i!=N;i++) {
			int dis = sc.nextInt();
			if(i==0) f1=dis;
			if(dis-past1>max) max=dis-past1;
			past1=dis;
			if(i==N-1) {dis=K-dis+f1;if(dis>max)max=dis;}
		}
		p(K-max);
	}

} Main private static Scanner sc = new Scanner(System.in); sc new Scanner(System.in) static void p(String ans) {System.out.println(ans);} p {System.out.println(ans);} System.out.println(ans) System.out.println System.out System System.out ans String ans ans static void p(int ans) {System.out.println(ans);} p {System.out.println(ans);} System.out.println(ans) System.out.println System.out System System.out ans int ans ans static void p(long ans) {System.out.println(ans);} p {System.out.println(ans);} System.out.println(ans) System.out.println System.out System System.out ans long ans ans public static void main(String[] args) {
		int K = sc.nextInt();
		int N = sc.nextInt();
		int max=0;int f1=0;int past1=0;
		
		
		
		for(int i =0;i!=N;i++) {
			int dis = sc.nextInt();
			if(i==0) f1=dis;
			if(dis-past1>max) max=dis-past1;
			past1=dis;
			if(i==N-1) {dis=K-dis+f1;if(dis>max)max=dis;}
		}
		p(K-max);
	} main {
		int K = sc.nextInt();
		int N = sc.nextInt();
		int max=0;int f1=0;int past1=0;
		
		
		
		for(int i =0;i!=N;i++) {
			int dis = sc.nextInt();
			if(i==0) f1=dis;
			if(dis-past1>max) max=dis-past1;
			past1=dis;
			if(i==N-1) {dis=K-dis+f1;if(dis>max)max=dis;}
		}
		p(K-max);
	} int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int max=0; max 0 int f1=0; f1 0 int past1=0; past1 0 for(int i =0;i!=N;i++) {
			int dis = sc.nextInt();
			if(i==0) f1=dis;
			if(dis-past1>max) max=dis-past1;
			past1=dis;
			if(i==N-1) {dis=K-dis+f1;if(dis>max)max=dis;}
		} int i =0; int i =0; i 0 i!=N i N i++ i++ i {
			int dis = sc.nextInt();
			if(i==0) f1=dis;
			if(dis-past1>max) max=dis-past1;
			past1=dis;
			if(i==N-1) {dis=K-dis+f1;if(dis>max)max=dis;}
		} {
			int dis = sc.nextInt();
			if(i==0) f1=dis;
			if(dis-past1>max) max=dis-past1;
			past1=dis;
			if(i==N-1) {dis=K-dis+f1;if(dis>max)max=dis;}
		} int dis = sc.nextInt(); dis sc.nextInt() sc.nextInt sc if(i==0) f1=dis; i==0 i 0 f1=dis f1 dis if(dis-past1>max) max=dis-past1; dis-past1>max dis-past1 dis past1 max max=dis-past1 max dis-past1 dis past1 past1=dis past1 dis if(i==N-1) {dis=K-dis+f1;if(dis>max)max=dis;} i==N-1 i N-1 N 1 {dis=K-dis+f1;if(dis>max)max=dis;} dis=K-dis+f1 dis K-dis+f1 K-dis K dis f1 if(dis>max)max=dis; dis>max dis max max=dis max dis p(K-max) p K-max K max String[] args args public class Main {
	private static Scanner sc = new Scanner(System.in);
	static void p(String ans) {System.out.println(ans);};
	static void p(int ans) {System.out.println(ans);};
	static void p(long ans) {System.out.println(ans);};
	public static void main(String[] args) {
		int K = sc.nextInt();
		int N = sc.nextInt();
		int max=0;int f1=0;int past1=0;
		
		
		
		for(int i =0;i!=N;i++) {
			int dis = sc.nextInt();
			if(i==0) f1=dis;
			if(dis-past1>max) max=dis-past1;
			past1=dis;
			if(i==N-1) {dis=K-dis+f1;if(dis>max)max=dis;}
		}
		p(K-max);
	}

} public class Main {
	private static Scanner sc = new Scanner(System.in);
	static void p(String ans) {System.out.println(ans);};
	static void p(int ans) {System.out.println(ans);};
	static void p(long ans) {System.out.println(ans);};
	public static void main(String[] args) {
		int K = sc.nextInt();
		int N = sc.nextInt();
		int max=0;int f1=0;int past1=0;
		
		
		
		for(int i =0;i!=N;i++) {
			int dis = sc.nextInt();
			if(i==0) f1=dis;
			if(dis-past1>max) max=dis-past1;
			past1=dis;
			if(i==N-1) {dis=K-dis+f1;if(dis>max)max=dis;}
		}
		p(K-max);
	}

} Main
import java.util.Scanner;

public class Main {
	private static Scanner sc = new Scanner(System.in);
	static void p(String ans) {System.out.println(ans);};
	static void p(int ans) {System.out.println(ans);};
	static void p(long ans) {System.out.println(ans);};
	public static void main(String[] args) {
		int K = sc.nextInt();
		int N = sc.nextInt();
		int max=0;int f1=0;int past1=0;
		
		
		
		for(int i =0;i!=N;i++) {
			int dis = sc.nextInt();
			if(i==0) f1=dis;
			if(dis-past1>max) max=dis-past1;
			past1=dis;
			if(i==N-1) {dis=K-dis+f1;if(dis>max)max=dis;}
		}
		p(K-max);
	}

}
