import java.util.ArrayList;
import java.util.List;
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();
      
        List<Integer> A = new ArrayList<>();
        for(int i =0; i < N; i++)
          A.add(sc.nextInt());
       
        int maxD = 0;
        
        for(int i =0; i < N; i++) {
        	int dist = 0;
        	if (i == N-1)
        		dist = K - A.get(i) + A.get(0);
        	else {
        		dist = Math.abs(A.get(i) - A.get(i + 1));
        	}
        	
        	if(dist > maxD) {
        		maxD = dist;
        	}
        	        		
        }
        
         System.out.println(K - maxD);
	}
} import java.util.ArrayList; ArrayList java.util import java.util.List; List java.util 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();
      
        List<Integer> A = new ArrayList<>();
        for(int i =0; i < N; i++)
          A.add(sc.nextInt());
       
        int maxD = 0;
        
        for(int i =0; i < N; i++) {
        	int dist = 0;
        	if (i == N-1)
        		dist = K - A.get(i) + A.get(0);
        	else {
        		dist = Math.abs(A.get(i) - A.get(i + 1));
        	}
        	
        	if(dist > maxD) {
        		maxD = dist;
        	}
        	        		
        }
        
         System.out.println(K - maxD);
	}
} Main public static void main(String[] args) {
 
		// 読み込み
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
        int N = sc.nextInt();
      
        List<Integer> A = new ArrayList<>();
        for(int i =0; i < N; i++)
          A.add(sc.nextInt());
       
        int maxD = 0;
        
        for(int i =0; i < N; i++) {
        	int dist = 0;
        	if (i == N-1)
        		dist = K - A.get(i) + A.get(0);
        	else {
        		dist = Math.abs(A.get(i) - A.get(i + 1));
        	}
        	
        	if(dist > maxD) {
        		maxD = dist;
        	}
        	        		
        }
        
         System.out.println(K - maxD);
	} main {
 
		// 読み込み
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
        int N = sc.nextInt();
      
        List<Integer> A = new ArrayList<>();
        for(int i =0; i < N; i++)
          A.add(sc.nextInt());
       
        int maxD = 0;
        
        for(int i =0; i < N; i++) {
        	int dist = 0;
        	if (i == N-1)
        		dist = K - A.get(i) + A.get(0);
        	else {
        		dist = Math.abs(A.get(i) - A.get(i + 1));
        	}
        	
        	if(dist > maxD) {
        		maxD = dist;
        	}
        	        		
        }
        
         System.out.println(K - maxD);
	} 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 List<Integer> A = new ArrayList<>(); A new ArrayList<>() for(int i =0; i < N; i++)
          A.add(sc.nextInt()); int i =0; int i =0; i 0 i < N i N i++ i++ i A.add(sc.nextInt()); A.add(sc.nextInt()) A.add A sc.nextInt() sc.nextInt sc int maxD = 0; maxD 0 for(int i =0; i < N; i++) {
        	int dist = 0;
        	if (i == N-1)
        		dist = K - A.get(i) + A.get(0);
        	else {
        		dist = Math.abs(A.get(i) - A.get(i + 1));
        	}
        	
        	if(dist > maxD) {
        		maxD = dist;
        	}
        	        		
        } int i =0; int i =0; i 0 i < N i N i++ i++ i {
        	int dist = 0;
        	if (i == N-1)
        		dist = K - A.get(i) + A.get(0);
        	else {
        		dist = Math.abs(A.get(i) - A.get(i + 1));
        	}
        	
        	if(dist > maxD) {
        		maxD = dist;
        	}
        	        		
        } {
        	int dist = 0;
        	if (i == N-1)
        		dist = K - A.get(i) + A.get(0);
        	else {
        		dist = Math.abs(A.get(i) - A.get(i + 1));
        	}
        	
        	if(dist > maxD) {
        		maxD = dist;
        	}
        	        		
        } int dist = 0; dist 0 if (i == N-1)
        		dist = K - A.get(i) + A.get(0);
        	else {
        		dist = Math.abs(A.get(i) - A.get(i + 1));
        	} i == N-1 i N-1 N 1 dist = K - A.get(i) + A.get(0) dist K - A.get(i) + A.get(0) K - A.get(i) K A.get(i) A.get A i A.get(0) A.get A 0 {
        		dist = Math.abs(A.get(i) - A.get(i + 1));
        	} dist = Math.abs(A.get(i) - A.get(i + 1)) dist Math.abs(A.get(i) - A.get(i + 1)) Math.abs Math A.get(i) - A.get(i + 1) A.get(i) A.get A i A.get(i + 1) A.get A i + 1 i 1 if(dist > maxD) {
        		maxD = dist;
        	} dist > maxD dist maxD {
        		maxD = dist;
        	} maxD = dist maxD dist System.out.println(K - maxD) System.out.println System.out System System.out K - maxD K maxD 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();
      
        List<Integer> A = new ArrayList<>();
        for(int i =0; i < N; i++)
          A.add(sc.nextInt());
       
        int maxD = 0;
        
        for(int i =0; i < N; i++) {
        	int dist = 0;
        	if (i == N-1)
        		dist = K - A.get(i) + A.get(0);
        	else {
        		dist = Math.abs(A.get(i) - A.get(i + 1));
        	}
        	
        	if(dist > maxD) {
        		maxD = dist;
        	}
        	        		
        }
        
         System.out.println(K - maxD);
	}
} public class Main {
 
	public static void main(String[] args) {
 
		// 読み込み
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
        int N = sc.nextInt();
      
        List<Integer> A = new ArrayList<>();
        for(int i =0; i < N; i++)
          A.add(sc.nextInt());
       
        int maxD = 0;
        
        for(int i =0; i < N; i++) {
        	int dist = 0;
        	if (i == N-1)
        		dist = K - A.get(i) + A.get(0);
        	else {
        		dist = Math.abs(A.get(i) - A.get(i + 1));
        	}
        	
        	if(dist > maxD) {
        		maxD = dist;
        	}
        	        		
        }
        
         System.out.println(K - maxD);
	}
} Main
import java.util.ArrayList;
import java.util.List;
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();
      
        List<Integer> A = new ArrayList<>();
        for(int i =0; i < N; i++)
          A.add(sc.nextInt());
       
        int maxD = 0;
        
        for(int i =0; i < N; i++) {
        	int dist = 0;
        	if (i == N-1)
        		dist = K - A.get(i) + A.get(0);
        	else {
        		dist = Math.abs(A.get(i) - A.get(i + 1));
        	}
        	
        	if(dist > maxD) {
        		maxD = dist;
        	}
        	        		
        }
        
         System.out.println(K - maxD);
	}
}