import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int K = scan.nextInt();
		int N = scan.nextInt();
		int[] A = new int[N];
		for (int i = 0; i < N; i++) {
			A[i] = scan.nextInt();
		}

		Arrays.sort(A);

		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);

	}

} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int K = scan.nextInt();
		int N = scan.nextInt();
		int[] A = new int[N];
		for (int i = 0; i < N; i++) {
			A[i] = scan.nextInt();
		}

		Arrays.sort(A);

		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 public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int K = scan.nextInt();
		int N = scan.nextInt();
		int[] A = new int[N];
		for (int i = 0; i < N; i++) {
			A[i] = scan.nextInt();
		}

		Arrays.sort(A);

		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 scan = new Scanner(System.in);

		int K = scan.nextInt();
		int N = scan.nextInt();
		int[] A = new int[N];
		for (int i = 0; i < N; i++) {
			A[i] = scan.nextInt();
		}

		Arrays.sort(A);

		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 scan = new Scanner(System.in); scan new Scanner(System.in) int K = scan.nextInt(); K scan.nextInt() scan.nextInt scan int N = scan.nextInt(); N scan.nextInt() scan.nextInt scan int[] A = new int[N]; A new int[N] N for (int i = 0; i < N; i++) {
			A[i] = scan.nextInt();
		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
			A[i] = scan.nextInt();
		} {
			A[i] = scan.nextInt();
		} A[i] = scan.nextInt() A[i] A i scan.nextInt() scan.nextInt scan Arrays.sort(A) Arrays.sort Arrays A 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 public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int K = scan.nextInt();
		int N = scan.nextInt();
		int[] A = new int[N];
		for (int i = 0; i < N; i++) {
			A[i] = scan.nextInt();
		}

		Arrays.sort(A);

		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);

	}

} public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int K = scan.nextInt();
		int N = scan.nextInt();
		int[] A = new int[N];
		for (int i = 0; i < N; i++) {
			A[i] = scan.nextInt();
		}

		Arrays.sort(A);

		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
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int K = scan.nextInt();
		int N = scan.nextInt();
		int[] A = new int[N];
		for (int i = 0; i < N; i++) {
			A[i] = scan.nextInt();
		}

		Arrays.sort(A);

		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);

	}

}