import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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();
		List<Integer> A = new ArrayList<>();
 
		for (int i=0; i<N; i++) {
			A.add(sc.nextInt());
		}
		
		Collections.sort(A);
		
		int max = 0;
		for (int i=0; i<N - 1; i++) {
			int kyori = A.get(i + 1) - A.get(i);
			max = Math.max(kyori, max);
		}
		int kyori = A.get(0) + (K - A.get(N - 1));
		max = Math.max(kyori, max);
		
		System.out.println(K - max);
	}
} import java.util.ArrayList; ArrayList java.util import java.util.Collections; Collections java.util import java.util.List; List java.util 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();
		List<Integer> A = new ArrayList<>();
 
		for (int i=0; i<N; i++) {
			A.add(sc.nextInt());
		}
		
		Collections.sort(A);
		
		int max = 0;
		for (int i=0; i<N - 1; i++) {
			int kyori = A.get(i + 1) - A.get(i);
			max = Math.max(kyori, max);
		}
		int kyori = A.get(0) + (K - A.get(N - 1));
		max = Math.max(kyori, 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();
		List<Integer> A = new ArrayList<>();
 
		for (int i=0; i<N; i++) {
			A.add(sc.nextInt());
		}
		
		Collections.sort(A);
		
		int max = 0;
		for (int i=0; i<N - 1; i++) {
			int kyori = A.get(i + 1) - A.get(i);
			max = Math.max(kyori, max);
		}
		int kyori = A.get(0) + (K - A.get(N - 1));
		max = Math.max(kyori, max);
		
		System.out.println(K - max);
	} main {
		Scanner sc = new Scanner(System.in);
 
		int K = sc.nextInt();
		int N = sc.nextInt();
		List<Integer> A = new ArrayList<>();
 
		for (int i=0; i<N; i++) {
			A.add(sc.nextInt());
		}
		
		Collections.sort(A);
		
		int max = 0;
		for (int i=0; i<N - 1; i++) {
			int kyori = A.get(i + 1) - A.get(i);
			max = Math.max(kyori, max);
		}
		int kyori = A.get(0) + (K - A.get(N - 1));
		max = Math.max(kyori, 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 List<Integer> A = new ArrayList<>(); A new ArrayList<>() for (int i=0; i<N; i++) {
			A.add(sc.nextInt());
		} int i=0; int i=0; i 0 i<N i N i++ i++ i {
			A.add(sc.nextInt());
		} {
			A.add(sc.nextInt());
		} A.add(sc.nextInt()) A.add A sc.nextInt() sc.nextInt sc Collections.sort(A) Collections.sort Collections A int max = 0; max 0 for (int i=0; i<N - 1; i++) {
			int kyori = A.get(i + 1) - A.get(i);
			max = Math.max(kyori, max);
		} int i=0; int i=0; i 0 i<N - 1 i N - 1 N 1 i++ i++ i {
			int kyori = A.get(i + 1) - A.get(i);
			max = Math.max(kyori, max);
		} {
			int kyori = A.get(i + 1) - A.get(i);
			max = Math.max(kyori, max);
		} int kyori = A.get(i + 1) - A.get(i); kyori A.get(i + 1) - A.get(i) A.get(i + 1) A.get A i + 1 i 1 A.get(i) A.get A i max = Math.max(kyori, max) max Math.max(kyori, max) Math.max Math kyori max int kyori = A.get(0) + (K - A.get(N - 1)); kyori A.get(0) + (K - A.get(N - 1)) A.get(0) A.get A 0 (K - A.get(N - 1)) K A.get(N - 1) A.get A N - 1 N 1 max = Math.max(kyori, max) max Math.max(kyori, max) Math.max Math kyori 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();
		List<Integer> A = new ArrayList<>();
 
		for (int i=0; i<N; i++) {
			A.add(sc.nextInt());
		}
		
		Collections.sort(A);
		
		int max = 0;
		for (int i=0; i<N - 1; i++) {
			int kyori = A.get(i + 1) - A.get(i);
			max = Math.max(kyori, max);
		}
		int kyori = A.get(0) + (K - A.get(N - 1));
		max = Math.max(kyori, 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();
		List<Integer> A = new ArrayList<>();
 
		for (int i=0; i<N; i++) {
			A.add(sc.nextInt());
		}
		
		Collections.sort(A);
		
		int max = 0;
		for (int i=0; i<N - 1; i++) {
			int kyori = A.get(i + 1) - A.get(i);
			max = Math.max(kyori, max);
		}
		int kyori = A.get(0) + (K - A.get(N - 1));
		max = Math.max(kyori, max);
		
		System.out.println(K - max);
	}
} Main
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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();
		List<Integer> A = new ArrayList<>();
 
		for (int i=0; i<N; i++) {
			A.add(sc.nextInt());
		}
		
		Collections.sort(A);
		
		int max = 0;
		for (int i=0; i<N - 1; i++) {
			int kyori = A.get(i + 1) - A.get(i);
			max = Math.max(kyori, max);
		}
		int kyori = A.get(0) + (K - A.get(N - 1));
		max = Math.max(kyori, max);
		
		System.out.println(K - max);
	}
}