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_n = new int[n];

		for (int i = 0; i < n; i++) {
			a_n[i] = sc.nextInt();
		}

		sc.close();

		int maxDistance = 0;
		int tmp = 0;
		for (int i = 0; i < n - 1; i++) {
			tmp = a_n[i + 1] - a_n[i];
			maxDistance = Math.max(tmp, maxDistance);
		}

		maxDistance = Math.max(maxDistance, (k - a_n[n - 1] + a_n[0]));

		System.out.println(k - maxDistance);
	}
} 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_n = new int[n];

		for (int i = 0; i < n; i++) {
			a_n[i] = sc.nextInt();
		}

		sc.close();

		int maxDistance = 0;
		int tmp = 0;
		for (int i = 0; i < n - 1; i++) {
			tmp = a_n[i + 1] - a_n[i];
			maxDistance = Math.max(tmp, maxDistance);
		}

		maxDistance = Math.max(maxDistance, (k - a_n[n - 1] + a_n[0]));

		System.out.println(k - maxDistance);
	}
} Main public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] a_n = new int[n];

		for (int i = 0; i < n; i++) {
			a_n[i] = sc.nextInt();
		}

		sc.close();

		int maxDistance = 0;
		int tmp = 0;
		for (int i = 0; i < n - 1; i++) {
			tmp = a_n[i + 1] - a_n[i];
			maxDistance = Math.max(tmp, maxDistance);
		}

		maxDistance = Math.max(maxDistance, (k - a_n[n - 1] + a_n[0]));

		System.out.println(k - maxDistance);
	} main {

		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] a_n = new int[n];

		for (int i = 0; i < n; i++) {
			a_n[i] = sc.nextInt();
		}

		sc.close();

		int maxDistance = 0;
		int tmp = 0;
		for (int i = 0; i < n - 1; i++) {
			tmp = a_n[i + 1] - a_n[i];
			maxDistance = Math.max(tmp, maxDistance);
		}

		maxDistance = Math.max(maxDistance, (k - a_n[n - 1] + a_n[0]));

		System.out.println(k - maxDistance);
	} 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_n = new int[n]; a_n new int[n] n for (int i = 0; i < n; i++) {
			a_n[i] = sc.nextInt();
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			a_n[i] = sc.nextInt();
		} {
			a_n[i] = sc.nextInt();
		} a_n[i] = sc.nextInt() a_n[i] a_n i sc.nextInt() sc.nextInt sc sc.close() sc.close sc int maxDistance = 0; maxDistance 0 int tmp = 0; tmp 0 for (int i = 0; i < n - 1; i++) {
			tmp = a_n[i + 1] - a_n[i];
			maxDistance = Math.max(tmp, maxDistance);
		} int i = 0; int i = 0; i 0 i < n - 1 i n - 1 n 1 i++ i++ i {
			tmp = a_n[i + 1] - a_n[i];
			maxDistance = Math.max(tmp, maxDistance);
		} {
			tmp = a_n[i + 1] - a_n[i];
			maxDistance = Math.max(tmp, maxDistance);
		} tmp = a_n[i + 1] - a_n[i] tmp a_n[i + 1] - a_n[i] a_n[i + 1] a_n i + 1 i 1 a_n[i] a_n i maxDistance = Math.max(tmp, maxDistance) maxDistance Math.max(tmp, maxDistance) Math.max Math tmp maxDistance maxDistance = Math.max(maxDistance, (k - a_n[n - 1] + a_n[0])) maxDistance Math.max(maxDistance, (k - a_n[n - 1] + a_n[0])) Math.max Math maxDistance (k - a_n[n - 1] + a_n[0]) k - a_n[n - 1] k a_n[n - 1] a_n n - 1 n 1 a_n[0] a_n 0 System.out.println(k - maxDistance) System.out.println System.out System System.out k - maxDistance k maxDistance 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[] a_n = new int[n];

		for (int i = 0; i < n; i++) {
			a_n[i] = sc.nextInt();
		}

		sc.close();

		int maxDistance = 0;
		int tmp = 0;
		for (int i = 0; i < n - 1; i++) {
			tmp = a_n[i + 1] - a_n[i];
			maxDistance = Math.max(tmp, maxDistance);
		}

		maxDistance = Math.max(maxDistance, (k - a_n[n - 1] + a_n[0]));

		System.out.println(k - maxDistance);
	}
} 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_n = new int[n];

		for (int i = 0; i < n; i++) {
			a_n[i] = sc.nextInt();
		}

		sc.close();

		int maxDistance = 0;
		int tmp = 0;
		for (int i = 0; i < n - 1; i++) {
			tmp = a_n[i + 1] - a_n[i];
			maxDistance = Math.max(tmp, maxDistance);
		}

		maxDistance = Math.max(maxDistance, (k - a_n[n - 1] + a_n[0]));

		System.out.println(k - maxDistance);
	}
} 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_n = new int[n];

		for (int i = 0; i < n; i++) {
			a_n[i] = sc.nextInt();
		}

		sc.close();

		int maxDistance = 0;
		int tmp = 0;
		for (int i = 0; i < n - 1; i++) {
			tmp = a_n[i + 1] - a_n[i];
			maxDistance = Math.max(tmp, maxDistance);
		}

		maxDistance = Math.max(maxDistance, (k - a_n[n - 1] + a_n[0]));

		System.out.println(k - maxDistance);
	}
}
