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[] array = new int [n];
		
		for(int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		}
		
		int lastDistance = k - array[n - 1] + array[0];
		int longest = lastDistance;
		for(int i = 0; i < n - 1; i++) {
			int distance = array[i + 1] - array[i];
			if(longest < distance) {
				longest = distance;
			}
		}
		
		System.out.println(k - longest);
		sc.close();
		
	}
	
} 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[] array = new int [n];
		
		for(int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		}
		
		int lastDistance = k - array[n - 1] + array[0];
		int longest = lastDistance;
		for(int i = 0; i < n - 1; i++) {
			int distance = array[i + 1] - array[i];
			if(longest < distance) {
				longest = distance;
			}
		}
		
		System.out.println(k - longest);
		sc.close();
		
	}
	
} Main public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] array = new int [n];
		
		for(int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		}
		
		int lastDistance = k - array[n - 1] + array[0];
		int longest = lastDistance;
		for(int i = 0; i < n - 1; i++) {
			int distance = array[i + 1] - array[i];
			if(longest < distance) {
				longest = distance;
			}
		}
		
		System.out.println(k - longest);
		sc.close();
		
	} main {
		
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] array = new int [n];
		
		for(int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		}
		
		int lastDistance = k - array[n - 1] + array[0];
		int longest = lastDistance;
		for(int i = 0; i < n - 1; i++) {
			int distance = array[i + 1] - array[i];
			if(longest < distance) {
				longest = distance;
			}
		}
		
		System.out.println(k - longest);
		sc.close();
		
	} 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[] array = new int [n]; array new int [n] n for(int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			array[i] = sc.nextInt();
		} {
			array[i] = sc.nextInt();
		} array[i] = sc.nextInt() array[i] array i sc.nextInt() sc.nextInt sc int lastDistance = k - array[n - 1] + array[0]; lastDistance k - array[n - 1] + array[0] k - array[n - 1] k array[n - 1] array n - 1 n 1 array[0] array 0 int longest = lastDistance; longest lastDistance for(int i = 0; i < n - 1; i++) {
			int distance = array[i + 1] - array[i];
			if(longest < distance) {
				longest = distance;
			}
		} int i = 0; int i = 0; i 0 i < n - 1 i n - 1 n 1 i++ i++ i {
			int distance = array[i + 1] - array[i];
			if(longest < distance) {
				longest = distance;
			}
		} {
			int distance = array[i + 1] - array[i];
			if(longest < distance) {
				longest = distance;
			}
		} int distance = array[i + 1] - array[i]; distance array[i + 1] - array[i] array[i + 1] array i + 1 i 1 array[i] array i if(longest < distance) {
				longest = distance;
			} longest < distance longest distance {
				longest = distance;
			} longest = distance longest distance System.out.println(k - longest) System.out.println System.out System System.out k - longest k longest sc.close() sc.close sc 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[] array = new int [n];
		
		for(int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		}
		
		int lastDistance = k - array[n - 1] + array[0];
		int longest = lastDistance;
		for(int i = 0; i < n - 1; i++) {
			int distance = array[i + 1] - array[i];
			if(longest < distance) {
				longest = distance;
			}
		}
		
		System.out.println(k - longest);
		sc.close();
		
	}
	
} public class Main {
 
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] array = new int [n];
		
		for(int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		}
		
		int lastDistance = k - array[n - 1] + array[0];
		int longest = lastDistance;
		for(int i = 0; i < n - 1; i++) {
			int distance = array[i + 1] - array[i];
			if(longest < distance) {
				longest = distance;
			}
		}
		
		System.out.println(k - longest);
		sc.close();
		
	}
	
} 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[] array = new int [n];
		
		for(int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		}
		
		int lastDistance = k - array[n - 1] + array[0];
		int longest = lastDistance;
		for(int i = 0; i < n - 1; i++) {
			int distance = array[i + 1] - array[i];
			if(longest < distance) {
				longest = distance;
			}
		}
		
		System.out.println(k - longest);
		sc.close();
		
	}
	
}