import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] A = new int[N];
		for(int i = 0; i < N; i++) {
			A[i] = sc.nextInt();
		}
		int longDistance = 0;
		for(int i = 0; i < N; i++) {
			int tmp = 0;
			if(i == N-1) {
				tmp = Math.abs(A[i]-(A[0]+K));
			} else {
				tmp = Math.abs(A[i]-A[i+1]);
			}

			if(tmp > longDistance) {
				longDistance = tmp;
			}
		}
		System.out.println(K-longDistance);
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] A = new int[N];
		for(int i = 0; i < N; i++) {
			A[i] = sc.nextInt();
		}
		int longDistance = 0;
		for(int i = 0; i < N; i++) {
			int tmp = 0;
			if(i == N-1) {
				tmp = Math.abs(A[i]-(A[0]+K));
			} else {
				tmp = Math.abs(A[i]-A[i+1]);
			}

			if(tmp > longDistance) {
				longDistance = tmp;
			}
		}
		System.out.println(K-longDistance);
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] A = new int[N];
		for(int i = 0; i < N; i++) {
			A[i] = sc.nextInt();
		}
		int longDistance = 0;
		for(int i = 0; i < N; i++) {
			int tmp = 0;
			if(i == N-1) {
				tmp = Math.abs(A[i]-(A[0]+K));
			} else {
				tmp = Math.abs(A[i]-A[i+1]);
			}

			if(tmp > longDistance) {
				longDistance = tmp;
			}
		}
		System.out.println(K-longDistance);
	} main {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] A = new int[N];
		for(int i = 0; i < N; i++) {
			A[i] = sc.nextInt();
		}
		int longDistance = 0;
		for(int i = 0; i < N; i++) {
			int tmp = 0;
			if(i == N-1) {
				tmp = Math.abs(A[i]-(A[0]+K));
			} else {
				tmp = Math.abs(A[i]-A[i+1]);
			}

			if(tmp > longDistance) {
				longDistance = tmp;
			}
		}
		System.out.println(K-longDistance);
	} 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[] A = new int[N]; A new int[N] N for(int i = 0; i < N; i++) {
			A[i] = sc.nextInt();
		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
			A[i] = sc.nextInt();
		} {
			A[i] = sc.nextInt();
		} A[i] = sc.nextInt() A[i] A i sc.nextInt() sc.nextInt sc int longDistance = 0; longDistance 0 for(int i = 0; i < N; i++) {
			int tmp = 0;
			if(i == N-1) {
				tmp = Math.abs(A[i]-(A[0]+K));
			} else {
				tmp = Math.abs(A[i]-A[i+1]);
			}

			if(tmp > longDistance) {
				longDistance = tmp;
			}
		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
			int tmp = 0;
			if(i == N-1) {
				tmp = Math.abs(A[i]-(A[0]+K));
			} else {
				tmp = Math.abs(A[i]-A[i+1]);
			}

			if(tmp > longDistance) {
				longDistance = tmp;
			}
		} {
			int tmp = 0;
			if(i == N-1) {
				tmp = Math.abs(A[i]-(A[0]+K));
			} else {
				tmp = Math.abs(A[i]-A[i+1]);
			}

			if(tmp > longDistance) {
				longDistance = tmp;
			}
		} int tmp = 0; tmp 0 if(i == N-1) {
				tmp = Math.abs(A[i]-(A[0]+K));
			} else {
				tmp = Math.abs(A[i]-A[i+1]);
			} i == N-1 i N-1 N 1 {
				tmp = Math.abs(A[i]-(A[0]+K));
			} tmp = Math.abs(A[i]-(A[0]+K)) tmp Math.abs(A[i]-(A[0]+K)) Math.abs Math A[i]-(A[0]+K) A[i] A i (A[0]+K) A[0] A 0 K {
				tmp = Math.abs(A[i]-A[i+1]);
			} tmp = Math.abs(A[i]-A[i+1]) tmp Math.abs(A[i]-A[i+1]) Math.abs Math A[i]-A[i+1] A[i] A i A[i+1] A i+1 i 1 if(tmp > longDistance) {
				longDistance = tmp;
			} tmp > longDistance tmp longDistance {
				longDistance = tmp;
			} longDistance = tmp longDistance tmp System.out.println(K-longDistance) System.out.println System.out System System.out K-longDistance K longDistance String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] A = new int[N];
		for(int i = 0; i < N; i++) {
			A[i] = sc.nextInt();
		}
		int longDistance = 0;
		for(int i = 0; i < N; i++) {
			int tmp = 0;
			if(i == N-1) {
				tmp = Math.abs(A[i]-(A[0]+K));
			} else {
				tmp = Math.abs(A[i]-A[i+1]);
			}

			if(tmp > longDistance) {
				longDistance = tmp;
			}
		}
		System.out.println(K-longDistance);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] A = new int[N];
		for(int i = 0; i < N; i++) {
			A[i] = sc.nextInt();
		}
		int longDistance = 0;
		for(int i = 0; i < N; i++) {
			int tmp = 0;
			if(i == N-1) {
				tmp = Math.abs(A[i]-(A[0]+K));
			} else {
				tmp = Math.abs(A[i]-A[i+1]);
			}

			if(tmp > longDistance) {
				longDistance = tmp;
			}
		}
		System.out.println(K-longDistance);
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] A = new int[N];
		for(int i = 0; i < N; i++) {
			A[i] = sc.nextInt();
		}
		int longDistance = 0;
		for(int i = 0; i < N; i++) {
			int tmp = 0;
			if(i == N-1) {
				tmp = Math.abs(A[i]-(A[0]+K));
			} else {
				tmp = Math.abs(A[i]-A[i+1]);
			}

			if(tmp > longDistance) {
				longDistance = tmp;
			}
		}
		System.out.println(K-longDistance);
	}
}