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 prev = 0;
		int maxDis = 0;
		int first = 0;
		for (int i=0; i<n; i++) {
			int x = sc.nextInt();
			if (i == 0) {
				first = x;
			}
			maxDis = Math.max(x - prev, maxDis);
			prev = x;
		}
		System.out.println(k - Math.max(maxDis, first + k - prev));
		sc.close();
	}

} 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 prev = 0;
		int maxDis = 0;
		int first = 0;
		for (int i=0; i<n; i++) {
			int x = sc.nextInt();
			if (i == 0) {
				first = x;
			}
			maxDis = Math.max(x - prev, maxDis);
			prev = x;
		}
		System.out.println(k - Math.max(maxDis, first + k - prev));
		sc.close();
	}

} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int prev = 0;
		int maxDis = 0;
		int first = 0;
		for (int i=0; i<n; i++) {
			int x = sc.nextInt();
			if (i == 0) {
				first = x;
			}
			maxDis = Math.max(x - prev, maxDis);
			prev = x;
		}
		System.out.println(k - Math.max(maxDis, first + k - prev));
		sc.close();
	} main {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int prev = 0;
		int maxDis = 0;
		int first = 0;
		for (int i=0; i<n; i++) {
			int x = sc.nextInt();
			if (i == 0) {
				first = x;
			}
			maxDis = Math.max(x - prev, maxDis);
			prev = x;
		}
		System.out.println(k - Math.max(maxDis, first + k - prev));
		sc.close();
	} 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 prev = 0; prev 0 int maxDis = 0; maxDis 0 int first = 0; first 0 for (int i=0; i<n; i++) {
			int x = sc.nextInt();
			if (i == 0) {
				first = x;
			}
			maxDis = Math.max(x - prev, maxDis);
			prev = x;
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			int x = sc.nextInt();
			if (i == 0) {
				first = x;
			}
			maxDis = Math.max(x - prev, maxDis);
			prev = x;
		} {
			int x = sc.nextInt();
			if (i == 0) {
				first = x;
			}
			maxDis = Math.max(x - prev, maxDis);
			prev = x;
		} int x = sc.nextInt(); x sc.nextInt() sc.nextInt sc if (i == 0) {
				first = x;
			} i == 0 i 0 {
				first = x;
			} first = x first x maxDis = Math.max(x - prev, maxDis) maxDis Math.max(x - prev, maxDis) Math.max Math x - prev x prev maxDis prev = x prev x System.out.println(k - Math.max(maxDis, first + k - prev)) System.out.println System.out System System.out k - Math.max(maxDis, first + k - prev) k Math.max(maxDis, first + k - prev) Math.max Math maxDis first + k - prev first + k first k prev sc.close() sc.close sc 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 prev = 0;
		int maxDis = 0;
		int first = 0;
		for (int i=0; i<n; i++) {
			int x = sc.nextInt();
			if (i == 0) {
				first = x;
			}
			maxDis = Math.max(x - prev, maxDis);
			prev = x;
		}
		System.out.println(k - Math.max(maxDis, first + k - prev));
		sc.close();
	}

} public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int prev = 0;
		int maxDis = 0;
		int first = 0;
		for (int i=0; i<n; i++) {
			int x = sc.nextInt();
			if (i == 0) {
				first = x;
			}
			maxDis = Math.max(x - prev, maxDis);
			prev = x;
		}
		System.out.println(k - Math.max(maxDis, first + k - prev));
		sc.close();
	}

} 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 prev = 0;
		int maxDis = 0;
		int first = 0;
		for (int i=0; i<n; i++) {
			int x = sc.nextInt();
			if (i == 0) {
				first = x;
			}
			maxDis = Math.max(x - prev, maxDis);
			prev = x;
		}
		System.out.println(k - Math.max(maxDis, first + k - prev));
		sc.close();
	}

}
