//package atcoder;
 
import 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 min = A[N-1]-A[0];
		for(int j=1;j<N;j++) {
			min = Math.min(min, K-A[j]+A[j-1]);
		}
		System.out.println(min);
	}
} import java.util.*; util.* java 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 min = A[N-1]-A[0];
		for(int j=1;j<N;j++) {
			min = Math.min(min, K-A[j]+A[j-1]);
		}
		System.out.println(min);
	}
} 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 min = A[N-1]-A[0];
		for(int j=1;j<N;j++) {
			min = Math.min(min, K-A[j]+A[j-1]);
		}
		System.out.println(min);
	} 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 min = A[N-1]-A[0];
		for(int j=1;j<N;j++) {
			min = Math.min(min, K-A[j]+A[j-1]);
		}
		System.out.println(min);
	} 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 min = A[N-1]-A[0]; min A[N-1]-A[0] A[N-1] A N-1 N 1 A[0] A 0 for(int j=1;j<N;j++) {
			min = Math.min(min, K-A[j]+A[j-1]);
		} int j=1; int j=1; j 1 j<N j N j++ j++ j {
			min = Math.min(min, K-A[j]+A[j-1]);
		} {
			min = Math.min(min, K-A[j]+A[j-1]);
		} min = Math.min(min, K-A[j]+A[j-1]) min Math.min(min, K-A[j]+A[j-1]) Math.min Math min K-A[j]+A[j-1] K-A[j] K A[j] A j A[j-1] A j-1 j 1 System.out.println(min) System.out.println System.out System System.out min 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 min = A[N-1]-A[0];
		for(int j=1;j<N;j++) {
			min = Math.min(min, K-A[j]+A[j-1]);
		}
		System.out.println(min);
	}
} 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 min = A[N-1]-A[0];
		for(int j=1;j<N;j++) {
			min = Math.min(min, K-A[j]+A[j-1]);
		}
		System.out.println(min);
	}
} Main
//package atcoder;
 
import 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 min = A[N-1]-A[0];
		for(int j=1;j<N;j++) {
			min = Math.min(min, K-A[j]+A[j-1]);
		}
		System.out.println(min);
	}
}