import java.io.*;
import java.util.*;

public class Main extends PrintWriter {
	Main() { super(System.out, true); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		Main o = new Main(); o.main(); o.flush();
	}

	void main() {
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] aa = new int[n];
		for (int i = 0; i < n; i++)
			aa[i] = sc.nextInt();
		int gap = aa[0] + k - aa[n - 1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i] - aa[i - 1]);
		println(k - gap);
	}
} import java.io.*; io.* java import java.util.*; util.* java public class Main extends PrintWriter {
	Main() { super(System.out, true); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		Main o = new Main(); o.main(); o.flush();
	}

	void main() {
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] aa = new int[n];
		for (int i = 0; i < n; i++)
			aa[i] = sc.nextInt();
		int gap = aa[0] + k - aa[n - 1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i] - aa[i - 1]);
		println(k - gap);
	}
} Main Scanner sc = new Scanner(System.in); sc new Scanner(System.in) Main() { super(System.out, true); } Main { super(System.out, true); } super(System.out, true) super System.out System System.out true public static void main(String[] $) {
		Main o = new Main(); o.main(); o.flush();
	} main {
		Main o = new Main(); o.main(); o.flush();
	} Main o = new Main(); o new Main() o.main() o.main o o.flush() o.flush o String[] $ $ void main() {
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] aa = new int[n];
		for (int i = 0; i < n; i++)
			aa[i] = sc.nextInt();
		int gap = aa[0] + k - aa[n - 1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i] - aa[i - 1]);
		println(k - gap);
	} main {
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] aa = new int[n];
		for (int i = 0; i < n; i++)
			aa[i] = sc.nextInt();
		int gap = aa[0] + k - aa[n - 1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i] - aa[i - 1]);
		println(k - gap);
	} int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] aa = new int[n]; aa new int[n] n for (int i = 0; i < n; i++)
			aa[i] = sc.nextInt(); int i = 0; int i = 0; i 0 i < n i n i++ i++ i aa[i] = sc.nextInt(); aa[i] = sc.nextInt() aa[i] aa i sc.nextInt() sc.nextInt sc int gap = aa[0] + k - aa[n - 1]; gap aa[0] + k - aa[n - 1] aa[0] + k aa[0] aa 0 k aa[n - 1] aa n - 1 n 1 for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i] - aa[i - 1]); int i = 1; int i = 1; i 1 i < n i n i++ i++ i gap = Math.max(gap, aa[i] - aa[i - 1]); gap = Math.max(gap, aa[i] - aa[i - 1]) gap Math.max(gap, aa[i] - aa[i - 1]) Math.max Math gap aa[i] - aa[i - 1] aa[i] aa i aa[i - 1] aa i - 1 i 1 println(k - gap) println k - gap k gap PrintWriter public class Main extends PrintWriter {
	Main() { super(System.out, true); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		Main o = new Main(); o.main(); o.flush();
	}

	void main() {
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] aa = new int[n];
		for (int i = 0; i < n; i++)
			aa[i] = sc.nextInt();
		int gap = aa[0] + k - aa[n - 1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i] - aa[i - 1]);
		println(k - gap);
	}
} public class Main extends PrintWriter {
	Main() { super(System.out, true); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		Main o = new Main(); o.main(); o.flush();
	}

	void main() {
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] aa = new int[n];
		for (int i = 0; i < n; i++)
			aa[i] = sc.nextInt();
		int gap = aa[0] + k - aa[n - 1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i] - aa[i - 1]);
		println(k - gap);
	}
} Main
import java.io.*;
import java.util.*;

public class Main extends PrintWriter {
	Main() { super(System.out, true); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		Main o = new Main(); o.main(); o.flush();
	}

	void main() {
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] aa = new int[n];
		for (int i = 0; i < n; i++)
			aa[i] = sc.nextInt();
		int gap = aa[0] + k - aa[n - 1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i] - aa[i - 1]);
		println(k - gap);
	}
}
