import java.util.*;

class Main {
	Scanner sc;
	int K, N;
	void calc() {
		sc = new Scanner(System.in);
		K = sc.nextInt();
		N = sc.nextInt();
		int max = Integer.MIN_VALUE;
		int a0 = sc.nextInt();
		int last = a0;
		for (int i = 1; i < N; i++) {
			int a = sc.nextInt();
			max = Math.max(max, a-last);
			last = a;
		}
		max = Math.max(max, K-last+a0);
		System.out.println( K-max );
	}
	
	public static void main(String[] args) {
		new Main().calc();
	}
} import java.util.*; util.* java class Main {
	Scanner sc;
	int K, N;
	void calc() {
		sc = new Scanner(System.in);
		K = sc.nextInt();
		N = sc.nextInt();
		int max = Integer.MIN_VALUE;
		int a0 = sc.nextInt();
		int last = a0;
		for (int i = 1; i < N; i++) {
			int a = sc.nextInt();
			max = Math.max(max, a-last);
			last = a;
		}
		max = Math.max(max, K-last+a0);
		System.out.println( K-max );
	}
	
	public static void main(String[] args) {
		new Main().calc();
	}
} Main Scanner sc; sc int K K N; N void calc() {
		sc = new Scanner(System.in);
		K = sc.nextInt();
		N = sc.nextInt();
		int max = Integer.MIN_VALUE;
		int a0 = sc.nextInt();
		int last = a0;
		for (int i = 1; i < N; i++) {
			int a = sc.nextInt();
			max = Math.max(max, a-last);
			last = a;
		}
		max = Math.max(max, K-last+a0);
		System.out.println( K-max );
	} calc {
		sc = new Scanner(System.in);
		K = sc.nextInt();
		N = sc.nextInt();
		int max = Integer.MIN_VALUE;
		int a0 = sc.nextInt();
		int last = a0;
		for (int i = 1; i < N; i++) {
			int a = sc.nextInt();
			max = Math.max(max, a-last);
			last = a;
		}
		max = Math.max(max, K-last+a0);
		System.out.println( K-max );
	} sc = new Scanner(System.in) sc new Scanner(System.in) 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 a0 = sc.nextInt(); a0 sc.nextInt() sc.nextInt sc int last = a0; last a0 for (int i = 1; i < N; i++) {
			int a = sc.nextInt();
			max = Math.max(max, a-last);
			last = a;
		} int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
			int a = sc.nextInt();
			max = Math.max(max, a-last);
			last = a;
		} {
			int a = sc.nextInt();
			max = Math.max(max, a-last);
			last = a;
		} int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc max = Math.max(max, a-last) max Math.max(max, a-last) Math.max Math max a-last a last last = a last a max = Math.max(max, K-last+a0) max Math.max(max, K-last+a0) Math.max Math max K-last+a0 K-last K last a0 System.out.println( K-max ) System.out.println System.out System System.out K-max K max public static void main(String[] args) {
		new Main().calc();
	} main {
		new Main().calc();
	} new Main().calc() new Main().calc new Main() String[] args args
import java.util.*;

class Main {
	Scanner sc;
	int K, N;
	void calc() {
		sc = new Scanner(System.in);
		K = sc.nextInt();
		N = sc.nextInt();
		int max = Integer.MIN_VALUE;
		int a0 = sc.nextInt();
		int last = a0;
		for (int i = 1; i < N; i++) {
			int a = sc.nextInt();
			max = Math.max(max, a-last);
			last = a;
		}
		max = Math.max(max, K-last+a0);
		System.out.println( K-max );
	}
	
	public static void main(String[] args) {
		new Main().calc();
	}
}
