import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		try(Scanner sc = new Scanner(System.in)) {
			
			int k = sc.nextInt();			
			int n = sc.nextInt();
			int[] aArray = new int[n];
			
			for(int i = 0 ; i < n ; i++ ) {
				aArray[i] = sc.nextInt();
			}
			
			PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
			
			for(int i = 0 ; i < n ; i++ ) {
				if ( i == 0 ) {
					int dist = k - aArray[n-1] + aArray[0];
					pq.add(dist);
				}
				else {
					pq.add(aArray[i] - aArray[i-1]);
				}
			}
			
			int max = pq.poll();
			
			System.out.println(k - max);
			
		}
	}

} import java.util.Collections; Collections java.util import java.util.PriorityQueue; PriorityQueue java.util import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args){
		try(Scanner sc = new Scanner(System.in)) {
			
			int k = sc.nextInt();			
			int n = sc.nextInt();
			int[] aArray = new int[n];
			
			for(int i = 0 ; i < n ; i++ ) {
				aArray[i] = sc.nextInt();
			}
			
			PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
			
			for(int i = 0 ; i < n ; i++ ) {
				if ( i == 0 ) {
					int dist = k - aArray[n-1] + aArray[0];
					pq.add(dist);
				}
				else {
					pq.add(aArray[i] - aArray[i-1]);
				}
			}
			
			int max = pq.poll();
			
			System.out.println(k - max);
			
		}
	}

} Main public static void main(String[] args){
		try(Scanner sc = new Scanner(System.in)) {
			
			int k = sc.nextInt();			
			int n = sc.nextInt();
			int[] aArray = new int[n];
			
			for(int i = 0 ; i < n ; i++ ) {
				aArray[i] = sc.nextInt();
			}
			
			PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
			
			for(int i = 0 ; i < n ; i++ ) {
				if ( i == 0 ) {
					int dist = k - aArray[n-1] + aArray[0];
					pq.add(dist);
				}
				else {
					pq.add(aArray[i] - aArray[i-1]);
				}
			}
			
			int max = pq.poll();
			
			System.out.println(k - max);
			
		}
	} main {
		try(Scanner sc = new Scanner(System.in)) {
			
			int k = sc.nextInt();			
			int n = sc.nextInt();
			int[] aArray = new int[n];
			
			for(int i = 0 ; i < n ; i++ ) {
				aArray[i] = sc.nextInt();
			}
			
			PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
			
			for(int i = 0 ; i < n ; i++ ) {
				if ( i == 0 ) {
					int dist = k - aArray[n-1] + aArray[0];
					pq.add(dist);
				}
				else {
					pq.add(aArray[i] - aArray[i-1]);
				}
			}
			
			int max = pq.poll();
			
			System.out.println(k - max);
			
		}
	} try(Scanner sc = new Scanner(System.in)) {
			
			int k = sc.nextInt();			
			int n = sc.nextInt();
			int[] aArray = new int[n];
			
			for(int i = 0 ; i < n ; i++ ) {
				aArray[i] = sc.nextInt();
			}
			
			PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
			
			for(int i = 0 ; i < n ; i++ ) {
				if ( i == 0 ) {
					int dist = k - aArray[n-1] + aArray[0];
					pq.add(dist);
				}
				else {
					pq.add(aArray[i] - aArray[i-1]);
				}
			}
			
			int max = pq.poll();
			
			System.out.println(k - max);
			
		} {
			
			int k = sc.nextInt();			
			int n = sc.nextInt();
			int[] aArray = new int[n];
			
			for(int i = 0 ; i < n ; i++ ) {
				aArray[i] = sc.nextInt();
			}
			
			PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
			
			for(int i = 0 ; i < n ; i++ ) {
				if ( i == 0 ) {
					int dist = k - aArray[n-1] + aArray[0];
					pq.add(dist);
				}
				else {
					pq.add(aArray[i] - aArray[i-1]);
				}
			}
			
			int max = pq.poll();
			
			System.out.println(k - max);
			
		} Scanner sc = new Scanner(System.in) Scanner sc = new Scanner(System.in) 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[] aArray = new int[n]; aArray new int[n] n for(int i = 0 ; i < n ; i++ ) {
				aArray[i] = sc.nextInt();
			} int i = 0 ; int i = 0 ; i 0 i < n i n i++ i++ i {
				aArray[i] = sc.nextInt();
			} {
				aArray[i] = sc.nextInt();
			} aArray[i] = sc.nextInt() aArray[i] aArray i sc.nextInt() sc.nextInt sc PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); pq new PriorityQueue<>(Collections.reverseOrder()) for(int i = 0 ; i < n ; i++ ) {
				if ( i == 0 ) {
					int dist = k - aArray[n-1] + aArray[0];
					pq.add(dist);
				}
				else {
					pq.add(aArray[i] - aArray[i-1]);
				}
			} int i = 0 ; int i = 0 ; i 0 i < n i n i++ i++ i {
				if ( i == 0 ) {
					int dist = k - aArray[n-1] + aArray[0];
					pq.add(dist);
				}
				else {
					pq.add(aArray[i] - aArray[i-1]);
				}
			} {
				if ( i == 0 ) {
					int dist = k - aArray[n-1] + aArray[0];
					pq.add(dist);
				}
				else {
					pq.add(aArray[i] - aArray[i-1]);
				}
			} if ( i == 0 ) {
					int dist = k - aArray[n-1] + aArray[0];
					pq.add(dist);
				}
				else {
					pq.add(aArray[i] - aArray[i-1]);
				} i == 0 i 0 {
					int dist = k - aArray[n-1] + aArray[0];
					pq.add(dist);
				} int dist = k - aArray[n-1] + aArray[0]; dist k - aArray[n-1] + aArray[0] k - aArray[n-1] k aArray[n-1] aArray n-1 n 1 aArray[0] aArray 0 pq.add(dist) pq.add pq dist {
					pq.add(aArray[i] - aArray[i-1]);
				} pq.add(aArray[i] - aArray[i-1]) pq.add pq aArray[i] - aArray[i-1] aArray[i] aArray i aArray[i-1] aArray i-1 i 1 int max = pq.poll(); max pq.poll() pq.poll pq 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){
		try(Scanner sc = new Scanner(System.in)) {
			
			int k = sc.nextInt();			
			int n = sc.nextInt();
			int[] aArray = new int[n];
			
			for(int i = 0 ; i < n ; i++ ) {
				aArray[i] = sc.nextInt();
			}
			
			PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
			
			for(int i = 0 ; i < n ; i++ ) {
				if ( i == 0 ) {
					int dist = k - aArray[n-1] + aArray[0];
					pq.add(dist);
				}
				else {
					pq.add(aArray[i] - aArray[i-1]);
				}
			}
			
			int max = pq.poll();
			
			System.out.println(k - max);
			
		}
	}

} public class Main {
	public static void main(String[] args){
		try(Scanner sc = new Scanner(System.in)) {
			
			int k = sc.nextInt();			
			int n = sc.nextInt();
			int[] aArray = new int[n];
			
			for(int i = 0 ; i < n ; i++ ) {
				aArray[i] = sc.nextInt();
			}
			
			PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
			
			for(int i = 0 ; i < n ; i++ ) {
				if ( i == 0 ) {
					int dist = k - aArray[n-1] + aArray[0];
					pq.add(dist);
				}
				else {
					pq.add(aArray[i] - aArray[i-1]);
				}
			}
			
			int max = pq.poll();
			
			System.out.println(k - max);
			
		}
	}

} Main
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		try(Scanner sc = new Scanner(System.in)) {
			
			int k = sc.nextInt();			
			int n = sc.nextInt();
			int[] aArray = new int[n];
			
			for(int i = 0 ; i < n ; i++ ) {
				aArray[i] = sc.nextInt();
			}
			
			PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
			
			for(int i = 0 ; i < n ; i++ ) {
				if ( i == 0 ) {
					int dist = k - aArray[n-1] + aArray[0];
					pq.add(dist);
				}
				else {
					pq.add(aArray[i] - aArray[i-1]);
				}
			}
			
			int max = pq.poll();
			
			System.out.println(k - max);
			
		}
	}

}