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(); //n軒
		
      	int[] d = new int[n]; //格家の距離代入
		for(int i = 0;i < n; i++) {
			d[i] = sc.nextInt();
		}
      
      	int maxs =0;  //一番距離が離れている二軒の距離
      	for( int i = 1 ; i < n ; i++){
			int value = d[i] - d[i-1];
			if( maxs <= value ){
				maxs = value;
				}
        }
        
      	int last = k - d[n-1] + d[0];
       	if( maxs <= last ){
				maxs = last;
				}
      
		int ans = 0;//最短移動距離
      	ans = k - maxs ;
     
		System.out.println(ans);

		sc.close();
	}
} 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(); //n軒
		
      	int[] d = new int[n]; //格家の距離代入
		for(int i = 0;i < n; i++) {
			d[i] = sc.nextInt();
		}
      
      	int maxs =0;  //一番距離が離れている二軒の距離
      	for( int i = 1 ; i < n ; i++){
			int value = d[i] - d[i-1];
			if( maxs <= value ){
				maxs = value;
				}
        }
        
      	int last = k - d[n-1] + d[0];
       	if( maxs <= last ){
				maxs = last;
				}
      
		int ans = 0;//最短移動距離
      	ans = k - maxs ;
     
		System.out.println(ans);

		sc.close();
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt(); //総距離
		int n = sc.nextInt(); //n軒
		
      	int[] d = new int[n]; //格家の距離代入
		for(int i = 0;i < n; i++) {
			d[i] = sc.nextInt();
		}
      
      	int maxs =0;  //一番距離が離れている二軒の距離
      	for( int i = 1 ; i < n ; i++){
			int value = d[i] - d[i-1];
			if( maxs <= value ){
				maxs = value;
				}
        }
        
      	int last = k - d[n-1] + d[0];
       	if( maxs <= last ){
				maxs = last;
				}
      
		int ans = 0;//最短移動距離
      	ans = k - maxs ;
     
		System.out.println(ans);

		sc.close();
	} main {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt(); //総距離
		int n = sc.nextInt(); //n軒
		
      	int[] d = new int[n]; //格家の距離代入
		for(int i = 0;i < n; i++) {
			d[i] = sc.nextInt();
		}
      
      	int maxs =0;  //一番距離が離れている二軒の距離
      	for( int i = 1 ; i < n ; i++){
			int value = d[i] - d[i-1];
			if( maxs <= value ){
				maxs = value;
				}
        }
        
      	int last = k - d[n-1] + d[0];
       	if( maxs <= last ){
				maxs = last;
				}
      
		int ans = 0;//最短移動距離
      	ans = k - maxs ;
     
		System.out.println(ans);

		sc.close();
	} 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[] d = new int[n]; d new int[n] n for(int i = 0;i < n; i++) {
			d[i] = sc.nextInt();
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			d[i] = sc.nextInt();
		} {
			d[i] = sc.nextInt();
		} d[i] = sc.nextInt() d[i] d i sc.nextInt() sc.nextInt sc int maxs =0; maxs 0 for( int i = 1 ; i < n ; i++){
			int value = d[i] - d[i-1];
			if( maxs <= value ){
				maxs = value;
				}
        } int i = 1 ; int i = 1 ; i 1 i < n i n i++ i++ i {
			int value = d[i] - d[i-1];
			if( maxs <= value ){
				maxs = value;
				}
        } {
			int value = d[i] - d[i-1];
			if( maxs <= value ){
				maxs = value;
				}
        } int value = d[i] - d[i-1]; value d[i] - d[i-1] d[i] d i d[i-1] d i-1 i 1 if( maxs <= value ){
				maxs = value;
				} maxs <= value maxs value {
				maxs = value;
				} maxs = value maxs value int last = k - d[n-1] + d[0]; last k - d[n-1] + d[0] k - d[n-1] k d[n-1] d n-1 n 1 d[0] d 0 if( maxs <= last ){
				maxs = last;
				} maxs <= last maxs last {
				maxs = last;
				} maxs = last maxs last int ans = 0; ans 0 ans = k - maxs ans k - maxs k maxs System.out.println(ans) System.out.println System.out System System.out ans sc.close() sc.close sc 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(); //n軒
		
      	int[] d = new int[n]; //格家の距離代入
		for(int i = 0;i < n; i++) {
			d[i] = sc.nextInt();
		}
      
      	int maxs =0;  //一番距離が離れている二軒の距離
      	for( int i = 1 ; i < n ; i++){
			int value = d[i] - d[i-1];
			if( maxs <= value ){
				maxs = value;
				}
        }
        
      	int last = k - d[n-1] + d[0];
       	if( maxs <= last ){
				maxs = last;
				}
      
		int ans = 0;//最短移動距離
      	ans = k - maxs ;
     
		System.out.println(ans);

		sc.close();
	}
} public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt(); //総距離
		int n = sc.nextInt(); //n軒
		
      	int[] d = new int[n]; //格家の距離代入
		for(int i = 0;i < n; i++) {
			d[i] = sc.nextInt();
		}
      
      	int maxs =0;  //一番距離が離れている二軒の距離
      	for( int i = 1 ; i < n ; i++){
			int value = d[i] - d[i-1];
			if( maxs <= value ){
				maxs = value;
				}
        }
        
      	int last = k - d[n-1] + d[0];
       	if( maxs <= last ){
				maxs = last;
				}
      
		int ans = 0;//最短移動距離
      	ans = k - maxs ;
     
		System.out.println(ans);

		sc.close();
	}
} 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(); //n軒
		
      	int[] d = new int[n]; //格家の距離代入
		for(int i = 0;i < n; i++) {
			d[i] = sc.nextInt();
		}
      
      	int maxs =0;  //一番距離が離れている二軒の距離
      	for( int i = 1 ; i < n ; i++){
			int value = d[i] - d[i-1];
			if( maxs <= value ){
				maxs = value;
				}
        }
        
      	int last = k - d[n-1] + d[0];
       	if( maxs <= last ){
				maxs = last;
				}
      
		int ans = 0;//最短移動距離
      	ans = k - maxs ;
     
		System.out.println(ans);

		sc.close();
	}
}