import java.util.Scanner;

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