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

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] input = new int[N];
		for (int i = 0; i < input.length; i++) {
			input[i] = sc.nextInt();
		}
		int[] diff = new int[N];
		for (int i = 0; i < input.length - 1; i++) {
			diff[i] = input[i + 1] - input[i];
		}
		diff[N - 1] = K - input[N - 1] + input[0];
		Arrays.sort(diff);
		long res = 0;
		for (int i = 0; i < input.length - 1; i++) {
			res += diff[i];
		}
		System.out.println(res);

	}
} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] input = new int[N];
		for (int i = 0; i < input.length; i++) {
			input[i] = sc.nextInt();
		}
		int[] diff = new int[N];
		for (int i = 0; i < input.length - 1; i++) {
			diff[i] = input[i + 1] - input[i];
		}
		diff[N - 1] = K - input[N - 1] + input[0];
		Arrays.sort(diff);
		long res = 0;
		for (int i = 0; i < input.length - 1; i++) {
			res += diff[i];
		}
		System.out.println(res);

	}
} Main public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] input = new int[N];
		for (int i = 0; i < input.length; i++) {
			input[i] = sc.nextInt();
		}
		int[] diff = new int[N];
		for (int i = 0; i < input.length - 1; i++) {
			diff[i] = input[i + 1] - input[i];
		}
		diff[N - 1] = K - input[N - 1] + input[0];
		Arrays.sort(diff);
		long res = 0;
		for (int i = 0; i < input.length - 1; i++) {
			res += diff[i];
		}
		System.out.println(res);

	} main {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] input = new int[N];
		for (int i = 0; i < input.length; i++) {
			input[i] = sc.nextInt();
		}
		int[] diff = new int[N];
		for (int i = 0; i < input.length - 1; i++) {
			diff[i] = input[i + 1] - input[i];
		}
		diff[N - 1] = K - input[N - 1] + input[0];
		Arrays.sort(diff);
		long res = 0;
		for (int i = 0; i < input.length - 1; i++) {
			res += diff[i];
		}
		System.out.println(res);

	} 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[] input = new int[N]; input new int[N] N for (int i = 0; i < input.length; i++) {
			input[i] = sc.nextInt();
		} int i = 0; int i = 0; i 0 i < input.length i input.length input input.length i++ i++ i {
			input[i] = sc.nextInt();
		} {
			input[i] = sc.nextInt();
		} input[i] = sc.nextInt() input[i] input i sc.nextInt() sc.nextInt sc int[] diff = new int[N]; diff new int[N] N for (int i = 0; i < input.length - 1; i++) {
			diff[i] = input[i + 1] - input[i];
		} int i = 0; int i = 0; i 0 i < input.length - 1 i input.length - 1 input.length input input.length 1 i++ i++ i {
			diff[i] = input[i + 1] - input[i];
		} {
			diff[i] = input[i + 1] - input[i];
		} diff[i] = input[i + 1] - input[i] diff[i] diff i input[i + 1] - input[i] input[i + 1] input i + 1 i 1 input[i] input i diff[N - 1] = K - input[N - 1] + input[0] diff[N - 1] diff N - 1 N 1 K - input[N - 1] + input[0] K - input[N - 1] K input[N - 1] input N - 1 N 1 input[0] input 0 Arrays.sort(diff) Arrays.sort Arrays diff long res = 0; res 0 for (int i = 0; i < input.length - 1; i++) {
			res += diff[i];
		} int i = 0; int i = 0; i 0 i < input.length - 1 i input.length - 1 input.length input input.length 1 i++ i++ i {
			res += diff[i];
		} {
			res += diff[i];
		} res += diff[i] res diff[i] diff i System.out.println(res) System.out.println System.out System System.out res String[] args args public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] input = new int[N];
		for (int i = 0; i < input.length; i++) {
			input[i] = sc.nextInt();
		}
		int[] diff = new int[N];
		for (int i = 0; i < input.length - 1; i++) {
			diff[i] = input[i + 1] - input[i];
		}
		diff[N - 1] = K - input[N - 1] + input[0];
		Arrays.sort(diff);
		long res = 0;
		for (int i = 0; i < input.length - 1; i++) {
			res += diff[i];
		}
		System.out.println(res);

	}
} public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] input = new int[N];
		for (int i = 0; i < input.length; i++) {
			input[i] = sc.nextInt();
		}
		int[] diff = new int[N];
		for (int i = 0; i < input.length - 1; i++) {
			diff[i] = input[i + 1] - input[i];
		}
		diff[N - 1] = K - input[N - 1] + input[0];
		Arrays.sort(diff);
		long res = 0;
		for (int i = 0; i < input.length - 1; i++) {
			res += diff[i];
		}
		System.out.println(res);

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

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();
		int[] input = new int[N];
		for (int i = 0; i < input.length; i++) {
			input[i] = sc.nextInt();
		}
		int[] diff = new int[N];
		for (int i = 0; i < input.length - 1; i++) {
			diff[i] = input[i + 1] - input[i];
		}
		diff[N - 1] = K - input[N - 1] + input[0];
		Arrays.sort(diff);
		long res = 0;
		for (int i = 0; i < input.length - 1; i++) {
			res += diff[i];
		}
		System.out.println(res);

	}
}
