import java.util.Scanner;

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();
		}
		Main.MergeSort(a, a.length);
		
		int max = (k - a[n-1]) + a[0];
		for(int i = 1; i < n; i++) {
			if (a[i] - a[i-1] > max) {
				max = a[i] - a[i-1];
			}
		}
		System.out.println(k - max);
	}
	
	
	static void MergeSort(int[] a,int num) {
		int mid = (num+1)/2;
		int[] left = new int[mid];
		int[] right = new int[num-mid];

		
		if(num > 1) {
			
			for(int i = 0; i < mid; i++) {left[i] = a[i];}
			for(int i = 0; i < num-mid; i++) {right[i] = a[mid+i];}
			MergeSort(left, mid);
			MergeSort(right, num-mid);
			Main.merge(a, left, right);
		}
	}
	static void merge(int[] a, int[] left, int[] right) {
		int i = 0, j = 0;
		while( i < left.length || j < right.length) {

			if(j >= right.length || (i < left.length && left[i] < right[j])) {
				a[i+j] = left[i];
				i++;
			}else {
				a[i+j] = right[j];
				j++;
			}
		}
	}
	
} import java.util.Scanner; Scanner 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();
		}
		Main.MergeSort(a, a.length);
		
		int max = (k - a[n-1]) + a[0];
		for(int i = 1; i < n; i++) {
			if (a[i] - a[i-1] > max) {
				max = a[i] - a[i-1];
			}
		}
		System.out.println(k - max);
	}
	
	
	static void MergeSort(int[] a,int num) {
		int mid = (num+1)/2;
		int[] left = new int[mid];
		int[] right = new int[num-mid];

		
		if(num > 1) {
			
			for(int i = 0; i < mid; i++) {left[i] = a[i];}
			for(int i = 0; i < num-mid; i++) {right[i] = a[mid+i];}
			MergeSort(left, mid);
			MergeSort(right, num-mid);
			Main.merge(a, left, right);
		}
	}
	static void merge(int[] a, int[] left, int[] right) {
		int i = 0, j = 0;
		while( i < left.length || j < right.length) {

			if(j >= right.length || (i < left.length && left[i] < right[j])) {
				a[i+j] = left[i];
				i++;
			}else {
				a[i+j] = right[j];
				j++;
			}
		}
	}
	
} 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();
		}
		Main.MergeSort(a, a.length);
		
		int max = (k - a[n-1]) + a[0];
		for(int i = 1; i < n; i++) {
			if (a[i] - a[i-1] > max) {
				max = a[i] - a[i-1];
			}
		}
		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];
		for(int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}
		Main.MergeSort(a, a.length);
		
		int max = (k - a[n-1]) + a[0];
		for(int i = 1; i < n; i++) {
			if (a[i] - a[i-1] > max) {
				max = a[i] - a[i-1];
			}
		}
		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 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 Main.MergeSort(a, a.length) Main.MergeSort Main a a.length a a.length int max = (k - a[n-1]) + a[0]; max (k - a[n-1]) + a[0] (k - a[n-1]) k a[n-1] a n-1 n 1 a[0] a 0 for(int i = 1; i < n; i++) {
			if (a[i] - a[i-1] > max) {
				max = a[i] - a[i-1];
			}
		} int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
			if (a[i] - a[i-1] > max) {
				max = a[i] - a[i-1];
			}
		} {
			if (a[i] - a[i-1] > max) {
				max = a[i] - a[i-1];
			}
		} if (a[i] - a[i-1] > max) {
				max = a[i] - a[i-1];
			} a[i] - a[i-1] > max a[i] - a[i-1] a[i] a i a[i-1] a i-1 i 1 max {
				max = a[i] - a[i-1];
			} max = a[i] - a[i-1] max a[i] - a[i-1] a[i] a i a[i-1] a i-1 i 1 System.out.println(k - max) System.out.println System.out System System.out k - max k max String[] args args static void MergeSort(int[] a,int num) {
		int mid = (num+1)/2;
		int[] left = new int[mid];
		int[] right = new int[num-mid];

		
		if(num > 1) {
			
			for(int i = 0; i < mid; i++) {left[i] = a[i];}
			for(int i = 0; i < num-mid; i++) {right[i] = a[mid+i];}
			MergeSort(left, mid);
			MergeSort(right, num-mid);
			Main.merge(a, left, right);
		}
	} MergeSort {
		int mid = (num+1)/2;
		int[] left = new int[mid];
		int[] right = new int[num-mid];

		
		if(num > 1) {
			
			for(int i = 0; i < mid; i++) {left[i] = a[i];}
			for(int i = 0; i < num-mid; i++) {right[i] = a[mid+i];}
			MergeSort(left, mid);
			MergeSort(right, num-mid);
			Main.merge(a, left, right);
		}
	} int mid = (num+1)/2; mid (num+1)/2 (num+1) num 1 2 int[] left = new int[mid]; left new int[mid] mid int[] right = new int[num-mid]; right new int[num-mid] num-mid num mid if(num > 1) {
			
			for(int i = 0; i < mid; i++) {left[i] = a[i];}
			for(int i = 0; i < num-mid; i++) {right[i] = a[mid+i];}
			MergeSort(left, mid);
			MergeSort(right, num-mid);
			Main.merge(a, left, right);
		} num > 1 num 1 {
			
			for(int i = 0; i < mid; i++) {left[i] = a[i];}
			for(int i = 0; i < num-mid; i++) {right[i] = a[mid+i];}
			MergeSort(left, mid);
			MergeSort(right, num-mid);
			Main.merge(a, left, right);
		} for(int i = 0; i < mid; i++) {left[i] = a[i];} int i = 0; int i = 0; i 0 i < mid i mid i++ i++ i {left[i] = a[i];} {left[i] = a[i];} left[i] = a[i] left[i] left i a[i] a i for(int i = 0; i < num-mid; i++) {right[i] = a[mid+i];} int i = 0; int i = 0; i 0 i < num-mid i num-mid num mid i++ i++ i {right[i] = a[mid+i];} {right[i] = a[mid+i];} right[i] = a[mid+i] right[i] right i a[mid+i] a mid+i mid i MergeSort(left, mid) MergeSort left mid MergeSort(right, num-mid) MergeSort right num-mid num mid Main.merge(a, left, right) Main.merge Main a left right int[] a a int num num static void merge(int[] a, int[] left, int[] right) {
		int i = 0, j = 0;
		while( i < left.length || j < right.length) {

			if(j >= right.length || (i < left.length && left[i] < right[j])) {
				a[i+j] = left[i];
				i++;
			}else {
				a[i+j] = right[j];
				j++;
			}
		}
	} merge {
		int i = 0, j = 0;
		while( i < left.length || j < right.length) {

			if(j >= right.length || (i < left.length && left[i] < right[j])) {
				a[i+j] = left[i];
				i++;
			}else {
				a[i+j] = right[j];
				j++;
			}
		}
	} int i = 0 i 0 j = 0; j 0 while( i < left.length || j < right.length) {

			if(j >= right.length || (i < left.length && left[i] < right[j])) {
				a[i+j] = left[i];
				i++;
			}else {
				a[i+j] = right[j];
				j++;
			}
		} i < left.length || j < right.length i < left.length i left.length left left.length j < right.length j right.length right right.length {

			if(j >= right.length || (i < left.length && left[i] < right[j])) {
				a[i+j] = left[i];
				i++;
			}else {
				a[i+j] = right[j];
				j++;
			}
		} if(j >= right.length || (i < left.length && left[i] < right[j])) {
				a[i+j] = left[i];
				i++;
			}else {
				a[i+j] = right[j];
				j++;
			} j >= right.length || (i < left.length && left[i] < right[j]) j >= right.length j right.length right right.length (i < left.length && left[i] < right[j]) i < left.length i left.length left left.length left[i] < right[j] left[i] left i right[j] right j {
				a[i+j] = left[i];
				i++;
			} a[i+j] = left[i] a[i+j] a i+j i j left[i] left i i++ i {
				a[i+j] = right[j];
				j++;
			} a[i+j] = right[j] a[i+j] a i+j i j right[j] right j j++ j int[] a a int[] left left int[] right right 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();
		}
		Main.MergeSort(a, a.length);
		
		int max = (k - a[n-1]) + a[0];
		for(int i = 1; i < n; i++) {
			if (a[i] - a[i-1] > max) {
				max = a[i] - a[i-1];
			}
		}
		System.out.println(k - max);
	}
	
	
	static void MergeSort(int[] a,int num) {
		int mid = (num+1)/2;
		int[] left = new int[mid];
		int[] right = new int[num-mid];

		
		if(num > 1) {
			
			for(int i = 0; i < mid; i++) {left[i] = a[i];}
			for(int i = 0; i < num-mid; i++) {right[i] = a[mid+i];}
			MergeSort(left, mid);
			MergeSort(right, num-mid);
			Main.merge(a, left, right);
		}
	}
	static void merge(int[] a, int[] left, int[] right) {
		int i = 0, j = 0;
		while( i < left.length || j < right.length) {

			if(j >= right.length || (i < left.length && left[i] < right[j])) {
				a[i+j] = left[i];
				i++;
			}else {
				a[i+j] = right[j];
				j++;
			}
		}
	}
	
} 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();
		}
		Main.MergeSort(a, a.length);
		
		int max = (k - a[n-1]) + a[0];
		for(int i = 1; i < n; i++) {
			if (a[i] - a[i-1] > max) {
				max = a[i] - a[i-1];
			}
		}
		System.out.println(k - max);
	}
	
	
	static void MergeSort(int[] a,int num) {
		int mid = (num+1)/2;
		int[] left = new int[mid];
		int[] right = new int[num-mid];

		
		if(num > 1) {
			
			for(int i = 0; i < mid; i++) {left[i] = a[i];}
			for(int i = 0; i < num-mid; i++) {right[i] = a[mid+i];}
			MergeSort(left, mid);
			MergeSort(right, num-mid);
			Main.merge(a, left, right);
		}
	}
	static void merge(int[] a, int[] left, int[] right) {
		int i = 0, j = 0;
		while( i < left.length || j < right.length) {

			if(j >= right.length || (i < left.length && left[i] < right[j])) {
				a[i+j] = left[i];
				i++;
			}else {
				a[i+j] = right[j];
				j++;
			}
		}
	}
	
} Main
import java.util.Scanner;

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();
		}
		Main.MergeSort(a, a.length);
		
		int max = (k - a[n-1]) + a[0];
		for(int i = 1; i < n; i++) {
			if (a[i] - a[i-1] > max) {
				max = a[i] - a[i-1];
			}
		}
		System.out.println(k - max);
	}
	
	
	static void MergeSort(int[] a,int num) {
		int mid = (num+1)/2;
		int[] left = new int[mid];
		int[] right = new int[num-mid];

		
		if(num > 1) {
			
			for(int i = 0; i < mid; i++) {left[i] = a[i];}
			for(int i = 0; i < num-mid; i++) {right[i] = a[mid+i];}
			MergeSort(left, mid);
			MergeSort(right, num-mid);
			Main.merge(a, left, right);
		}
	}
	static void merge(int[] a, int[] left, int[] right) {
		int i = 0, j = 0;
		while( i < left.length || j < right.length) {

			if(j >= right.length || (i < left.length && left[i] < right[j])) {
				a[i+j] = left[i];
				i++;
			}else {
				a[i+j] = right[j];
				j++;
			}
		}
	}
	
}
