import java.util.Scanner;

public class Main {
	static Scanner scn = new Scanner(System.in);

	public static void main(String[] args) {
		int k = scn.nextInt(), n = scn.nextInt(), temp = 0, sum = 0;
		int[] arr = new int[n + 1];
		for (int i = 1; i < n + 1; i++) {
			temp = scn.nextInt();
			arr[i] = temp - sum;
			sum += arr[i];
		}
		int min = temp - arr[1];
		for (int i = 2; i <= n - 1; i++) {
			min = Integer.min(min, Integer.min(k - arr[i + 1], k - arr[i - 1]));
		}
		System.out.println(min);
	}
} import java.util.Scanner; Scanner java.util public class Main {
	static Scanner scn = new Scanner(System.in);

	public static void main(String[] args) {
		int k = scn.nextInt(), n = scn.nextInt(), temp = 0, sum = 0;
		int[] arr = new int[n + 1];
		for (int i = 1; i < n + 1; i++) {
			temp = scn.nextInt();
			arr[i] = temp - sum;
			sum += arr[i];
		}
		int min = temp - arr[1];
		for (int i = 2; i <= n - 1; i++) {
			min = Integer.min(min, Integer.min(k - arr[i + 1], k - arr[i - 1]));
		}
		System.out.println(min);
	}
} Main static Scanner scn = new Scanner(System.in); scn new Scanner(System.in) public static void main(String[] args) {
		int k = scn.nextInt(), n = scn.nextInt(), temp = 0, sum = 0;
		int[] arr = new int[n + 1];
		for (int i = 1; i < n + 1; i++) {
			temp = scn.nextInt();
			arr[i] = temp - sum;
			sum += arr[i];
		}
		int min = temp - arr[1];
		for (int i = 2; i <= n - 1; i++) {
			min = Integer.min(min, Integer.min(k - arr[i + 1], k - arr[i - 1]));
		}
		System.out.println(min);
	} main {
		int k = scn.nextInt(), n = scn.nextInt(), temp = 0, sum = 0;
		int[] arr = new int[n + 1];
		for (int i = 1; i < n + 1; i++) {
			temp = scn.nextInt();
			arr[i] = temp - sum;
			sum += arr[i];
		}
		int min = temp - arr[1];
		for (int i = 2; i <= n - 1; i++) {
			min = Integer.min(min, Integer.min(k - arr[i + 1], k - arr[i - 1]));
		}
		System.out.println(min);
	} int k = scn.nextInt() k scn.nextInt() scn.nextInt scn n = scn.nextInt() n scn.nextInt() scn.nextInt scn temp = 0 temp 0 sum = 0; sum 0 int[] arr = new int[n + 1]; arr new int[n + 1] n + 1 n 1 for (int i = 1; i < n + 1; i++) {
			temp = scn.nextInt();
			arr[i] = temp - sum;
			sum += arr[i];
		} int i = 1; int i = 1; i 1 i < n + 1 i n + 1 n 1 i++ i++ i {
			temp = scn.nextInt();
			arr[i] = temp - sum;
			sum += arr[i];
		} {
			temp = scn.nextInt();
			arr[i] = temp - sum;
			sum += arr[i];
		} temp = scn.nextInt() temp scn.nextInt() scn.nextInt scn arr[i] = temp - sum arr[i] arr i temp - sum temp sum sum += arr[i] sum arr[i] arr i int min = temp - arr[1]; min temp - arr[1] temp arr[1] arr 1 for (int i = 2; i <= n - 1; i++) {
			min = Integer.min(min, Integer.min(k - arr[i + 1], k - arr[i - 1]));
		} int i = 2; int i = 2; i 2 i <= n - 1 i n - 1 n 1 i++ i++ i {
			min = Integer.min(min, Integer.min(k - arr[i + 1], k - arr[i - 1]));
		} {
			min = Integer.min(min, Integer.min(k - arr[i + 1], k - arr[i - 1]));
		} min = Integer.min(min, Integer.min(k - arr[i + 1], k - arr[i - 1])) min Integer.min(min, Integer.min(k - arr[i + 1], k - arr[i - 1])) Integer.min Integer min Integer.min(k - arr[i + 1], k - arr[i - 1]) Integer.min Integer k - arr[i + 1] k arr[i + 1] arr i + 1 i 1 k - arr[i - 1] k arr[i - 1] arr i - 1 i 1 System.out.println(min) System.out.println System.out System System.out min String[] args args public class Main {
	static Scanner scn = new Scanner(System.in);

	public static void main(String[] args) {
		int k = scn.nextInt(), n = scn.nextInt(), temp = 0, sum = 0;
		int[] arr = new int[n + 1];
		for (int i = 1; i < n + 1; i++) {
			temp = scn.nextInt();
			arr[i] = temp - sum;
			sum += arr[i];
		}
		int min = temp - arr[1];
		for (int i = 2; i <= n - 1; i++) {
			min = Integer.min(min, Integer.min(k - arr[i + 1], k - arr[i - 1]));
		}
		System.out.println(min);
	}
} public class Main {
	static Scanner scn = new Scanner(System.in);

	public static void main(String[] args) {
		int k = scn.nextInt(), n = scn.nextInt(), temp = 0, sum = 0;
		int[] arr = new int[n + 1];
		for (int i = 1; i < n + 1; i++) {
			temp = scn.nextInt();
			arr[i] = temp - sum;
			sum += arr[i];
		}
		int min = temp - arr[1];
		for (int i = 2; i <= n - 1; i++) {
			min = Integer.min(min, Integer.min(k - arr[i + 1], k - arr[i - 1]));
		}
		System.out.println(min);
	}
} Main
import java.util.Scanner;

public class Main {
	static Scanner scn = new Scanner(System.in);

	public static void main(String[] args) {
		int k = scn.nextInt(), n = scn.nextInt(), temp = 0, sum = 0;
		int[] arr = new int[n + 1];
		for (int i = 1; i < n + 1; i++) {
			temp = scn.nextInt();
			arr[i] = temp - sum;
			sum += arr[i];
		}
		int min = temp - arr[1];
		for (int i = 2; i <= n - 1; i++) {
			min = Integer.min(min, Integer.min(k - arr[i + 1], k - arr[i - 1]));
		}
		System.out.println(min);
	}
}
