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 max = 0;
		int data[] = new int[N];
		int data1[] = new int[N];
		for(int i = 0;i<N;i++) {
			data[i] = sc.nextInt();
		}
		max =Math.abs(k-data[N-1] + data[0]);
		for(int i =0; i<N-1; i++) {
			    max = Math.max (data[i+1]-data[i],max);
		
		}
	
		System.out.println(k-max);
	}

} 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 max = 0;
		int data[] = new int[N];
		int data1[] = new int[N];
		for(int i = 0;i<N;i++) {
			data[i] = sc.nextInt();
		}
		max =Math.abs(k-data[N-1] + data[0]);
		for(int i =0; i<N-1; i++) {
			    max = Math.max (data[i+1]-data[i],max);
		
		}
	
		System.out.println(k-max);
	}

} Main public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int N = sc.nextInt();
		int max = 0;
		int data[] = new int[N];
		int data1[] = new int[N];
		for(int i = 0;i<N;i++) {
			data[i] = sc.nextInt();
		}
		max =Math.abs(k-data[N-1] + data[0]);
		for(int i =0; i<N-1; i++) {
			    max = Math.max (data[i+1]-data[i],max);
		
		}
	
		System.out.println(k-max);
	} main {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int N = sc.nextInt();
		int max = 0;
		int data[] = new int[N];
		int data1[] = new int[N];
		for(int i = 0;i<N;i++) {
			data[i] = sc.nextInt();
		}
		max =Math.abs(k-data[N-1] + data[0]);
		for(int i =0; i<N-1; i++) {
			    max = Math.max (data[i+1]-data[i],max);
		
		}
	
		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 max = 0; max 0 int data[] = new int[N]; data new int[N] N int data1[] = new int[N]; data1 new int[N] N for(int i = 0;i<N;i++) {
			data[i] = sc.nextInt();
		} int i = 0; int i = 0; i 0 i<N i N i++ i++ i {
			data[i] = sc.nextInt();
		} {
			data[i] = sc.nextInt();
		} data[i] = sc.nextInt() data[i] data i sc.nextInt() sc.nextInt sc max =Math.abs(k-data[N-1] + data[0]) max Math.abs(k-data[N-1] + data[0]) Math.abs Math k-data[N-1] + data[0] k-data[N-1] k data[N-1] data N-1 N 1 data[0] data 0 for(int i =0; i<N-1; i++) {
			    max = Math.max (data[i+1]-data[i],max);
		
		} int i =0; int i =0; i 0 i<N-1 i N-1 N 1 i++ i++ i {
			    max = Math.max (data[i+1]-data[i],max);
		
		} {
			    max = Math.max (data[i+1]-data[i],max);
		
		} max = Math.max (data[i+1]-data[i],max) max Math.max (data[i+1]-data[i],max) Math.max Math data[i+1]-data[i] data[i+1] data i+1 i 1 data[i] data i max System.out.println(k-max) System.out.println System.out System System.out k-max k max 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 max = 0;
		int data[] = new int[N];
		int data1[] = new int[N];
		for(int i = 0;i<N;i++) {
			data[i] = sc.nextInt();
		}
		max =Math.abs(k-data[N-1] + data[0]);
		for(int i =0; i<N-1; i++) {
			    max = Math.max (data[i+1]-data[i],max);
		
		}
	
		System.out.println(k-max);
	}

} public class Main{
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int N = sc.nextInt();
		int max = 0;
		int data[] = new int[N];
		int data1[] = new int[N];
		for(int i = 0;i<N;i++) {
			data[i] = sc.nextInt();
		}
		max =Math.abs(k-data[N-1] + data[0]);
		for(int i =0; i<N-1; i++) {
			    max = Math.max (data[i+1]-data[i],max);
		
		}
	
		System.out.println(k-max);
	}

} Main
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 max = 0;
		int data[] = new int[N];
		int data1[] = new int[N];
		for(int i = 0;i<N;i++) {
			data[i] = sc.nextInt();
		}
		max =Math.abs(k-data[N-1] + data[0]);
		for(int i =0; i<N-1; i++) {
			    max = Math.max (data[i+1]-data[i],max);
		
		}
	
		System.out.println(k-max);
	}

}