import java.util.Scanner;

public class Main {

	public static void main(final String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] A = new int[N];
		for(int i = 0; i < N; i++) {
			A[i] = sc.nextInt();
		}
		sc.close();
		int wa = 0, max = 0;;
		for(int i = 0; i < N; i++) {
			int a = 0;
			if(i == N - 1) a = K - A[i] + A[0];
			else a = A[i + 1] - A[i];
			wa += a;
			if(a > max) max = a;
		}
		System.out.println(wa - max);
	}
} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(final String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] A = new int[N];
		for(int i = 0; i < N; i++) {
			A[i] = sc.nextInt();
		}
		sc.close();
		int wa = 0, max = 0;;
		for(int i = 0; i < N; i++) {
			int a = 0;
			if(i == N - 1) a = K - A[i] + A[0];
			else a = A[i + 1] - A[i];
			wa += a;
			if(a > max) max = a;
		}
		System.out.println(wa - max);
	}
} Main public static void main(final String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] A = new int[N];
		for(int i = 0; i < N; i++) {
			A[i] = sc.nextInt();
		}
		sc.close();
		int wa = 0, max = 0;;
		for(int i = 0; i < N; i++) {
			int a = 0;
			if(i == N - 1) a = K - A[i] + A[0];
			else a = A[i + 1] - A[i];
			wa += a;
			if(a > max) max = a;
		}
		System.out.println(wa - max);
	} main {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] A = new int[N];
		for(int i = 0; i < N; i++) {
			A[i] = sc.nextInt();
		}
		sc.close();
		int wa = 0, max = 0;;
		for(int i = 0; i < N; i++) {
			int a = 0;
			if(i == N - 1) a = K - A[i] + A[0];
			else a = A[i + 1] - A[i];
			wa += a;
			if(a > max) max = a;
		}
		System.out.println(wa - max);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int[] A = new int[N]; A new int[N] N for(int i = 0; i < N; i++) {
			A[i] = sc.nextInt();
		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
			A[i] = sc.nextInt();
		} {
			A[i] = sc.nextInt();
		} A[i] = sc.nextInt() A[i] A i sc.nextInt() sc.nextInt sc sc.close() sc.close sc int wa = 0 wa 0 max = 0; max 0 for(int i = 0; i < N; i++) {
			int a = 0;
			if(i == N - 1) a = K - A[i] + A[0];
			else a = A[i + 1] - A[i];
			wa += a;
			if(a > max) max = a;
		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
			int a = 0;
			if(i == N - 1) a = K - A[i] + A[0];
			else a = A[i + 1] - A[i];
			wa += a;
			if(a > max) max = a;
		} {
			int a = 0;
			if(i == N - 1) a = K - A[i] + A[0];
			else a = A[i + 1] - A[i];
			wa += a;
			if(a > max) max = a;
		} int a = 0; a 0 if(i == N - 1) a = K - A[i] + A[0];
			else a = A[i + 1] - A[i]; i == N - 1 i N - 1 N 1 a = K - A[i] + A[0] a K - A[i] + A[0] K - A[i] K A[i] A i A[0] A 0 a = A[i + 1] - A[i] a A[i + 1] - A[i] A[i + 1] A i + 1 i 1 A[i] A i wa += a wa a if(a > max) max = a; a > max a max max = a max a System.out.println(wa - max) System.out.println System.out System System.out wa - max wa max final String[] args args public class Main {

	public static void main(final String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] A = new int[N];
		for(int i = 0; i < N; i++) {
			A[i] = sc.nextInt();
		}
		sc.close();
		int wa = 0, max = 0;;
		for(int i = 0; i < N; i++) {
			int a = 0;
			if(i == N - 1) a = K - A[i] + A[0];
			else a = A[i + 1] - A[i];
			wa += a;
			if(a > max) max = a;
		}
		System.out.println(wa - max);
	}
} public class Main {

	public static void main(final String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] A = new int[N];
		for(int i = 0; i < N; i++) {
			A[i] = sc.nextInt();
		}
		sc.close();
		int wa = 0, max = 0;;
		for(int i = 0; i < N; i++) {
			int a = 0;
			if(i == N - 1) a = K - A[i] + A[0];
			else a = A[i + 1] - A[i];
			wa += a;
			if(a > max) max = a;
		}
		System.out.println(wa - max);
	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(final String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] A = new int[N];
		for(int i = 0; i < N; i++) {
			A[i] = sc.nextInt();
		}
		sc.close();
		int wa = 0, max = 0;;
		for(int i = 0; i < N; i++) {
			int a = 0;
			if(i == N - 1) a = K - A[i] + A[0];
			else a = A[i + 1] - A[i];
			wa += a;
			if(a > max) max = a;
		}
		System.out.println(wa - max);
	}
}