import java.util.Scanner;

public class Main {

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

	}

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

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

	}

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

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

	} int K = 0; K 0 int N = 0; N 0 Scanner sc = new Scanner(System.in); sc new Scanner(System.in) K = Integer.parseInt(sc.next()) K Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc 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 int i; i for (i = 0; i < N; i++) {
			A[i] = Integer.parseInt(sc.next());
		} i = 0; 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 int max = 0; max 0 for (i = 0; i < N - 1; i++) {
			if (max < A[i + 1] - A[i]) {
				max = A[i + 1] - A[i];
			}
		} i = 0; i = 0 i 0 i < N - 1 i N - 1 N 1 i++ i++ i {
			if (max < A[i + 1] - A[i]) {
				max = A[i + 1] - A[i];
			}
		} {
			if (max < A[i + 1] - A[i]) {
				max = A[i + 1] - A[i];
			}
		} if (max < A[i + 1] - A[i]) {
				max = A[i + 1] - A[i];
			} max < A[i + 1] - A[i] max A[i + 1] - A[i] A[i + 1] A i + 1 i 1 A[i] A i {
				max = A[i + 1] - A[i];
			} max = A[i + 1] - A[i] max A[i + 1] - A[i] A[i + 1] A i + 1 i 1 A[i] A i if (max < A[0] + K - A[N - 1]) {
			max = A[0] + K - A[N - 1];
		} max < A[0] + K - A[N - 1] max A[0] + K - A[N - 1] A[0] + K A[0] A 0 K A[N - 1] A N - 1 N 1 {
			max = A[0] + K - A[N - 1];
		} max = A[0] + K - A[N - 1] max A[0] + K - A[N - 1] A[0] + K A[0] A 0 K A[N - 1] A N - 1 N 1 System.out.println(K - max) System.out.println System.out System System.out K - max K max sc.close() sc.close sc String[] args args public class Main {

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

	}

} public class Main {

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

	}

} Main
import java.util.Scanner;

public class Main {

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

	}

}