import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int k = 0;
		int n = 0;

		k = sc.nextInt();
		n = sc.nextInt();

		int array[] = new int [n];

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

		int ans = Integer.MAX_VALUE;
		for(int i = 0; i < n; i++){
			int  tmp = 0;
			int index = i -1;
			if(index < 0){
				index += n;
			}
			tmp = array[index] - array[i];
			if(tmp <= 0){
				tmp += k;
			}
			ans = Math.min(ans, tmp);
		}
			System.out.println(ans);
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int k = 0;
		int n = 0;

		k = sc.nextInt();
		n = sc.nextInt();

		int array[] = new int [n];

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

		int ans = Integer.MAX_VALUE;
		for(int i = 0; i < n; i++){
			int  tmp = 0;
			int index = i -1;
			if(index < 0){
				index += n;
			}
			tmp = array[index] - array[i];
			if(tmp <= 0){
				tmp += k;
			}
			ans = Math.min(ans, tmp);
		}
			System.out.println(ans);
	}
} Main public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int k = 0;
		int n = 0;

		k = sc.nextInt();
		n = sc.nextInt();

		int array[] = new int [n];

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

		int ans = Integer.MAX_VALUE;
		for(int i = 0; i < n; i++){
			int  tmp = 0;
			int index = i -1;
			if(index < 0){
				index += n;
			}
			tmp = array[index] - array[i];
			if(tmp <= 0){
				tmp += k;
			}
			ans = Math.min(ans, tmp);
		}
			System.out.println(ans);
	} main {
		Scanner sc = new Scanner(System.in);
		int k = 0;
		int n = 0;

		k = sc.nextInt();
		n = sc.nextInt();

		int array[] = new int [n];

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

		int ans = Integer.MAX_VALUE;
		for(int i = 0; i < n; i++){
			int  tmp = 0;
			int index = i -1;
			if(index < 0){
				index += n;
			}
			tmp = array[index] - array[i];
			if(tmp <= 0){
				tmp += k;
			}
			ans = Math.min(ans, tmp);
		}
			System.out.println(ans);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int k = 0; k 0 int n = 0; n 0 k = sc.nextInt() k sc.nextInt() sc.nextInt sc n = sc.nextInt() n sc.nextInt() sc.nextInt sc int array[] = new int [n]; array new int [n] n 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 int ans = Integer.MAX_VALUE; ans Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int i = 0; i < n; i++){
			int  tmp = 0;
			int index = i -1;
			if(index < 0){
				index += n;
			}
			tmp = array[index] - array[i];
			if(tmp <= 0){
				tmp += k;
			}
			ans = Math.min(ans, tmp);
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			int  tmp = 0;
			int index = i -1;
			if(index < 0){
				index += n;
			}
			tmp = array[index] - array[i];
			if(tmp <= 0){
				tmp += k;
			}
			ans = Math.min(ans, tmp);
		} {
			int  tmp = 0;
			int index = i -1;
			if(index < 0){
				index += n;
			}
			tmp = array[index] - array[i];
			if(tmp <= 0){
				tmp += k;
			}
			ans = Math.min(ans, tmp);
		} int  tmp = 0; tmp 0 int index = i -1; index i -1 i 1 if(index < 0){
				index += n;
			} index < 0 index 0 {
				index += n;
			} index += n index n tmp = array[index] - array[i] tmp array[index] - array[i] array[index] array index array[i] array i if(tmp <= 0){
				tmp += k;
			} tmp <= 0 tmp 0 {
				tmp += k;
			} tmp += k tmp k ans = Math.min(ans, tmp) ans Math.min(ans, tmp) Math.min Math ans tmp System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int k = 0;
		int n = 0;

		k = sc.nextInt();
		n = sc.nextInt();

		int array[] = new int [n];

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

		int ans = Integer.MAX_VALUE;
		for(int i = 0; i < n; i++){
			int  tmp = 0;
			int index = i -1;
			if(index < 0){
				index += n;
			}
			tmp = array[index] - array[i];
			if(tmp <= 0){
				tmp += k;
			}
			ans = Math.min(ans, tmp);
		}
			System.out.println(ans);
	}
} public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int k = 0;
		int n = 0;

		k = sc.nextInt();
		n = sc.nextInt();

		int array[] = new int [n];

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

		int ans = Integer.MAX_VALUE;
		for(int i = 0; i < n; i++){
			int  tmp = 0;
			int index = i -1;
			if(index < 0){
				index += n;
			}
			tmp = array[index] - array[i];
			if(tmp <= 0){
				tmp += k;
			}
			ans = Math.min(ans, tmp);
		}
			System.out.println(ans);
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int k = 0;
		int n = 0;

		k = sc.nextInt();
		n = sc.nextInt();

		int array[] = new int [n];

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

		int ans = Integer.MAX_VALUE;
		for(int i = 0; i < n; i++){
			int  tmp = 0;
			int index = i -1;
			if(index < 0){
				index += n;
			}
			tmp = array[index] - array[i];
			if(tmp <= 0){
				tmp += k;
			}
			ans = Math.min(ans, tmp);
		}
			System.out.println(ans);
	}
}