import java.util.*;
import java.lang.*;
import java.io.*;
 
public class Main {
    public static void main(String args[]) {
		 Scanner sc = new Scanner(System.in);
		 int k = sc.nextInt();
		 int n = sc.nextInt();
		 int iBefore = sc.nextInt();
		 int i0 = iBefore;
		 int max = Integer.MIN_VALUE;
		 int in = 0;
		 for (int j = 1; j<n ; j++) {
			in = sc.nextInt();
			max = Math.max(max,in - iBefore);
			iBefore = in;
		 }
		 System.out.println(k - Math.max(max,k - in + i0));
	}
} import java.util.*; util.* java import java.lang.*; lang.* java import java.io.*; io.* java public class Main {
    public static void main(String args[]) {
		 Scanner sc = new Scanner(System.in);
		 int k = sc.nextInt();
		 int n = sc.nextInt();
		 int iBefore = sc.nextInt();
		 int i0 = iBefore;
		 int max = Integer.MIN_VALUE;
		 int in = 0;
		 for (int j = 1; j<n ; j++) {
			in = sc.nextInt();
			max = Math.max(max,in - iBefore);
			iBefore = in;
		 }
		 System.out.println(k - Math.max(max,k - in + i0));
	}
} Main public static void main(String args[]) {
		 Scanner sc = new Scanner(System.in);
		 int k = sc.nextInt();
		 int n = sc.nextInt();
		 int iBefore = sc.nextInt();
		 int i0 = iBefore;
		 int max = Integer.MIN_VALUE;
		 int in = 0;
		 for (int j = 1; j<n ; j++) {
			in = sc.nextInt();
			max = Math.max(max,in - iBefore);
			iBefore = in;
		 }
		 System.out.println(k - Math.max(max,k - in + i0));
	} main {
		 Scanner sc = new Scanner(System.in);
		 int k = sc.nextInt();
		 int n = sc.nextInt();
		 int iBefore = sc.nextInt();
		 int i0 = iBefore;
		 int max = Integer.MIN_VALUE;
		 int in = 0;
		 for (int j = 1; j<n ; j++) {
			in = sc.nextInt();
			max = Math.max(max,in - iBefore);
			iBefore = in;
		 }
		 System.out.println(k - Math.max(max,k - in + i0));
	} 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 iBefore = sc.nextInt(); iBefore sc.nextInt() sc.nextInt sc int i0 = iBefore; i0 iBefore int max = Integer.MIN_VALUE; max Integer.MIN_VALUE Integer Integer.MIN_VALUE int in = 0; in 0 for (int j = 1; j<n ; j++) {
			in = sc.nextInt();
			max = Math.max(max,in - iBefore);
			iBefore = in;
		 } int j = 1; int j = 1; j 1 j<n j n j++ j++ j {
			in = sc.nextInt();
			max = Math.max(max,in - iBefore);
			iBefore = in;
		 } {
			in = sc.nextInt();
			max = Math.max(max,in - iBefore);
			iBefore = in;
		 } in = sc.nextInt() in sc.nextInt() sc.nextInt sc max = Math.max(max,in - iBefore) max Math.max(max,in - iBefore) Math.max Math max in - iBefore in iBefore iBefore = in iBefore in System.out.println(k - Math.max(max,k - in + i0)) System.out.println System.out System System.out k - Math.max(max,k - in + i0) k Math.max(max,k - in + i0) Math.max Math max k - in + i0 k - in k in i0 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 iBefore = sc.nextInt();
		 int i0 = iBefore;
		 int max = Integer.MIN_VALUE;
		 int in = 0;
		 for (int j = 1; j<n ; j++) {
			in = sc.nextInt();
			max = Math.max(max,in - iBefore);
			iBefore = in;
		 }
		 System.out.println(k - Math.max(max,k - in + i0));
	}
} public class Main {
    public static void main(String args[]) {
		 Scanner sc = new Scanner(System.in);
		 int k = sc.nextInt();
		 int n = sc.nextInt();
		 int iBefore = sc.nextInt();
		 int i0 = iBefore;
		 int max = Integer.MIN_VALUE;
		 int in = 0;
		 for (int j = 1; j<n ; j++) {
			in = sc.nextInt();
			max = Math.max(max,in - iBefore);
			iBefore = in;
		 }
		 System.out.println(k - Math.max(max,k - in + i0));
	}
} Main
import java.util.*;
import java.lang.*;
import java.io.*;
 
public class Main {
    public static void main(String args[]) {
		 Scanner sc = new Scanner(System.in);
		 int k = sc.nextInt();
		 int n = sc.nextInt();
		 int iBefore = sc.nextInt();
		 int i0 = iBefore;
		 int max = Integer.MIN_VALUE;
		 int in = 0;
		 for (int j = 1; j<n ; j++) {
			in = sc.nextInt();
			max = Math.max(max,in - iBefore);
			iBefore = in;
		 }
		 System.out.println(k - Math.max(max,k - in + i0));
	}
}