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[] h = new int[n];
        for(int i = 0; i<n;i++) {
        	h[i] = sc.nextInt();
        }
        int[] d = new int[n];
        for(int i=0; i<n; i++){
            if(i==0){
                d[i] = h[i] + k - h[n-1];
            }else{
                d[i] = Math.abs(h[i]-h[i-1]);
            }
        }
        int sum=0;
        int mx=0;
        for(int i=0; i<n; i++){
            mx = Math.max(mx,d[i]);
        }
        int ans;
        ans = k - mx;  //总长减去两点间距离最短的那段
        
        
        System.out.print(ans);
        
	}
} 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[] h = new int[n];
        for(int i = 0; i<n;i++) {
        	h[i] = sc.nextInt();
        }
        int[] d = new int[n];
        for(int i=0; i<n; i++){
            if(i==0){
                d[i] = h[i] + k - h[n-1];
            }else{
                d[i] = Math.abs(h[i]-h[i-1]);
            }
        }
        int sum=0;
        int mx=0;
        for(int i=0; i<n; i++){
            mx = Math.max(mx,d[i]);
        }
        int ans;
        ans = k - mx;  //总长减去两点间距离最短的那段
        
        
        System.out.print(ans);
        
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] h = new int[n];
        for(int i = 0; i<n;i++) {
        	h[i] = sc.nextInt();
        }
        int[] d = new int[n];
        for(int i=0; i<n; i++){
            if(i==0){
                d[i] = h[i] + k - h[n-1];
            }else{
                d[i] = Math.abs(h[i]-h[i-1]);
            }
        }
        int sum=0;
        int mx=0;
        for(int i=0; i<n; i++){
            mx = Math.max(mx,d[i]);
        }
        int ans;
        ans = k - mx;  //总长减去两点间距离最短的那段
        
        
        System.out.print(ans);
        
	} main {
		Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] h = new int[n];
        for(int i = 0; i<n;i++) {
        	h[i] = sc.nextInt();
        }
        int[] d = new int[n];
        for(int i=0; i<n; i++){
            if(i==0){
                d[i] = h[i] + k - h[n-1];
            }else{
                d[i] = Math.abs(h[i]-h[i-1]);
            }
        }
        int sum=0;
        int mx=0;
        for(int i=0; i<n; i++){
            mx = Math.max(mx,d[i]);
        }
        int ans;
        ans = k - mx;  //总长减去两点间距离最短的那段
        
        
        System.out.print(ans);
        
	} 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[] h = new int[n]; h new int[n] n for(int i = 0; i<n;i++) {
        	h[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i<n i n i++ i++ i {
        	h[i] = sc.nextInt();
        } {
        	h[i] = sc.nextInt();
        } h[i] = sc.nextInt() h[i] h i sc.nextInt() sc.nextInt sc int[] d = new int[n]; d new int[n] n for(int i=0; i<n; i++){
            if(i==0){
                d[i] = h[i] + k - h[n-1];
            }else{
                d[i] = Math.abs(h[i]-h[i-1]);
            }
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
            if(i==0){
                d[i] = h[i] + k - h[n-1];
            }else{
                d[i] = Math.abs(h[i]-h[i-1]);
            }
        } {
            if(i==0){
                d[i] = h[i] + k - h[n-1];
            }else{
                d[i] = Math.abs(h[i]-h[i-1]);
            }
        } if(i==0){
                d[i] = h[i] + k - h[n-1];
            }else{
                d[i] = Math.abs(h[i]-h[i-1]);
            } i==0 i 0 {
                d[i] = h[i] + k - h[n-1];
            } d[i] = h[i] + k - h[n-1] d[i] d i h[i] + k - h[n-1] h[i] + k h[i] h i k h[n-1] h n-1 n 1 {
                d[i] = Math.abs(h[i]-h[i-1]);
            } d[i] = Math.abs(h[i]-h[i-1]) d[i] d i Math.abs(h[i]-h[i-1]) Math.abs Math h[i]-h[i-1] h[i] h i h[i-1] h i-1 i 1 int sum=0; sum 0 int mx=0; mx 0 for(int i=0; i<n; i++){
            mx = Math.max(mx,d[i]);
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
            mx = Math.max(mx,d[i]);
        } {
            mx = Math.max(mx,d[i]);
        } mx = Math.max(mx,d[i]) mx Math.max(mx,d[i]) Math.max Math mx d[i] d i int ans; ans ans = k - mx ans k - mx k mx System.out.print(ans) System.out.print System.out System System.out ans 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[] h = new int[n];
        for(int i = 0; i<n;i++) {
        	h[i] = sc.nextInt();
        }
        int[] d = new int[n];
        for(int i=0; i<n; i++){
            if(i==0){
                d[i] = h[i] + k - h[n-1];
            }else{
                d[i] = Math.abs(h[i]-h[i-1]);
            }
        }
        int sum=0;
        int mx=0;
        for(int i=0; i<n; i++){
            mx = Math.max(mx,d[i]);
        }
        int ans;
        ans = k - mx;  //总长减去两点间距离最短的那段
        
        
        System.out.print(ans);
        
	}
} public class Main {
	

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] h = new int[n];
        for(int i = 0; i<n;i++) {
        	h[i] = sc.nextInt();
        }
        int[] d = new int[n];
        for(int i=0; i<n; i++){
            if(i==0){
                d[i] = h[i] + k - h[n-1];
            }else{
                d[i] = Math.abs(h[i]-h[i-1]);
            }
        }
        int sum=0;
        int mx=0;
        for(int i=0; i<n; i++){
            mx = Math.max(mx,d[i]);
        }
        int ans;
        ans = k - mx;  //总长减去两点间距离最短的那段
        
        
        System.out.print(ans);
        
	}
} Main
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[] h = new int[n];
        for(int i = 0; i<n;i++) {
        	h[i] = sc.nextInt();
        }
        int[] d = new int[n];
        for(int i=0; i<n; i++){
            if(i==0){
                d[i] = h[i] + k - h[n-1];
            }else{
                d[i] = Math.abs(h[i]-h[i-1]);
            }
        }
        int sum=0;
        int mx=0;
        for(int i=0; i<n; i++){
            mx = Math.max(mx,d[i]);
        }
        int ans;
        ans = k - mx;  //总长减去两点间距离最短的那段
        
        
        System.out.print(ans);
        
	}
}