import java.util.Scanner;

public class Main {

	// test 3 C
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		int k = sc.nextInt();
		int n = sc.nextInt();
		
		int ary[] = new int[n];

		for(int i =0; i<n; i++) {
			ary[i] = sc.nextInt();
		}

		// 隣同士の距離を調査しつつ、最大の距離を求める。
		// 最大 > (K/2) の場合、それは K-最大 に adjust する
		int maxLen = 0;
		for(int i=0; i< (n-1); i++) {
			int j = i+1;
			int dist1;
			dist1 = ary[j]-ary[i];

//			if ( dist1 > (k/2) ) {
//				dist1 = k - dist1;
//			}
			if(maxLen<dist1)
				maxLen = dist1;
		}
		
		// end を調査
		int lastDist = k + ary[0] - ary[n-1];
		if (maxLen < lastDist)
			maxLen = lastDist;
		
		int tripLen = k - maxLen;
		
		log( tripLen );
		sc.close();
		
	}
	
	static void log(String s) {
		System.out.println(s);
	}

	static void log(int i) {
		System.out.println(i);
	}

	static void log(double d) {
		System.out.printf("%.12f\n",d);
	}

	
	
} import java.util.Scanner; Scanner java.util public class Main {

	// test 3 C
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		int k = sc.nextInt();
		int n = sc.nextInt();
		
		int ary[] = new int[n];

		for(int i =0; i<n; i++) {
			ary[i] = sc.nextInt();
		}

		// 隣同士の距離を調査しつつ、最大の距離を求める。
		// 最大 > (K/2) の場合、それは K-最大 に adjust する
		int maxLen = 0;
		for(int i=0; i< (n-1); i++) {
			int j = i+1;
			int dist1;
			dist1 = ary[j]-ary[i];

//			if ( dist1 > (k/2) ) {
//				dist1 = k - dist1;
//			}
			if(maxLen<dist1)
				maxLen = dist1;
		}
		
		// end を調査
		int lastDist = k + ary[0] - ary[n-1];
		if (maxLen < lastDist)
			maxLen = lastDist;
		
		int tripLen = k - maxLen;
		
		log( tripLen );
		sc.close();
		
	}
	
	static void log(String s) {
		System.out.println(s);
	}

	static void log(int i) {
		System.out.println(i);
	}

	static void log(double d) {
		System.out.printf("%.12f\n",d);
	}

	
	
} Main // test 3 C
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		int k = sc.nextInt();
		int n = sc.nextInt();
		
		int ary[] = new int[n];

		for(int i =0; i<n; i++) {
			ary[i] = sc.nextInt();
		}

		// 隣同士の距離を調査しつつ、最大の距離を求める。
		// 最大 > (K/2) の場合、それは K-最大 に adjust する
		int maxLen = 0;
		for(int i=0; i< (n-1); i++) {
			int j = i+1;
			int dist1;
			dist1 = ary[j]-ary[i];

//			if ( dist1 > (k/2) ) {
//				dist1 = k - dist1;
//			}
			if(maxLen<dist1)
				maxLen = dist1;
		}
		
		// end を調査
		int lastDist = k + ary[0] - ary[n-1];
		if (maxLen < lastDist)
			maxLen = lastDist;
		
		int tripLen = k - maxLen;
		
		log( tripLen );
		sc.close();
		
	} main {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		int k = sc.nextInt();
		int n = sc.nextInt();
		
		int ary[] = new int[n];

		for(int i =0; i<n; i++) {
			ary[i] = sc.nextInt();
		}

		// 隣同士の距離を調査しつつ、最大の距離を求める。
		// 最大 > (K/2) の場合、それは K-最大 に adjust する
		int maxLen = 0;
		for(int i=0; i< (n-1); i++) {
			int j = i+1;
			int dist1;
			dist1 = ary[j]-ary[i];

//			if ( dist1 > (k/2) ) {
//				dist1 = k - dist1;
//			}
			if(maxLen<dist1)
				maxLen = dist1;
		}
		
		// end を調査
		int lastDist = k + ary[0] - ary[n-1];
		if (maxLen < lastDist)
			maxLen = lastDist;
		
		int tripLen = k - maxLen;
		
		log( tripLen );
		sc.close();
		
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int ary[] = new int[n]; ary new int[n] n for(int i =0; i<n; i++) {
			ary[i] = sc.nextInt();
		} int i =0; int i =0; i 0 i<n i n i++ i++ i {
			ary[i] = sc.nextInt();
		} {
			ary[i] = sc.nextInt();
		} ary[i] = sc.nextInt() ary[i] ary i sc.nextInt() sc.nextInt sc int maxLen = 0; maxLen 0 for(int i=0; i< (n-1); i++) {
			int j = i+1;
			int dist1;
			dist1 = ary[j]-ary[i];

//			if ( dist1 > (k/2) ) {
//				dist1 = k - dist1;
//			}
			if(maxLen<dist1)
				maxLen = dist1;
		} int i=0; int i=0; i 0 i< (n-1) i (n-1) n 1 i++ i++ i {
			int j = i+1;
			int dist1;
			dist1 = ary[j]-ary[i];

//			if ( dist1 > (k/2) ) {
//				dist1 = k - dist1;
//			}
			if(maxLen<dist1)
				maxLen = dist1;
		} {
			int j = i+1;
			int dist1;
			dist1 = ary[j]-ary[i];

//			if ( dist1 > (k/2) ) {
//				dist1 = k - dist1;
//			}
			if(maxLen<dist1)
				maxLen = dist1;
		} int j = i+1; j i+1 i 1 int dist1; dist1 dist1 = ary[j]-ary[i] dist1 ary[j]-ary[i] ary[j] ary j ary[i] ary i if(maxLen<dist1)
				maxLen = dist1; maxLen<dist1 maxLen dist1 maxLen = dist1 maxLen dist1 int lastDist = k + ary[0] - ary[n-1]; lastDist k + ary[0] - ary[n-1] k + ary[0] k ary[0] ary 0 ary[n-1] ary n-1 n 1 if (maxLen < lastDist)
			maxLen = lastDist; maxLen < lastDist maxLen lastDist maxLen = lastDist maxLen lastDist int tripLen = k - maxLen; tripLen k - maxLen k maxLen log( tripLen ) log tripLen sc.close() sc.close sc String[] args args static void log(String s) {
		System.out.println(s);
	} log {
		System.out.println(s);
	} System.out.println(s) System.out.println System.out System System.out s String s s static void log(int i) {
		System.out.println(i);
	} log {
		System.out.println(i);
	} System.out.println(i) System.out.println System.out System System.out i int i i static void log(double d) {
		System.out.printf("%.12f\n",d);
	} log {
		System.out.printf("%.12f\n",d);
	} System.out.printf("%.12f\n",d) System.out.printf System.out System System.out "%.12f\n" d double d d public class Main {

	// test 3 C
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		int k = sc.nextInt();
		int n = sc.nextInt();
		
		int ary[] = new int[n];

		for(int i =0; i<n; i++) {
			ary[i] = sc.nextInt();
		}

		// 隣同士の距離を調査しつつ、最大の距離を求める。
		// 最大 > (K/2) の場合、それは K-最大 に adjust する
		int maxLen = 0;
		for(int i=0; i< (n-1); i++) {
			int j = i+1;
			int dist1;
			dist1 = ary[j]-ary[i];

//			if ( dist1 > (k/2) ) {
//				dist1 = k - dist1;
//			}
			if(maxLen<dist1)
				maxLen = dist1;
		}
		
		// end を調査
		int lastDist = k + ary[0] - ary[n-1];
		if (maxLen < lastDist)
			maxLen = lastDist;
		
		int tripLen = k - maxLen;
		
		log( tripLen );
		sc.close();
		
	}
	
	static void log(String s) {
		System.out.println(s);
	}

	static void log(int i) {
		System.out.println(i);
	}

	static void log(double d) {
		System.out.printf("%.12f\n",d);
	}

	
	
} public class Main {

	// test 3 C
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		int k = sc.nextInt();
		int n = sc.nextInt();
		
		int ary[] = new int[n];

		for(int i =0; i<n; i++) {
			ary[i] = sc.nextInt();
		}

		// 隣同士の距離を調査しつつ、最大の距離を求める。
		// 最大 > (K/2) の場合、それは K-最大 に adjust する
		int maxLen = 0;
		for(int i=0; i< (n-1); i++) {
			int j = i+1;
			int dist1;
			dist1 = ary[j]-ary[i];

//			if ( dist1 > (k/2) ) {
//				dist1 = k - dist1;
//			}
			if(maxLen<dist1)
				maxLen = dist1;
		}
		
		// end を調査
		int lastDist = k + ary[0] - ary[n-1];
		if (maxLen < lastDist)
			maxLen = lastDist;
		
		int tripLen = k - maxLen;
		
		log( tripLen );
		sc.close();
		
	}
	
	static void log(String s) {
		System.out.println(s);
	}

	static void log(int i) {
		System.out.println(i);
	}

	static void log(double d) {
		System.out.printf("%.12f\n",d);
	}

	
	
} Main
import java.util.Scanner;

public class Main {

	// test 3 C
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		int k = sc.nextInt();
		int n = sc.nextInt();
		
		int ary[] = new int[n];

		for(int i =0; i<n; i++) {
			ary[i] = sc.nextInt();
		}

		// 隣同士の距離を調査しつつ、最大の距離を求める。
		// 最大 > (K/2) の場合、それは K-最大 に adjust する
		int maxLen = 0;
		for(int i=0; i< (n-1); i++) {
			int j = i+1;
			int dist1;
			dist1 = ary[j]-ary[i];

//			if ( dist1 > (k/2) ) {
//				dist1 = k - dist1;
//			}
			if(maxLen<dist1)
				maxLen = dist1;
		}
		
		// end を調査
		int lastDist = k + ary[0] - ary[n-1];
		if (maxLen < lastDist)
			maxLen = lastDist;
		
		int tripLen = k - maxLen;
		
		log( tripLen );
		sc.close();
		
	}
	
	static void log(String s) {
		System.out.println(s);
	}

	static void log(int i) {
		System.out.println(i);
	}

	static void log(double d) {
		System.out.printf("%.12f\n",d);
	}

	
	
}
