

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		String lines = sc.nextLine();
		String[] line = lines.split(" ");
		int K = Integer.parseInt(line[0]);
		int N = Integer.parseInt(line[1]);

		lines = sc.nextLine();
		line = lines.split(" ");
		int[] A = new int[N];
		int distance = -1;

		for(int i = 0; i < A.length; i++) {
			A[i] = Integer.parseInt(line[i]);
		}

		int sum = 0;
		int Max = -1;
		for(int i = 0; i < N-1; i++) {
			distance = A[i+1] - A[i];
			if(Max < distance) {
				Max = distance;
			}else {

			}
		}
		distance = K - (A[N-1] - A[0]);
		if(Max < distance) {
			Max = distance;
		}

		System.out.println(K - Max);

	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		String lines = sc.nextLine();
		String[] line = lines.split(" ");
		int K = Integer.parseInt(line[0]);
		int N = Integer.parseInt(line[1]);

		lines = sc.nextLine();
		line = lines.split(" ");
		int[] A = new int[N];
		int distance = -1;

		for(int i = 0; i < A.length; i++) {
			A[i] = Integer.parseInt(line[i]);
		}

		int sum = 0;
		int Max = -1;
		for(int i = 0; i < N-1; i++) {
			distance = A[i+1] - A[i];
			if(Max < distance) {
				Max = distance;
			}else {

			}
		}
		distance = K - (A[N-1] - A[0]);
		if(Max < distance) {
			Max = distance;
		}

		System.out.println(K - Max);

	}
} Main public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		String lines = sc.nextLine();
		String[] line = lines.split(" ");
		int K = Integer.parseInt(line[0]);
		int N = Integer.parseInt(line[1]);

		lines = sc.nextLine();
		line = lines.split(" ");
		int[] A = new int[N];
		int distance = -1;

		for(int i = 0; i < A.length; i++) {
			A[i] = Integer.parseInt(line[i]);
		}

		int sum = 0;
		int Max = -1;
		for(int i = 0; i < N-1; i++) {
			distance = A[i+1] - A[i];
			if(Max < distance) {
				Max = distance;
			}else {

			}
		}
		distance = K - (A[N-1] - A[0]);
		if(Max < distance) {
			Max = distance;
		}

		System.out.println(K - Max);

	} main {

		Scanner sc = new Scanner(System.in);
		String lines = sc.nextLine();
		String[] line = lines.split(" ");
		int K = Integer.parseInt(line[0]);
		int N = Integer.parseInt(line[1]);

		lines = sc.nextLine();
		line = lines.split(" ");
		int[] A = new int[N];
		int distance = -1;

		for(int i = 0; i < A.length; i++) {
			A[i] = Integer.parseInt(line[i]);
		}

		int sum = 0;
		int Max = -1;
		for(int i = 0; i < N-1; i++) {
			distance = A[i+1] - A[i];
			if(Max < distance) {
				Max = distance;
			}else {

			}
		}
		distance = K - (A[N-1] - A[0]);
		if(Max < distance) {
			Max = distance;
		}

		System.out.println(K - Max);

	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) String lines = sc.nextLine(); lines sc.nextLine() sc.nextLine sc String[] line = lines.split(" "); line lines.split(" ") lines.split lines " " int K = Integer.parseInt(line[0]); K Integer.parseInt(line[0]) Integer.parseInt Integer line[0] line 0 int N = Integer.parseInt(line[1]); N Integer.parseInt(line[1]) Integer.parseInt Integer line[1] line 1 lines = sc.nextLine() lines sc.nextLine() sc.nextLine sc line = lines.split(" ") line lines.split(" ") lines.split lines " " int[] A = new int[N]; A new int[N] N int distance = -1; distance -1 1 for(int i = 0; i < A.length; i++) {
			A[i] = Integer.parseInt(line[i]);
		} int i = 0; int i = 0; i 0 i < A.length i A.length A A.length i++ i++ i {
			A[i] = Integer.parseInt(line[i]);
		} {
			A[i] = Integer.parseInt(line[i]);
		} A[i] = Integer.parseInt(line[i]) A[i] A i Integer.parseInt(line[i]) Integer.parseInt Integer line[i] line i int sum = 0; sum 0 int Max = -1; Max -1 1 for(int i = 0; i < N-1; i++) {
			distance = A[i+1] - A[i];
			if(Max < distance) {
				Max = distance;
			}else {

			}
		} int i = 0; int i = 0; i 0 i < N-1 i N-1 N 1 i++ i++ i {
			distance = A[i+1] - A[i];
			if(Max < distance) {
				Max = distance;
			}else {

			}
		} {
			distance = A[i+1] - A[i];
			if(Max < distance) {
				Max = distance;
			}else {

			}
		} distance = A[i+1] - A[i] distance A[i+1] - A[i] A[i+1] A i+1 i 1 A[i] A i if(Max < distance) {
				Max = distance;
			}else {

			} Max < distance Max distance {
				Max = distance;
			} Max = distance Max distance {

			} distance = K - (A[N-1] - A[0]) distance K - (A[N-1] - A[0]) K (A[N-1] - A[0]) A[N-1] A N-1 N 1 A[0] A 0 if(Max < distance) {
			Max = distance;
		} Max < distance Max distance {
			Max = distance;
		} Max = distance Max distance 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 sc = new Scanner(System.in);
		String lines = sc.nextLine();
		String[] line = lines.split(" ");
		int K = Integer.parseInt(line[0]);
		int N = Integer.parseInt(line[1]);

		lines = sc.nextLine();
		line = lines.split(" ");
		int[] A = new int[N];
		int distance = -1;

		for(int i = 0; i < A.length; i++) {
			A[i] = Integer.parseInt(line[i]);
		}

		int sum = 0;
		int Max = -1;
		for(int i = 0; i < N-1; i++) {
			distance = A[i+1] - A[i];
			if(Max < distance) {
				Max = distance;
			}else {

			}
		}
		distance = K - (A[N-1] - A[0]);
		if(Max < distance) {
			Max = distance;
		}

		System.out.println(K - Max);

	}
} public class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		String lines = sc.nextLine();
		String[] line = lines.split(" ");
		int K = Integer.parseInt(line[0]);
		int N = Integer.parseInt(line[1]);

		lines = sc.nextLine();
		line = lines.split(" ");
		int[] A = new int[N];
		int distance = -1;

		for(int i = 0; i < A.length; i++) {
			A[i] = Integer.parseInt(line[i]);
		}

		int sum = 0;
		int Max = -1;
		for(int i = 0; i < N-1; i++) {
			distance = A[i+1] - A[i];
			if(Max < distance) {
				Max = distance;
			}else {

			}
		}
		distance = K - (A[N-1] - A[0]);
		if(Max < distance) {
			Max = distance;
		}

		System.out.println(K - Max);

	}
} Main


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		String lines = sc.nextLine();
		String[] line = lines.split(" ");
		int K = Integer.parseInt(line[0]);
		int N = Integer.parseInt(line[1]);

		lines = sc.nextLine();
		line = lines.split(" ");
		int[] A = new int[N];
		int distance = -1;

		for(int i = 0; i < A.length; i++) {
			A[i] = Integer.parseInt(line[i]);
		}

		int sum = 0;
		int Max = -1;
		for(int i = 0; i < N-1; i++) {
			distance = A[i+1] - A[i];
			if(Max < distance) {
				Max = distance;
			}else {

			}
		}
		distance = K - (A[N-1] - A[0]);
		if(Max < distance) {
			Max = distance;
		}

		System.out.println(K - Max);

	}
}
