import java.util.Scanner;


	public class Main {
		
		public static void main(String[] args) {

			Scanner sc = new Scanner(System.in);
			
			long k = sc.nextLong();
			long n = sc.nextLong();
			long[] a = new long[(int)n];
			
			for(int i=0;i<n;i++) {
				a[i] = sc.nextLong();
			}
			long dis = 0;
			for(int i=1;i<n;i++) {
				dis = Math.max(dis, a[i]-a[i-1]);
			}
			
			dis = Math.max(dis, Math.abs(k-a[(int)n-1]+a[0]));
			
			System.out.println(k-dis);
			
	    }
		
	}
	 import java.util.Scanner; Scanner java.util public class Main {
		
		public static void main(String[] args) {

			Scanner sc = new Scanner(System.in);
			
			long k = sc.nextLong();
			long n = sc.nextLong();
			long[] a = new long[(int)n];
			
			for(int i=0;i<n;i++) {
				a[i] = sc.nextLong();
			}
			long dis = 0;
			for(int i=1;i<n;i++) {
				dis = Math.max(dis, a[i]-a[i-1]);
			}
			
			dis = Math.max(dis, Math.abs(k-a[(int)n-1]+a[0]));
			
			System.out.println(k-dis);
			
	    }
		
	} Main public static void main(String[] args) {

			Scanner sc = new Scanner(System.in);
			
			long k = sc.nextLong();
			long n = sc.nextLong();
			long[] a = new long[(int)n];
			
			for(int i=0;i<n;i++) {
				a[i] = sc.nextLong();
			}
			long dis = 0;
			for(int i=1;i<n;i++) {
				dis = Math.max(dis, a[i]-a[i-1]);
			}
			
			dis = Math.max(dis, Math.abs(k-a[(int)n-1]+a[0]));
			
			System.out.println(k-dis);
			
	    } main {

			Scanner sc = new Scanner(System.in);
			
			long k = sc.nextLong();
			long n = sc.nextLong();
			long[] a = new long[(int)n];
			
			for(int i=0;i<n;i++) {
				a[i] = sc.nextLong();
			}
			long dis = 0;
			for(int i=1;i<n;i++) {
				dis = Math.max(dis, a[i]-a[i-1]);
			}
			
			dis = Math.max(dis, Math.abs(k-a[(int)n-1]+a[0]));
			
			System.out.println(k-dis);
			
	    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long k = sc.nextLong(); k sc.nextLong() sc.nextLong sc long n = sc.nextLong(); n sc.nextLong() sc.nextLong sc long[] a = new long[(int)n]; a new long[(int)n] (int)n for(int i=0;i<n;i++) {
				a[i] = sc.nextLong();
			} int i=0; int i=0; i 0 i<n i n i++ i++ i {
				a[i] = sc.nextLong();
			} {
				a[i] = sc.nextLong();
			} a[i] = sc.nextLong() a[i] a i sc.nextLong() sc.nextLong sc long dis = 0; dis 0 for(int i=1;i<n;i++) {
				dis = Math.max(dis, a[i]-a[i-1]);
			} int i=1; int i=1; i 1 i<n i n i++ i++ i {
				dis = Math.max(dis, a[i]-a[i-1]);
			} {
				dis = Math.max(dis, a[i]-a[i-1]);
			} dis = Math.max(dis, a[i]-a[i-1]) dis Math.max(dis, a[i]-a[i-1]) Math.max Math dis a[i]-a[i-1] a[i] a i a[i-1] a i-1 i 1 dis = Math.max(dis, Math.abs(k-a[(int)n-1]+a[0])) dis Math.max(dis, Math.abs(k-a[(int)n-1]+a[0])) Math.max Math dis Math.abs(k-a[(int)n-1]+a[0]) Math.abs Math k-a[(int)n-1]+a[0] k-a[(int)n-1] k a[(int)n-1] a (int)n-1 (int)n 1 a[0] a 0 System.out.println(k-dis) System.out.println System.out System System.out k-dis k dis String[] args args public class Main {
		
		public static void main(String[] args) {

			Scanner sc = new Scanner(System.in);
			
			long k = sc.nextLong();
			long n = sc.nextLong();
			long[] a = new long[(int)n];
			
			for(int i=0;i<n;i++) {
				a[i] = sc.nextLong();
			}
			long dis = 0;
			for(int i=1;i<n;i++) {
				dis = Math.max(dis, a[i]-a[i-1]);
			}
			
			dis = Math.max(dis, Math.abs(k-a[(int)n-1]+a[0]));
			
			System.out.println(k-dis);
			
	    }
		
	} public class Main {
		
		public static void main(String[] args) {

			Scanner sc = new Scanner(System.in);
			
			long k = sc.nextLong();
			long n = sc.nextLong();
			long[] a = new long[(int)n];
			
			for(int i=0;i<n;i++) {
				a[i] = sc.nextLong();
			}
			long dis = 0;
			for(int i=1;i<n;i++) {
				dis = Math.max(dis, a[i]-a[i-1]);
			}
			
			dis = Math.max(dis, Math.abs(k-a[(int)n-1]+a[0]));
			
			System.out.println(k-dis);
			
	    }
		
	} Main
import java.util.Scanner;


	public class Main {
		
		public static void main(String[] args) {

			Scanner sc = new Scanner(System.in);
			
			long k = sc.nextLong();
			long n = sc.nextLong();
			long[] a = new long[(int)n];
			
			for(int i=0;i<n;i++) {
				a[i] = sc.nextLong();
			}
			long dis = 0;
			for(int i=1;i<n;i++) {
				dis = Math.max(dis, a[i]-a[i-1]);
			}
			
			dis = Math.max(dis, Math.abs(k-a[(int)n-1]+a[0]));
			
			System.out.println(k-dis);
			
	    }
		
	}
	
