//Referred Professors code
import java.util.Scanner;
 
public class Main {
	
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] aa = new int[n];
		for (int i=0; i<n; i++)
			aa[i] = sc.nextInt();
		int gap = aa[0] + k - aa[n-1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i]-aa[i-1]);
		System.out.println(k-gap);
	}
} import java.util.Scanner; Scanner java.util public class Main {
	
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] aa = new int[n];
		for (int i=0; i<n; i++)
			aa[i] = sc.nextInt();
		int gap = aa[0] + k - aa[n-1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i]-aa[i-1]);
		System.out.println(k-gap);
	}
} Main public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] aa = new int[n];
		for (int i=0; i<n; i++)
			aa[i] = sc.nextInt();
		int gap = aa[0] + k - aa[n-1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i]-aa[i-1]);
		System.out.println(k-gap);
	} main {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] aa = new int[n];
		for (int i=0; i<n; i++)
			aa[i] = sc.nextInt();
		int gap = aa[0] + k - aa[n-1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i]-aa[i-1]);
		System.out.println(k-gap);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] aa = new int[n]; aa new int[n] n for (int i=0; i<n; i++)
			aa[i] = sc.nextInt(); int i=0; int i=0; i 0 i<n i n i++ i++ i aa[i] = sc.nextInt(); aa[i] = sc.nextInt() aa[i] aa i sc.nextInt() sc.nextInt sc int gap = aa[0] + k - aa[n-1]; gap aa[0] + k - aa[n-1] aa[0] + k aa[0] aa 0 k aa[n-1] aa n-1 n 1 for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i]-aa[i-1]); int i = 1; int i = 1; i 1 i < n i n i++ i++ i gap = Math.max(gap, aa[i]-aa[i-1]); gap = Math.max(gap, aa[i]-aa[i-1]) gap Math.max(gap, aa[i]-aa[i-1]) Math.max Math gap aa[i]-aa[i-1] aa[i] aa i aa[i-1] aa i-1 i 1 System.out.println(k-gap) System.out.println System.out System System.out k-gap k gap String args[] args public class Main {
	
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] aa = new int[n];
		for (int i=0; i<n; i++)
			aa[i] = sc.nextInt();
		int gap = aa[0] + k - aa[n-1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i]-aa[i-1]);
		System.out.println(k-gap);
	}
} public class Main {
	
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] aa = new int[n];
		for (int i=0; i<n; i++)
			aa[i] = sc.nextInt();
		int gap = aa[0] + k - aa[n-1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i]-aa[i-1]);
		System.out.println(k-gap);
	}
} Main
//Referred Professors code
import java.util.Scanner;
 
public class Main {
	
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] aa = new int[n];
		for (int i=0; i<n; i++)
			aa[i] = sc.nextInt();
		int gap = aa[0] + k - aa[n-1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i]-aa[i-1]);
		System.out.println(k-gap);
	}
}