import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ

		Scanner sc = new Scanner(System.in);

		//1周の距離
		int K = sc.nextInt();
		//家の数
		int N = sc.nextInt();

		//家の距離
		int A[] = new int[N];
		int B[] = new int[N];

		int sum = 0;
		//家の数分繰り返す
		for (int i = 0; i < N; i++) {
			A[i] = sc.nextInt();

		}

		//
		for (int i = 0; i < N; i++) {
			if (i == 0) {
				B[i] = K - A[N - 1] + A[i];
			} else {
				B[i] = A[i] - A[i - 1];
			}
		}
		Arrays.sort(B);

		for (int i = 0; i < N - 1; i++) {
			sum += B[i];

		}
		System.out.println(sum);
		sc.close();
	}

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

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ

		Scanner sc = new Scanner(System.in);

		//1周の距離
		int K = sc.nextInt();
		//家の数
		int N = sc.nextInt();

		//家の距離
		int A[] = new int[N];
		int B[] = new int[N];

		int sum = 0;
		//家の数分繰り返す
		for (int i = 0; i < N; i++) {
			A[i] = sc.nextInt();

		}

		//
		for (int i = 0; i < N; i++) {
			if (i == 0) {
				B[i] = K - A[N - 1] + A[i];
			} else {
				B[i] = A[i] - A[i - 1];
			}
		}
		Arrays.sort(B);

		for (int i = 0; i < N - 1; i++) {
			sum += B[i];

		}
		System.out.println(sum);
		sc.close();
	}

} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ

		Scanner sc = new Scanner(System.in);

		//1周の距離
		int K = sc.nextInt();
		//家の数
		int N = sc.nextInt();

		//家の距離
		int A[] = new int[N];
		int B[] = new int[N];

		int sum = 0;
		//家の数分繰り返す
		for (int i = 0; i < N; i++) {
			A[i] = sc.nextInt();

		}

		//
		for (int i = 0; i < N; i++) {
			if (i == 0) {
				B[i] = K - A[N - 1] + A[i];
			} else {
				B[i] = A[i] - A[i - 1];
			}
		}
		Arrays.sort(B);

		for (int i = 0; i < N - 1; i++) {
			sum += B[i];

		}
		System.out.println(sum);
		sc.close();
	} main {
		// TODO 自動生成されたメソッド・スタブ

		Scanner sc = new Scanner(System.in);

		//1周の距離
		int K = sc.nextInt();
		//家の数
		int N = sc.nextInt();

		//家の距離
		int A[] = new int[N];
		int B[] = new int[N];

		int sum = 0;
		//家の数分繰り返す
		for (int i = 0; i < N; i++) {
			A[i] = sc.nextInt();

		}

		//
		for (int i = 0; i < N; i++) {
			if (i == 0) {
				B[i] = K - A[N - 1] + A[i];
			} else {
				B[i] = A[i] - A[i - 1];
			}
		}
		Arrays.sort(B);

		for (int i = 0; i < N - 1; i++) {
			sum += B[i];

		}
		System.out.println(sum);
		sc.close();
	} 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 int B[] = new int[N]; B new int[N] N int sum = 0; sum 0 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 for (int i = 0; i < N; i++) {
			if (i == 0) {
				B[i] = K - A[N - 1] + A[i];
			} else {
				B[i] = A[i] - A[i - 1];
			}
		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
			if (i == 0) {
				B[i] = K - A[N - 1] + A[i];
			} else {
				B[i] = A[i] - A[i - 1];
			}
		} {
			if (i == 0) {
				B[i] = K - A[N - 1] + A[i];
			} else {
				B[i] = A[i] - A[i - 1];
			}
		} if (i == 0) {
				B[i] = K - A[N - 1] + A[i];
			} else {
				B[i] = A[i] - A[i - 1];
			} i == 0 i 0 {
				B[i] = K - A[N - 1] + A[i];
			} B[i] = K - A[N - 1] + A[i] B[i] B i K - A[N - 1] + A[i] K - A[N - 1] K A[N - 1] A N - 1 N 1 A[i] A i {
				B[i] = A[i] - A[i - 1];
			} B[i] = A[i] - A[i - 1] B[i] B i A[i] - A[i - 1] A[i] A i A[i - 1] A i - 1 i 1 Arrays.sort(B) Arrays.sort Arrays B for (int i = 0; i < N - 1; i++) {
			sum += B[i];

		} int i = 0; int i = 0; i 0 i < N - 1 i N - 1 N 1 i++ i++ i {
			sum += B[i];

		} {
			sum += B[i];

		} sum += B[i] sum B[i] B i System.out.println(sum) System.out.println System.out System System.out sum sc.close() sc.close sc String[] args args public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ

		Scanner sc = new Scanner(System.in);

		//1周の距離
		int K = sc.nextInt();
		//家の数
		int N = sc.nextInt();

		//家の距離
		int A[] = new int[N];
		int B[] = new int[N];

		int sum = 0;
		//家の数分繰り返す
		for (int i = 0; i < N; i++) {
			A[i] = sc.nextInt();

		}

		//
		for (int i = 0; i < N; i++) {
			if (i == 0) {
				B[i] = K - A[N - 1] + A[i];
			} else {
				B[i] = A[i] - A[i - 1];
			}
		}
		Arrays.sort(B);

		for (int i = 0; i < N - 1; i++) {
			sum += B[i];

		}
		System.out.println(sum);
		sc.close();
	}

} public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ

		Scanner sc = new Scanner(System.in);

		//1周の距離
		int K = sc.nextInt();
		//家の数
		int N = sc.nextInt();

		//家の距離
		int A[] = new int[N];
		int B[] = new int[N];

		int sum = 0;
		//家の数分繰り返す
		for (int i = 0; i < N; i++) {
			A[i] = sc.nextInt();

		}

		//
		for (int i = 0; i < N; i++) {
			if (i == 0) {
				B[i] = K - A[N - 1] + A[i];
			} else {
				B[i] = A[i] - A[i - 1];
			}
		}
		Arrays.sort(B);

		for (int i = 0; i < N - 1; i++) {
			sum += B[i];

		}
		System.out.println(sum);
		sc.close();
	}

} Main
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ

		Scanner sc = new Scanner(System.in);

		//1周の距離
		int K = sc.nextInt();
		//家の数
		int N = sc.nextInt();

		//家の距離
		int A[] = new int[N];
		int B[] = new int[N];

		int sum = 0;
		//家の数分繰り返す
		for (int i = 0; i < N; i++) {
			A[i] = sc.nextInt();

		}

		//
		for (int i = 0; i < N; i++) {
			if (i == 0) {
				B[i] = K - A[N - 1] + A[i];
			} else {
				B[i] = A[i] - A[i - 1];
			}
		}
		Arrays.sort(B);

		for (int i = 0; i < N - 1; i++) {
			sum += B[i];

		}
		System.out.println(sum);
		sc.close();
	}

}
