import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] array = new int[n+1];
		int max_dis = -1;

		for(int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		}
		array[n] = k+array[0];

		for(int i = 1; i < n+1; i++) {
			int temp = array[i]-array[i-1];
			if(temp > max_dis) {
				max_dis = temp;
			}
		}
		System.out.println(k-max_dis);
	}
}
 import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] array = new int[n+1];
		int max_dis = -1;

		for(int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		}
		array[n] = k+array[0];

		for(int i = 1; i < n+1; i++) {
			int temp = array[i]-array[i-1];
			if(temp > max_dis) {
				max_dis = temp;
			}
		}
		System.out.println(k-max_dis);
	}
} Main public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] array = new int[n+1];
		int max_dis = -1;

		for(int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		}
		array[n] = k+array[0];

		for(int i = 1; i < n+1; i++) {
			int temp = array[i]-array[i-1];
			if(temp > max_dis) {
				max_dis = temp;
			}
		}
		System.out.println(k-max_dis);
	} main {

		Scanner sc = new Scanner(System.in);

		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] array = new int[n+1];
		int max_dis = -1;

		for(int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		}
		array[n] = k+array[0];

		for(int i = 1; i < n+1; i++) {
			int temp = array[i]-array[i-1];
			if(temp > max_dis) {
				max_dis = temp;
			}
		}
		System.out.println(k-max_dis);
	} 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[] array = new int[n+1]; array new int[n+1] n+1 n 1 int max_dis = -1; max_dis -1 1 for(int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			array[i] = sc.nextInt();
		} {
			array[i] = sc.nextInt();
		} array[i] = sc.nextInt() array[i] array i sc.nextInt() sc.nextInt sc array[n] = k+array[0] array[n] array n k+array[0] k array[0] array 0 for(int i = 1; i < n+1; i++) {
			int temp = array[i]-array[i-1];
			if(temp > max_dis) {
				max_dis = temp;
			}
		} int i = 1; int i = 1; i 1 i < n+1 i n+1 n 1 i++ i++ i {
			int temp = array[i]-array[i-1];
			if(temp > max_dis) {
				max_dis = temp;
			}
		} {
			int temp = array[i]-array[i-1];
			if(temp > max_dis) {
				max_dis = temp;
			}
		} int temp = array[i]-array[i-1]; temp array[i]-array[i-1] array[i] array i array[i-1] array i-1 i 1 if(temp > max_dis) {
				max_dis = temp;
			} temp > max_dis temp max_dis {
				max_dis = temp;
			} max_dis = temp max_dis temp System.out.println(k-max_dis) System.out.println System.out System System.out k-max_dis k max_dis String[] args args public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] array = new int[n+1];
		int max_dis = -1;

		for(int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		}
		array[n] = k+array[0];

		for(int i = 1; i < n+1; i++) {
			int temp = array[i]-array[i-1];
			if(temp > max_dis) {
				max_dis = temp;
			}
		}
		System.out.println(k-max_dis);
	}
} public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] array = new int[n+1];
		int max_dis = -1;

		for(int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		}
		array[n] = k+array[0];

		for(int i = 1; i < n+1; i++) {
			int temp = array[i]-array[i-1];
			if(temp > max_dis) {
				max_dis = temp;
			}
		}
		System.out.println(k-max_dis);
	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] array = new int[n+1];
		int max_dis = -1;

		for(int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		}
		array[n] = k+array[0];

		for(int i = 1; i < n+1; i++) {
			int temp = array[i]-array[i-1];
			if(temp > max_dis) {
				max_dis = temp;
			}
		}
		System.out.println(k-max_dis);
	}
}

