import java.util.Scanner;

public class Main{
	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		String[] tmp = scanner.nextLine().split(" ");
		Long k 	     = Long.parseLong(tmp[0]);
		int  n 	     = Integer.parseInt(tmp[1]);

		String[] tp  = scanner.nextLine().split(" ");
		long[] list  = new long[n];
		for(int i = 0; i < n; i++) list[i] = Long.parseLong(tp[i]);

		long ans = 0;

		for(int j = 0; j < n; j++) {

			if(j != n - 1) ans = Math.max(ans, list[j + 1] - list[j]);
			else ans = Math.max(ans, Math.abs(list[0] +  (k- list[j])));


		}

		System.out.println(k - ans);



	}
} import java.util.Scanner; Scanner java.util public class Main{
	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		String[] tmp = scanner.nextLine().split(" ");
		Long k 	     = Long.parseLong(tmp[0]);
		int  n 	     = Integer.parseInt(tmp[1]);

		String[] tp  = scanner.nextLine().split(" ");
		long[] list  = new long[n];
		for(int i = 0; i < n; i++) list[i] = Long.parseLong(tp[i]);

		long ans = 0;

		for(int j = 0; j < n; j++) {

			if(j != n - 1) ans = Math.max(ans, list[j + 1] - list[j]);
			else ans = Math.max(ans, Math.abs(list[0] +  (k- list[j])));


		}

		System.out.println(k - ans);



	}
} Main public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		String[] tmp = scanner.nextLine().split(" ");
		Long k 	     = Long.parseLong(tmp[0]);
		int  n 	     = Integer.parseInt(tmp[1]);

		String[] tp  = scanner.nextLine().split(" ");
		long[] list  = new long[n];
		for(int i = 0; i < n; i++) list[i] = Long.parseLong(tp[i]);

		long ans = 0;

		for(int j = 0; j < n; j++) {

			if(j != n - 1) ans = Math.max(ans, list[j + 1] - list[j]);
			else ans = Math.max(ans, Math.abs(list[0] +  (k- list[j])));


		}

		System.out.println(k - ans);



	} main {

		Scanner scanner = new Scanner(System.in);

		String[] tmp = scanner.nextLine().split(" ");
		Long k 	     = Long.parseLong(tmp[0]);
		int  n 	     = Integer.parseInt(tmp[1]);

		String[] tp  = scanner.nextLine().split(" ");
		long[] list  = new long[n];
		for(int i = 0; i < n; i++) list[i] = Long.parseLong(tp[i]);

		long ans = 0;

		for(int j = 0; j < n; j++) {

			if(j != n - 1) ans = Math.max(ans, list[j + 1] - list[j]);
			else ans = Math.max(ans, Math.abs(list[0] +  (k- list[j])));


		}

		System.out.println(k - ans);



	} Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) String[] tmp = scanner.nextLine().split(" "); tmp scanner.nextLine().split(" ") scanner.nextLine().split scanner.nextLine() scanner.nextLine scanner " " Long k 	     = Long.parseLong(tmp[0]); k Long.parseLong(tmp[0]) Long.parseLong Long tmp[0] tmp 0 int  n 	     = Integer.parseInt(tmp[1]); n Integer.parseInt(tmp[1]) Integer.parseInt Integer tmp[1] tmp 1 String[] tp  = scanner.nextLine().split(" "); tp scanner.nextLine().split(" ") scanner.nextLine().split scanner.nextLine() scanner.nextLine scanner " " long[] list  = new long[n]; list new long[n] n for(int i = 0; i < n; i++) list[i] = Long.parseLong(tp[i]); int i = 0; int i = 0; i 0 i < n i n i++ i++ i list[i] = Long.parseLong(tp[i]); list[i] = Long.parseLong(tp[i]) list[i] list i Long.parseLong(tp[i]) Long.parseLong Long tp[i] tp i long ans = 0; ans 0 for(int j = 0; j < n; j++) {

			if(j != n - 1) ans = Math.max(ans, list[j + 1] - list[j]);
			else ans = Math.max(ans, Math.abs(list[0] +  (k- list[j])));


		} int j = 0; int j = 0; j 0 j < n j n j++ j++ j {

			if(j != n - 1) ans = Math.max(ans, list[j + 1] - list[j]);
			else ans = Math.max(ans, Math.abs(list[0] +  (k- list[j])));


		} {

			if(j != n - 1) ans = Math.max(ans, list[j + 1] - list[j]);
			else ans = Math.max(ans, Math.abs(list[0] +  (k- list[j])));


		} if(j != n - 1) ans = Math.max(ans, list[j + 1] - list[j]);
			else ans = Math.max(ans, Math.abs(list[0] +  (k- list[j]))); j != n - 1 j n - 1 n 1 ans = Math.max(ans, list[j + 1] - list[j]) ans Math.max(ans, list[j + 1] - list[j]) Math.max Math ans list[j + 1] - list[j] list[j + 1] list j + 1 j 1 list[j] list j ans = Math.max(ans, Math.abs(list[0] +  (k- list[j]))) ans Math.max(ans, Math.abs(list[0] +  (k- list[j]))) Math.max Math ans Math.abs(list[0] +  (k- list[j])) Math.abs Math list[0] +  (k- list[j]) list[0] list 0 (k- list[j]) k list[j] list j System.out.println(k - ans) System.out.println System.out System System.out k - ans k ans String[] args args public class Main{
	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		String[] tmp = scanner.nextLine().split(" ");
		Long k 	     = Long.parseLong(tmp[0]);
		int  n 	     = Integer.parseInt(tmp[1]);

		String[] tp  = scanner.nextLine().split(" ");
		long[] list  = new long[n];
		for(int i = 0; i < n; i++) list[i] = Long.parseLong(tp[i]);

		long ans = 0;

		for(int j = 0; j < n; j++) {

			if(j != n - 1) ans = Math.max(ans, list[j + 1] - list[j]);
			else ans = Math.max(ans, Math.abs(list[0] +  (k- list[j])));


		}

		System.out.println(k - ans);



	}
} public class Main{
	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		String[] tmp = scanner.nextLine().split(" ");
		Long k 	     = Long.parseLong(tmp[0]);
		int  n 	     = Integer.parseInt(tmp[1]);

		String[] tp  = scanner.nextLine().split(" ");
		long[] list  = new long[n];
		for(int i = 0; i < n; i++) list[i] = Long.parseLong(tp[i]);

		long ans = 0;

		for(int j = 0; j < n; j++) {

			if(j != n - 1) ans = Math.max(ans, list[j + 1] - list[j]);
			else ans = Math.max(ans, Math.abs(list[0] +  (k- list[j])));


		}

		System.out.println(k - ans);



	}
} Main
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		String[] tmp = scanner.nextLine().split(" ");
		Long k 	     = Long.parseLong(tmp[0]);
		int  n 	     = Integer.parseInt(tmp[1]);

		String[] tp  = scanner.nextLine().split(" ");
		long[] list  = new long[n];
		for(int i = 0; i < n; i++) list[i] = Long.parseLong(tp[i]);

		long ans = 0;

		for(int j = 0; j < n; j++) {

			if(j != n - 1) ans = Math.max(ans, list[j + 1] - list[j]);
			else ans = Math.max(ans, Math.abs(list[0] +  (k- list[j])));


		}

		System.out.println(k - ans);



	}
}