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

		int min = a[n - 1] - a[0];
		for (int i = 1; i < n; i++) {
			min = Math.min(min, k - a[i] + a[i - 1]);
		}

		System.out.println(min);

		scan.close();
	}

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

		int min = a[n - 1] - a[0];
		for (int i = 1; i < n; i++) {
			min = Math.min(min, k - a[i] + a[i - 1]);
		}

		System.out.println(min);

		scan.close();
	}

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

		int min = a[n - 1] - a[0];
		for (int i = 1; i < n; i++) {
			min = Math.min(min, k - a[i] + a[i - 1]);
		}

		System.out.println(min);

		scan.close();
	} 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();
		}

		int min = a[n - 1] - a[0];
		for (int i = 1; i < n; i++) {
			min = Math.min(min, k - a[i] + a[i - 1]);
		}

		System.out.println(min);

		scan.close();
	} 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 int min = a[n - 1] - a[0]; min a[n - 1] - a[0] a[n - 1] a n - 1 n 1 a[0] a 0 for (int i = 1; i < n; i++) {
			min = Math.min(min, k - a[i] + a[i - 1]);
		} int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
			min = Math.min(min, k - a[i] + a[i - 1]);
		} {
			min = Math.min(min, k - a[i] + a[i - 1]);
		} min = Math.min(min, k - a[i] + a[i - 1]) min Math.min(min, k - a[i] + a[i - 1]) Math.min Math min k - a[i] + a[i - 1] k - a[i] k a[i] a i a[i - 1] a i - 1 i 1 System.out.println(min) System.out.println System.out System System.out min scan.close() scan.close scan 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();
		}

		int min = a[n - 1] - a[0];
		for (int i = 1; i < n; i++) {
			min = Math.min(min, k - a[i] + a[i - 1]);
		}

		System.out.println(min);

		scan.close();
	}

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

		int min = a[n - 1] - a[0];
		for (int i = 1; i < n; i++) {
			min = Math.min(min, k - a[i] + a[i - 1]);
		}

		System.out.println(min);

		scan.close();
	}

} Main
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();
		}

		int min = a[n - 1] - a[0];
		for (int i = 1; i < n; i++) {
			min = Math.min(min, k - a[i] + a[i - 1]);
		}

		System.out.println(min);

		scan.close();
	}

}
