import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in); 
		int P = input.nextInt();
		int N = input.nextInt();
		int[] houses = new int[N];
		for (int i = 0; i < N; i++) {
			houses[i] = input.nextInt();
		}
		int MAX = 0;
		for (int i = 0; i < N-1; i++) {
			MAX=Math.max(MAX, houses[i+1]-houses[i]);
		}
		System.out.println(Math.min(P-MAX,houses[N-1]-houses[0]));
	}
} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in); 
		int P = input.nextInt();
		int N = input.nextInt();
		int[] houses = new int[N];
		for (int i = 0; i < N; i++) {
			houses[i] = input.nextInt();
		}
		int MAX = 0;
		for (int i = 0; i < N-1; i++) {
			MAX=Math.max(MAX, houses[i+1]-houses[i]);
		}
		System.out.println(Math.min(P-MAX,houses[N-1]-houses[0]));
	}
} Main public static void main(String[] args) {
		Scanner input = new Scanner(System.in); 
		int P = input.nextInt();
		int N = input.nextInt();
		int[] houses = new int[N];
		for (int i = 0; i < N; i++) {
			houses[i] = input.nextInt();
		}
		int MAX = 0;
		for (int i = 0; i < N-1; i++) {
			MAX=Math.max(MAX, houses[i+1]-houses[i]);
		}
		System.out.println(Math.min(P-MAX,houses[N-1]-houses[0]));
	} main {
		Scanner input = new Scanner(System.in); 
		int P = input.nextInt();
		int N = input.nextInt();
		int[] houses = new int[N];
		for (int i = 0; i < N; i++) {
			houses[i] = input.nextInt();
		}
		int MAX = 0;
		for (int i = 0; i < N-1; i++) {
			MAX=Math.max(MAX, houses[i+1]-houses[i]);
		}
		System.out.println(Math.min(P-MAX,houses[N-1]-houses[0]));
	} Scanner input = new Scanner(System.in); input new Scanner(System.in) int P = input.nextInt(); P input.nextInt() input.nextInt input int N = input.nextInt(); N input.nextInt() input.nextInt input int[] houses = new int[N]; houses new int[N] N for (int i = 0; i < N; i++) {
			houses[i] = input.nextInt();
		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
			houses[i] = input.nextInt();
		} {
			houses[i] = input.nextInt();
		} houses[i] = input.nextInt() houses[i] houses i input.nextInt() input.nextInt input int MAX = 0; MAX 0 for (int i = 0; i < N-1; i++) {
			MAX=Math.max(MAX, houses[i+1]-houses[i]);
		} int i = 0; int i = 0; i 0 i < N-1 i N-1 N 1 i++ i++ i {
			MAX=Math.max(MAX, houses[i+1]-houses[i]);
		} {
			MAX=Math.max(MAX, houses[i+1]-houses[i]);
		} MAX=Math.max(MAX, houses[i+1]-houses[i]) MAX Math.max(MAX, houses[i+1]-houses[i]) Math.max Math MAX houses[i+1]-houses[i] houses[i+1] houses i+1 i 1 houses[i] houses i System.out.println(Math.min(P-MAX,houses[N-1]-houses[0])) System.out.println System.out System System.out Math.min(P-MAX,houses[N-1]-houses[0]) Math.min Math P-MAX P MAX houses[N-1]-houses[0] houses[N-1] houses N-1 N 1 houses[0] houses 0 String[] args args public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in); 
		int P = input.nextInt();
		int N = input.nextInt();
		int[] houses = new int[N];
		for (int i = 0; i < N; i++) {
			houses[i] = input.nextInt();
		}
		int MAX = 0;
		for (int i = 0; i < N-1; i++) {
			MAX=Math.max(MAX, houses[i+1]-houses[i]);
		}
		System.out.println(Math.min(P-MAX,houses[N-1]-houses[0]));
	}
} public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in); 
		int P = input.nextInt();
		int N = input.nextInt();
		int[] houses = new int[N];
		for (int i = 0; i < N; i++) {
			houses[i] = input.nextInt();
		}
		int MAX = 0;
		for (int i = 0; i < N-1; i++) {
			MAX=Math.max(MAX, houses[i+1]-houses[i]);
		}
		System.out.println(Math.min(P-MAX,houses[N-1]-houses[0]));
	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in); 
		int P = input.nextInt();
		int N = input.nextInt();
		int[] houses = new int[N];
		for (int i = 0; i < N; i++) {
			houses[i] = input.nextInt();
		}
		int MAX = 0;
		for (int i = 0; i < N-1; i++) {
			MAX=Math.max(MAX, houses[i+1]-houses[i]);
		}
		System.out.println(Math.min(P-MAX,houses[N-1]-houses[0]));
	}
}