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 Max = 0;
		for (int i = 1; i < N; i++) {
			Max = Math.max(A[i] - A[i-1], Max);
		}
		Max = Math.max( K - (A[N-1] - A[0]), Max);

		System.out.println(K - Max);
	}
} 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 Max = 0;
		for (int i = 1; i < N; i++) {
			Max = Math.max(A[i] - A[i-1], Max);
		}
		Max = Math.max( K - (A[N-1] - A[0]), Max);

		System.out.println(K - Max);
	}
} 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 Max = 0;
		for (int i = 1; i < N; i++) {
			Max = Math.max(A[i] - A[i-1], Max);
		}
		Max = Math.max( K - (A[N-1] - A[0]), Max);

		System.out.println(K - Max);
	} 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 Max = 0;
		for (int i = 1; i < N; i++) {
			Max = Math.max(A[i] - A[i-1], Max);
		}
		Max = Math.max( K - (A[N-1] - A[0]), Max);

		System.out.println(K - Max);
	} 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 Max = 0; Max 0 for (int i = 1; i < N; i++) {
			Max = Math.max(A[i] - A[i-1], Max);
		} int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
			Max = Math.max(A[i] - A[i-1], Max);
		} {
			Max = Math.max(A[i] - A[i-1], Max);
		} Max = Math.max(A[i] - A[i-1], Max) Max Math.max(A[i] - A[i-1], Max) Math.max Math A[i] - A[i-1] A[i] A i A[i-1] A i-1 i 1 Max Max = Math.max( K - (A[N-1] - A[0]), Max) Max Math.max( K - (A[N-1] - A[0]), Max) Math.max Math K - (A[N-1] - A[0]) K (A[N-1] - A[0]) A[N-1] A N-1 N 1 A[0] A 0 Max System.out.println(K - Max) System.out.println System.out System System.out K - Max K Max 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 Max = 0;
		for (int i = 1; i < N; i++) {
			Max = Math.max(A[i] - A[i-1], Max);
		}
		Max = Math.max( K - (A[N-1] - A[0]), Max);

		System.out.println(K - Max);
	}
} 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 Max = 0;
		for (int i = 1; i < N; i++) {
			Max = Math.max(A[i] - A[i-1], Max);
		}
		Max = Math.max( K - (A[N-1] - A[0]), Max);

		System.out.println(K - Max);
	}
} 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 Max = 0;
		for (int i = 1; i < N; i++) {
			Max = Math.max(A[i] - A[i-1], Max);
		}
		Max = Math.max( K - (A[N-1] - A[0]), Max);

		System.out.println(K - Max);
	}
}
