import java.util.Scanner;

//AtCoder Beginner Contest 160
// C - Traveling Salesman around Lake
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int N = Integer.parseInt(sc.next());
		int[] A = new int[N];
		for (int i = 0; i < N; i++) {
			A[i] = Integer.parseInt(sc.next());
		}
		sc.close();

		int ans = Math.min(K, Math.abs((A[N - 1]) - A[0]));
		for (int i = 1; i < N; i++) {
			ans = Math.min(ans, Math.abs((K + A[i - 1]) - A[i]));
		}

		System.out.println(ans);
	};
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int N = Integer.parseInt(sc.next());
		int[] A = new int[N];
		for (int i = 0; i < N; i++) {
			A[i] = Integer.parseInt(sc.next());
		}
		sc.close();

		int ans = Math.min(K, Math.abs((A[N - 1]) - A[0]));
		for (int i = 1; i < N; i++) {
			ans = Math.min(ans, Math.abs((K + A[i - 1]) - A[i]));
		}

		System.out.println(ans);
	};
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int N = Integer.parseInt(sc.next());
		int[] A = new int[N];
		for (int i = 0; i < N; i++) {
			A[i] = Integer.parseInt(sc.next());
		}
		sc.close();

		int ans = Math.min(K, Math.abs((A[N - 1]) - A[0]));
		for (int i = 1; i < N; i++) {
			ans = Math.min(ans, Math.abs((K + A[i - 1]) - A[i]));
		}

		System.out.println(ans);
	} main {
		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int N = Integer.parseInt(sc.next());
		int[] A = new int[N];
		for (int i = 0; i < N; i++) {
			A[i] = Integer.parseInt(sc.next());
		}
		sc.close();

		int ans = Math.min(K, Math.abs((A[N - 1]) - A[0]));
		for (int i = 1; i < N; i++) {
			ans = Math.min(ans, Math.abs((K + A[i - 1]) - A[i]));
		}

		System.out.println(ans);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = Integer.parseInt(sc.next()); K Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int N = Integer.parseInt(sc.next()); N Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int[] A = new int[N]; A new int[N] N for (int i = 0; i < N; i++) {
			A[i] = Integer.parseInt(sc.next());
		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
			A[i] = Integer.parseInt(sc.next());
		} {
			A[i] = Integer.parseInt(sc.next());
		} A[i] = Integer.parseInt(sc.next()) A[i] A i Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc sc.close() sc.close sc int ans = Math.min(K, Math.abs((A[N - 1]) - A[0])); ans Math.min(K, Math.abs((A[N - 1]) - A[0])) Math.min Math K Math.abs((A[N - 1]) - A[0]) Math.abs Math (A[N - 1]) - A[0] (A[N - 1]) A N - 1 N 1 A[0] A 0 for (int i = 1; i < N; i++) {
			ans = Math.min(ans, Math.abs((K + A[i - 1]) - A[i]));
		} int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
			ans = Math.min(ans, Math.abs((K + A[i - 1]) - A[i]));
		} {
			ans = Math.min(ans, Math.abs((K + A[i - 1]) - A[i]));
		} ans = Math.min(ans, Math.abs((K + A[i - 1]) - A[i])) ans Math.min(ans, Math.abs((K + A[i - 1]) - A[i])) Math.min Math ans Math.abs((K + A[i - 1]) - A[i]) Math.abs Math (K + A[i - 1]) - A[i] (K + A[i - 1]) K A[i - 1] A i - 1 i 1 A[i] A i System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int N = Integer.parseInt(sc.next());
		int[] A = new int[N];
		for (int i = 0; i < N; i++) {
			A[i] = Integer.parseInt(sc.next());
		}
		sc.close();

		int ans = Math.min(K, Math.abs((A[N - 1]) - A[0]));
		for (int i = 1; i < N; i++) {
			ans = Math.min(ans, Math.abs((K + A[i - 1]) - A[i]));
		}

		System.out.println(ans);
	};
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int N = Integer.parseInt(sc.next());
		int[] A = new int[N];
		for (int i = 0; i < N; i++) {
			A[i] = Integer.parseInt(sc.next());
		}
		sc.close();

		int ans = Math.min(K, Math.abs((A[N - 1]) - A[0]));
		for (int i = 1; i < N; i++) {
			ans = Math.min(ans, Math.abs((K + A[i - 1]) - A[i]));
		}

		System.out.println(ans);
	};
} Main
import java.util.Scanner;

//AtCoder Beginner Contest 160
// C - Traveling Salesman around Lake
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int N = Integer.parseInt(sc.next());
		int[] A = new int[N];
		for (int i = 0; i < N; i++) {
			A[i] = Integer.parseInt(sc.next());
		}
		sc.close();

		int ans = Math.min(K, Math.abs((A[N - 1]) - A[0]));
		for (int i = 1; i < N; i++) {
			ans = Math.min(ans, Math.abs((K + A[i - 1]) - A[i]));
		}

		System.out.println(ans);
	};
}
