import java.util.Scanner;
public class Main {

	@SuppressWarnings("resource")
	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];
		int max=0;
		int maxi=0;
		A[0]=sc.nextInt();
		for(int i=1;i<N;i++) {
			A[i]=sc.nextInt();
			if(max<A[i]-A[i-1]) {
				max=A[i]-A[i-1];
				maxi=i;
			}
		}
		if(K-(A[N-1]-A[0])>max) {
			maxi=0;
			max=K-(A[N-1]-A[0]);
		}
		System.out.println(K-max);
	}
} import java.util.Scanner; Scanner java.util public class Main {

	@SuppressWarnings("resource")
	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];
		int max=0;
		int maxi=0;
		A[0]=sc.nextInt();
		for(int i=1;i<N;i++) {
			A[i]=sc.nextInt();
			if(max<A[i]-A[i-1]) {
				max=A[i]-A[i-1];
				maxi=i;
			}
		}
		if(K-(A[N-1]-A[0])>max) {
			maxi=0;
			max=K-(A[N-1]-A[0]);
		}
		System.out.println(K-max);
	}
} Main @SuppressWarnings("resource")
	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];
		int max=0;
		int maxi=0;
		A[0]=sc.nextInt();
		for(int i=1;i<N;i++) {
			A[i]=sc.nextInt();
			if(max<A[i]-A[i-1]) {
				max=A[i]-A[i-1];
				maxi=i;
			}
		}
		if(K-(A[N-1]-A[0])>max) {
			maxi=0;
			max=K-(A[N-1]-A[0]);
		}
		System.out.println(K-max);
	} main {
		Scanner sc=new Scanner(System.in);
		int K=sc.nextInt();
		int N=(sc.nextInt());
		int A[]=new int[N];
		int max=0;
		int maxi=0;
		A[0]=sc.nextInt();
		for(int i=1;i<N;i++) {
			A[i]=sc.nextInt();
			if(max<A[i]-A[i-1]) {
				max=A[i]-A[i-1];
				maxi=i;
			}
		}
		if(K-(A[N-1]-A[0])>max) {
			maxi=0;
			max=K-(A[N-1]-A[0]);
		}
		System.out.println(K-max);
	} 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 int max=0; max 0 int maxi=0; maxi 0 A[0]=sc.nextInt() A[0] A 0 sc.nextInt() sc.nextInt sc for(int i=1;i<N;i++) {
			A[i]=sc.nextInt();
			if(max<A[i]-A[i-1]) {
				max=A[i]-A[i-1];
				maxi=i;
			}
		} int i=1; int i=1; i 1 i<N i N i++ i++ i {
			A[i]=sc.nextInt();
			if(max<A[i]-A[i-1]) {
				max=A[i]-A[i-1];
				maxi=i;
			}
		} {
			A[i]=sc.nextInt();
			if(max<A[i]-A[i-1]) {
				max=A[i]-A[i-1];
				maxi=i;
			}
		} A[i]=sc.nextInt() A[i] A i sc.nextInt() sc.nextInt sc if(max<A[i]-A[i-1]) {
				max=A[i]-A[i-1];
				maxi=i;
			} max<A[i]-A[i-1] max A[i]-A[i-1] A[i] A i A[i-1] A i-1 i 1 {
				max=A[i]-A[i-1];
				maxi=i;
			} max=A[i]-A[i-1] max A[i]-A[i-1] A[i] A i A[i-1] A i-1 i 1 maxi=i maxi i if(K-(A[N-1]-A[0])>max) {
			maxi=0;
			max=K-(A[N-1]-A[0]);
		} K-(A[N-1]-A[0])>max K-(A[N-1]-A[0]) K (A[N-1]-A[0]) A[N-1] A N-1 N 1 A[0] A 0 max {
			maxi=0;
			max=K-(A[N-1]-A[0]);
		} maxi=0 maxi 0 max=K-(A[N-1]-A[0]) max K-(A[N-1]-A[0]) K (A[N-1]-A[0]) A[N-1] A N-1 N 1 A[0] A 0 System.out.println(K-max) System.out.println System.out System System.out K-max K max String[] args args public class Main {

	@SuppressWarnings("resource")
	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];
		int max=0;
		int maxi=0;
		A[0]=sc.nextInt();
		for(int i=1;i<N;i++) {
			A[i]=sc.nextInt();
			if(max<A[i]-A[i-1]) {
				max=A[i]-A[i-1];
				maxi=i;
			}
		}
		if(K-(A[N-1]-A[0])>max) {
			maxi=0;
			max=K-(A[N-1]-A[0]);
		}
		System.out.println(K-max);
	}
} public class Main {

	@SuppressWarnings("resource")
	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];
		int max=0;
		int maxi=0;
		A[0]=sc.nextInt();
		for(int i=1;i<N;i++) {
			A[i]=sc.nextInt();
			if(max<A[i]-A[i-1]) {
				max=A[i]-A[i-1];
				maxi=i;
			}
		}
		if(K-(A[N-1]-A[0])>max) {
			maxi=0;
			max=K-(A[N-1]-A[0]);
		}
		System.out.println(K-max);
	}
} Main
import java.util.Scanner;
public class Main {

	@SuppressWarnings("resource")
	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];
		int max=0;
		int maxi=0;
		A[0]=sc.nextInt();
		for(int i=1;i<N;i++) {
			A[i]=sc.nextInt();
			if(max<A[i]-A[i-1]) {
				max=A[i]-A[i-1];
				maxi=i;
			}
		}
		if(K-(A[N-1]-A[0])>max) {
			maxi=0;
			max=K-(A[N-1]-A[0]);
		}
		System.out.println(K-max);
	}
}
