import java.util.Scanner;

class Main {
	public static void main(String[] $) {
		Scanner sc = new Scanner(System.in);
		int k=sc.nextInt(),n=sc.nextInt();
		int max=Integer.MIN_VALUE;
		int[] a=new int[n];
		a[0]=sc.nextInt();
		for(int i=1;i<n;i++) {
			a[i]=sc.nextInt();
			max=Math.max(max, Math.abs(a[i]-a[i-1]));
		}
		max=Math.max(max, a[0]+k-a[n-1]);
		System.out.println(k-max);
		//System.out.println(max);
	}
} import java.util.Scanner; Scanner java.util class Main {
	public static void main(String[] $) {
		Scanner sc = new Scanner(System.in);
		int k=sc.nextInt(),n=sc.nextInt();
		int max=Integer.MIN_VALUE;
		int[] a=new int[n];
		a[0]=sc.nextInt();
		for(int i=1;i<n;i++) {
			a[i]=sc.nextInt();
			max=Math.max(max, Math.abs(a[i]-a[i-1]));
		}
		max=Math.max(max, a[0]+k-a[n-1]);
		System.out.println(k-max);
		//System.out.println(max);
	}
} Main public static void main(String[] $) {
		Scanner sc = new Scanner(System.in);
		int k=sc.nextInt(),n=sc.nextInt();
		int max=Integer.MIN_VALUE;
		int[] a=new int[n];
		a[0]=sc.nextInt();
		for(int i=1;i<n;i++) {
			a[i]=sc.nextInt();
			max=Math.max(max, Math.abs(a[i]-a[i-1]));
		}
		max=Math.max(max, a[0]+k-a[n-1]);
		System.out.println(k-max);
		//System.out.println(max);
	} main {
		Scanner sc = new Scanner(System.in);
		int k=sc.nextInt(),n=sc.nextInt();
		int max=Integer.MIN_VALUE;
		int[] a=new int[n];
		a[0]=sc.nextInt();
		for(int i=1;i<n;i++) {
			a[i]=sc.nextInt();
			max=Math.max(max, Math.abs(a[i]-a[i-1]));
		}
		max=Math.max(max, a[0]+k-a[n-1]);
		System.out.println(k-max);
		//System.out.println(max);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int k=sc.nextInt() k sc.nextInt() sc.nextInt sc n=sc.nextInt(); n sc.nextInt() sc.nextInt sc int max=Integer.MIN_VALUE; max Integer.MIN_VALUE Integer Integer.MIN_VALUE int[] a=new int[n]; a new int[n] n a[0]=sc.nextInt() a[0] a 0 sc.nextInt() sc.nextInt sc for(int i=1;i<n;i++) {
			a[i]=sc.nextInt();
			max=Math.max(max, Math.abs(a[i]-a[i-1]));
		} int i=1; int i=1; i 1 i<n i n i++ i++ i {
			a[i]=sc.nextInt();
			max=Math.max(max, Math.abs(a[i]-a[i-1]));
		} {
			a[i]=sc.nextInt();
			max=Math.max(max, Math.abs(a[i]-a[i-1]));
		} a[i]=sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc max=Math.max(max, Math.abs(a[i]-a[i-1])) max Math.max(max, Math.abs(a[i]-a[i-1])) Math.max Math max Math.abs(a[i]-a[i-1]) Math.abs Math a[i]-a[i-1] a[i] a i a[i-1] a i-1 i 1 max=Math.max(max, a[0]+k-a[n-1]) max Math.max(max, a[0]+k-a[n-1]) Math.max Math max a[0]+k-a[n-1] a[0]+k a[0] a 0 k a[n-1] a n-1 n 1 System.out.println(k-max) System.out.println System.out System System.out k-max k max String[] $ $
import java.util.Scanner;

class Main {
	public static void main(String[] $) {
		Scanner sc = new Scanner(System.in);
		int k=sc.nextInt(),n=sc.nextInt();
		int max=Integer.MIN_VALUE;
		int[] a=new int[n];
		a[0]=sc.nextInt();
		for(int i=1;i<n;i++) {
			a[i]=sc.nextInt();
			max=Math.max(max, Math.abs(a[i]-a[i-1]));
		}
		max=Math.max(max, a[0]+k-a[n-1]);
		System.out.println(k-max);
		//System.out.println(max);
	}
}
