import java.util.*;

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

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