import java.util.Arrays;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int k = scanner.nextInt();
		int n = scanner.nextInt();
		int[] a = new int[n+1];
		int min = Integer.MAX_VALUE;
		for(int i=0; i<n; i++) {
			a[i] = (scanner.nextInt())%k;
		}
		a[n] = Integer.MAX_VALUE;
		a[n] = k+a[0];

		for(int i=0; i<n; i++) {
			min = Math.min(min, k - a[i+1] + a[i]);
		}

		System.out.println(min);
	}
} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int k = scanner.nextInt();
		int n = scanner.nextInt();
		int[] a = new int[n+1];
		int min = Integer.MAX_VALUE;
		for(int i=0; i<n; i++) {
			a[i] = (scanner.nextInt())%k;
		}
		a[n] = Integer.MAX_VALUE;
		a[n] = k+a[0];

		for(int i=0; i<n; i++) {
			min = Math.min(min, k - a[i+1] + a[i]);
		}

		System.out.println(min);
	}
} Main public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int k = scanner.nextInt();
		int n = scanner.nextInt();
		int[] a = new int[n+1];
		int min = Integer.MAX_VALUE;
		for(int i=0; i<n; i++) {
			a[i] = (scanner.nextInt())%k;
		}
		a[n] = Integer.MAX_VALUE;
		a[n] = k+a[0];

		for(int i=0; i<n; i++) {
			min = Math.min(min, k - a[i+1] + a[i]);
		}

		System.out.println(min);
	} main {
		Scanner scanner = new Scanner(System.in);
		int k = scanner.nextInt();
		int n = scanner.nextInt();
		int[] a = new int[n+1];
		int min = Integer.MAX_VALUE;
		for(int i=0; i<n; i++) {
			a[i] = (scanner.nextInt())%k;
		}
		a[n] = Integer.MAX_VALUE;
		a[n] = k+a[0];

		for(int i=0; i<n; i++) {
			min = Math.min(min, k - a[i+1] + a[i]);
		}

		System.out.println(min);
	} Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int k = scanner.nextInt(); k scanner.nextInt() scanner.nextInt scanner int n = scanner.nextInt(); n scanner.nextInt() scanner.nextInt scanner int[] a = new int[n+1]; a new int[n+1] n+1 n 1 int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int i=0; i<n; i++) {
			a[i] = (scanner.nextInt())%k;
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			a[i] = (scanner.nextInt())%k;
		} {
			a[i] = (scanner.nextInt())%k;
		} a[i] = (scanner.nextInt())%k a[i] a i (scanner.nextInt())%k (scanner.nextInt()) scanner.nextInt scanner k a[n] = Integer.MAX_VALUE a[n] a n Integer.MAX_VALUE Integer Integer.MAX_VALUE a[n] = k+a[0] a[n] a n k+a[0] k a[0] a 0 for(int i=0; i<n; i++) {
			min = Math.min(min, k - a[i+1] + a[i]);
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			min = Math.min(min, k - a[i+1] + a[i]);
		} {
			min = Math.min(min, k - a[i+1] + a[i]);
		} min = Math.min(min, k - a[i+1] + a[i]) min Math.min(min, k - a[i+1] + a[i]) Math.min Math min k - a[i+1] + a[i] k - a[i+1] k a[i+1] a i+1 i 1 a[i] a i System.out.println(min) System.out.println System.out System System.out min String[] args args
import java.util.Arrays;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int k = scanner.nextInt();
		int n = scanner.nextInt();
		int[] a = new int[n+1];
		int min = Integer.MAX_VALUE;
		for(int i=0; i<n; i++) {
			a[i] = (scanner.nextInt())%k;
		}
		a[n] = Integer.MAX_VALUE;
		a[n] = k+a[0];

		for(int i=0; i<n; i++) {
			min = Math.min(min, k - a[i+1] + a[i]);
		}

		System.out.println(min);
	}
}