import java.util.*;
import java.math.*;
public class Main {
	static int r; 
	static int c;
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int k = in.nextInt();
		int n = in.nextInt();
		int[] house = new int[n];
		for(int i = 0; i < n; i++) house[i] = in.nextInt();
		Arrays.sort(house);
		int ans = 0;
		if(n > 1) {
			ans = house[n - 1] - house[0];
			for(int i = 1; i < n; i++) {
				ans = Math.min(ans, (k - house[i]) + house[i - 1]);
			}
			for(int i = 0; i < n - 1 ; i++) {
				ans = Math.min(ans, house[i] + (k - house[i + 1]));
			}
		}
		System.out.println(ans);
	}
}  import java.util.*; util.* java import java.math.*; math.* java public class Main {
	static int r; 
	static int c;
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int k = in.nextInt();
		int n = in.nextInt();
		int[] house = new int[n];
		for(int i = 0; i < n; i++) house[i] = in.nextInt();
		Arrays.sort(house);
		int ans = 0;
		if(n > 1) {
			ans = house[n - 1] - house[0];
			for(int i = 1; i < n; i++) {
				ans = Math.min(ans, (k - house[i]) + house[i - 1]);
			}
			for(int i = 0; i < n - 1 ; i++) {
				ans = Math.min(ans, house[i] + (k - house[i + 1]));
			}
		}
		System.out.println(ans);
	}
} Main static int r; r static int c; c public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int k = in.nextInt();
		int n = in.nextInt();
		int[] house = new int[n];
		for(int i = 0; i < n; i++) house[i] = in.nextInt();
		Arrays.sort(house);
		int ans = 0;
		if(n > 1) {
			ans = house[n - 1] - house[0];
			for(int i = 1; i < n; i++) {
				ans = Math.min(ans, (k - house[i]) + house[i - 1]);
			}
			for(int i = 0; i < n - 1 ; i++) {
				ans = Math.min(ans, house[i] + (k - house[i + 1]));
			}
		}
		System.out.println(ans);
	} main {
		Scanner in = new Scanner(System.in);
		int k = in.nextInt();
		int n = in.nextInt();
		int[] house = new int[n];
		for(int i = 0; i < n; i++) house[i] = in.nextInt();
		Arrays.sort(house);
		int ans = 0;
		if(n > 1) {
			ans = house[n - 1] - house[0];
			for(int i = 1; i < n; i++) {
				ans = Math.min(ans, (k - house[i]) + house[i - 1]);
			}
			for(int i = 0; i < n - 1 ; i++) {
				ans = Math.min(ans, house[i] + (k - house[i + 1]));
			}
		}
		System.out.println(ans);
	} Scanner in = new Scanner(System.in); in new Scanner(System.in) int k = in.nextInt(); k in.nextInt() in.nextInt in int n = in.nextInt(); n in.nextInt() in.nextInt in int[] house = new int[n]; house new int[n] n for(int i = 0; i < n; i++) house[i] = in.nextInt(); int i = 0; int i = 0; i 0 i < n i n i++ i++ i house[i] = in.nextInt(); house[i] = in.nextInt() house[i] house i in.nextInt() in.nextInt in Arrays.sort(house) Arrays.sort Arrays house int ans = 0; ans 0 if(n > 1) {
			ans = house[n - 1] - house[0];
			for(int i = 1; i < n; i++) {
				ans = Math.min(ans, (k - house[i]) + house[i - 1]);
			}
			for(int i = 0; i < n - 1 ; i++) {
				ans = Math.min(ans, house[i] + (k - house[i + 1]));
			}
		} n > 1 n 1 {
			ans = house[n - 1] - house[0];
			for(int i = 1; i < n; i++) {
				ans = Math.min(ans, (k - house[i]) + house[i - 1]);
			}
			for(int i = 0; i < n - 1 ; i++) {
				ans = Math.min(ans, house[i] + (k - house[i + 1]));
			}
		} ans = house[n - 1] - house[0] ans house[n - 1] - house[0] house[n - 1] house n - 1 n 1 house[0] house 0 for(int i = 1; i < n; i++) {
				ans = Math.min(ans, (k - house[i]) + house[i - 1]);
			} int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
				ans = Math.min(ans, (k - house[i]) + house[i - 1]);
			} {
				ans = Math.min(ans, (k - house[i]) + house[i - 1]);
			} ans = Math.min(ans, (k - house[i]) + house[i - 1]) ans Math.min(ans, (k - house[i]) + house[i - 1]) Math.min Math ans (k - house[i]) + house[i - 1] (k - house[i]) k house[i] house i house[i - 1] house i - 1 i 1 for(int i = 0; i < n - 1 ; i++) {
				ans = Math.min(ans, house[i] + (k - house[i + 1]));
			} int i = 0; int i = 0; i 0 i < n - 1 i n - 1 n 1 i++ i++ i {
				ans = Math.min(ans, house[i] + (k - house[i + 1]));
			} {
				ans = Math.min(ans, house[i] + (k - house[i + 1]));
			} ans = Math.min(ans, house[i] + (k - house[i + 1])) ans Math.min(ans, house[i] + (k - house[i + 1])) Math.min Math ans house[i] + (k - house[i + 1]) house[i] house i (k - house[i + 1]) k house[i + 1] house i + 1 i 1 System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
	static int r; 
	static int c;
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int k = in.nextInt();
		int n = in.nextInt();
		int[] house = new int[n];
		for(int i = 0; i < n; i++) house[i] = in.nextInt();
		Arrays.sort(house);
		int ans = 0;
		if(n > 1) {
			ans = house[n - 1] - house[0];
			for(int i = 1; i < n; i++) {
				ans = Math.min(ans, (k - house[i]) + house[i - 1]);
			}
			for(int i = 0; i < n - 1 ; i++) {
				ans = Math.min(ans, house[i] + (k - house[i + 1]));
			}
		}
		System.out.println(ans);
	}
} public class Main {
	static int r; 
	static int c;
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int k = in.nextInt();
		int n = in.nextInt();
		int[] house = new int[n];
		for(int i = 0; i < n; i++) house[i] = in.nextInt();
		Arrays.sort(house);
		int ans = 0;
		if(n > 1) {
			ans = house[n - 1] - house[0];
			for(int i = 1; i < n; i++) {
				ans = Math.min(ans, (k - house[i]) + house[i - 1]);
			}
			for(int i = 0; i < n - 1 ; i++) {
				ans = Math.min(ans, house[i] + (k - house[i + 1]));
			}
		}
		System.out.println(ans);
	}
} Main
import java.util.*;
import java.math.*;
public class Main {
	static int r; 
	static int c;
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int k = in.nextInt();
		int n = in.nextInt();
		int[] house = new int[n];
		for(int i = 0; i < n; i++) house[i] = in.nextInt();
		Arrays.sort(house);
		int ans = 0;
		if(n > 1) {
			ans = house[n - 1] - house[0];
			for(int i = 1; i < n; i++) {
				ans = Math.min(ans, (k - house[i]) + house[i - 1]);
			}
			for(int i = 0; i < n - 1 ; i++) {
				ans = Math.min(ans, house[i] + (k - house[i + 1]));
			}
		}
		System.out.println(ans);
	}
} 
