import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();

		int[] a = new int[n];
		int[] b = new int[n];

		int i;
		for(i = 0; i < n; i++)
			a[i] = scan.nextInt();

		b[0] = a[n - 1] - a[0];
		for(i = 1; i < n; i++) {
			int end = i - 1;
			if(a[i] > a[end])
				b[i] = (a[end] + k) - a[i];
			else
				b[i] = a[end] - a[i];

		}

		long ans = Integer.MAX_VALUE;
		for(i = 0; i < n; i++) {
			if(b[i] < ans) {
				ans = b[i];
			}
		}

		System.out.println(ans);
	}
} import java.util.Scanner; Scanner java.util public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();

		int[] a = new int[n];
		int[] b = new int[n];

		int i;
		for(i = 0; i < n; i++)
			a[i] = scan.nextInt();

		b[0] = a[n - 1] - a[0];
		for(i = 1; i < n; i++) {
			int end = i - 1;
			if(a[i] > a[end])
				b[i] = (a[end] + k) - a[i];
			else
				b[i] = a[end] - a[i];

		}

		long ans = Integer.MAX_VALUE;
		for(i = 0; i < n; i++) {
			if(b[i] < ans) {
				ans = b[i];
			}
		}

		System.out.println(ans);
	}
} Main public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();

		int[] a = new int[n];
		int[] b = new int[n];

		int i;
		for(i = 0; i < n; i++)
			a[i] = scan.nextInt();

		b[0] = a[n - 1] - a[0];
		for(i = 1; i < n; i++) {
			int end = i - 1;
			if(a[i] > a[end])
				b[i] = (a[end] + k) - a[i];
			else
				b[i] = a[end] - a[i];

		}

		long ans = Integer.MAX_VALUE;
		for(i = 0; i < n; i++) {
			if(b[i] < ans) {
				ans = b[i];
			}
		}

		System.out.println(ans);
	} main {
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();

		int[] a = new int[n];
		int[] b = new int[n];

		int i;
		for(i = 0; i < n; i++)
			a[i] = scan.nextInt();

		b[0] = a[n - 1] - a[0];
		for(i = 1; i < n; i++) {
			int end = i - 1;
			if(a[i] > a[end])
				b[i] = (a[end] + k) - a[i];
			else
				b[i] = a[end] - a[i];

		}

		long ans = Integer.MAX_VALUE;
		for(i = 0; i < n; i++) {
			if(b[i] < ans) {
				ans = b[i];
			}
		}

		System.out.println(ans);
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan int n = scan.nextInt(); n scan.nextInt() scan.nextInt scan int[] a = new int[n]; a new int[n] n int[] b = new int[n]; b new int[n] n int i; i for(i = 0; i < n; i++)
			a[i] = scan.nextInt(); i = 0; i = 0 i 0 i < n i n i++ i++ i a[i] = scan.nextInt(); a[i] = scan.nextInt() a[i] a i scan.nextInt() scan.nextInt scan b[0] = a[n - 1] - a[0] b[0] b 0 a[n - 1] - a[0] a[n - 1] a n - 1 n 1 a[0] a 0 for(i = 1; i < n; i++) {
			int end = i - 1;
			if(a[i] > a[end])
				b[i] = (a[end] + k) - a[i];
			else
				b[i] = a[end] - a[i];

		} i = 1; i = 1 i 1 i < n i n i++ i++ i {
			int end = i - 1;
			if(a[i] > a[end])
				b[i] = (a[end] + k) - a[i];
			else
				b[i] = a[end] - a[i];

		} {
			int end = i - 1;
			if(a[i] > a[end])
				b[i] = (a[end] + k) - a[i];
			else
				b[i] = a[end] - a[i];

		} int end = i - 1; end i - 1 i 1 if(a[i] > a[end])
				b[i] = (a[end] + k) - a[i];
			else
				b[i] = a[end] - a[i]; a[i] > a[end] a[i] a i a[end] a end b[i] = (a[end] + k) - a[i] b[i] b i (a[end] + k) - a[i] (a[end] + k) a[end] a end k a[i] a i b[i] = a[end] - a[i] b[i] b i a[end] - a[i] a[end] a end a[i] a i long ans = Integer.MAX_VALUE; ans Integer.MAX_VALUE Integer Integer.MAX_VALUE for(i = 0; i < n; i++) {
			if(b[i] < ans) {
				ans = b[i];
			}
		} i = 0; i = 0 i 0 i < n i n i++ i++ i {
			if(b[i] < ans) {
				ans = b[i];
			}
		} {
			if(b[i] < ans) {
				ans = b[i];
			}
		} if(b[i] < ans) {
				ans = b[i];
			} b[i] < ans b[i] b i ans {
				ans = b[i];
			} ans = b[i] ans b[i] b i 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 scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();

		int[] a = new int[n];
		int[] b = new int[n];

		int i;
		for(i = 0; i < n; i++)
			a[i] = scan.nextInt();

		b[0] = a[n - 1] - a[0];
		for(i = 1; i < n; i++) {
			int end = i - 1;
			if(a[i] > a[end])
				b[i] = (a[end] + k) - a[i];
			else
				b[i] = a[end] - a[i];

		}

		long ans = Integer.MAX_VALUE;
		for(i = 0; i < n; i++) {
			if(b[i] < ans) {
				ans = b[i];
			}
		}

		System.out.println(ans);
	}
} public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();

		int[] a = new int[n];
		int[] b = new int[n];

		int i;
		for(i = 0; i < n; i++)
			a[i] = scan.nextInt();

		b[0] = a[n - 1] - a[0];
		for(i = 1; i < n; i++) {
			int end = i - 1;
			if(a[i] > a[end])
				b[i] = (a[end] + k) - a[i];
			else
				b[i] = a[end] - a[i];

		}

		long ans = Integer.MAX_VALUE;
		for(i = 0; i < n; i++) {
			if(b[i] < ans) {
				ans = b[i];
			}
		}

		System.out.println(ans);
	}
} Main
import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();

		int[] a = new int[n];
		int[] b = new int[n];

		int i;
		for(i = 0; i < n; i++)
			a[i] = scan.nextInt();

		b[0] = a[n - 1] - a[0];
		for(i = 1; i < n; i++) {
			int end = i - 1;
			if(a[i] > a[end])
				b[i] = (a[end] + k) - a[i];
			else
				b[i] = a[end] - a[i];

		}

		long ans = Integer.MAX_VALUE;
		for(i = 0; i < n; i++) {
			if(b[i] < ans) {
				ans = b[i];
			}
		}

		System.out.println(ans);
	}
}