
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[] house = new int[n];
		for(int i=0;i<n;i++){
			house[i]=sc.nextInt();
		}
		int maxDist = k-(house[n-1]-house[0]);
		for(int i=0;i<n-1;i++){
			if(house[i+1]-house[i]>maxDist){
				maxDist = house[i+1]-house[i];
			}
		}
		System.out.println(k-maxDist);
	}
} 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[] house = new int[n];
		for(int i=0;i<n;i++){
			house[i]=sc.nextInt();
		}
		int maxDist = k-(house[n-1]-house[0]);
		for(int i=0;i<n-1;i++){
			if(house[i+1]-house[i]>maxDist){
				maxDist = house[i+1]-house[i];
			}
		}
		System.out.println(k-maxDist);
	}
} Main public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] house = new int[n];
		for(int i=0;i<n;i++){
			house[i]=sc.nextInt();
		}
		int maxDist = k-(house[n-1]-house[0]);
		for(int i=0;i<n-1;i++){
			if(house[i+1]-house[i]>maxDist){
				maxDist = house[i+1]-house[i];
			}
		}
		System.out.println(k-maxDist);
	} main {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] house = new int[n];
		for(int i=0;i<n;i++){
			house[i]=sc.nextInt();
		}
		int maxDist = k-(house[n-1]-house[0]);
		for(int i=0;i<n-1;i++){
			if(house[i+1]-house[i]>maxDist){
				maxDist = house[i+1]-house[i];
			}
		}
		System.out.println(k-maxDist);
	} 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[] house = new int[n]; house new int[n] n for(int i=0;i<n;i++){
			house[i]=sc.nextInt();
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			house[i]=sc.nextInt();
		} {
			house[i]=sc.nextInt();
		} house[i]=sc.nextInt() house[i] house i sc.nextInt() sc.nextInt sc int maxDist = k-(house[n-1]-house[0]); maxDist k-(house[n-1]-house[0]) k (house[n-1]-house[0]) house[n-1] house n-1 n 1 house[0] house 0 for(int i=0;i<n-1;i++){
			if(house[i+1]-house[i]>maxDist){
				maxDist = house[i+1]-house[i];
			}
		} int i=0; int i=0; i 0 i<n-1 i n-1 n 1 i++ i++ i {
			if(house[i+1]-house[i]>maxDist){
				maxDist = house[i+1]-house[i];
			}
		} {
			if(house[i+1]-house[i]>maxDist){
				maxDist = house[i+1]-house[i];
			}
		} if(house[i+1]-house[i]>maxDist){
				maxDist = house[i+1]-house[i];
			} house[i+1]-house[i]>maxDist house[i+1]-house[i] house[i+1] house i+1 i 1 house[i] house i maxDist {
				maxDist = house[i+1]-house[i];
			} maxDist = house[i+1]-house[i] maxDist house[i+1]-house[i] house[i+1] house i+1 i 1 house[i] house i System.out.println(k-maxDist) System.out.println System.out System System.out k-maxDist k maxDist 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[] house = new int[n];
		for(int i=0;i<n;i++){
			house[i]=sc.nextInt();
		}
		int maxDist = k-(house[n-1]-house[0]);
		for(int i=0;i<n-1;i++){
			if(house[i+1]-house[i]>maxDist){
				maxDist = house[i+1]-house[i];
			}
		}
		System.out.println(k-maxDist);
	}
} public class Main {

	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int[] house = new int[n];
		for(int i=0;i<n;i++){
			house[i]=sc.nextInt();
		}
		int maxDist = k-(house[n-1]-house[0]);
		for(int i=0;i<n-1;i++){
			if(house[i+1]-house[i]>maxDist){
				maxDist = house[i+1]-house[i];
			}
		}
		System.out.println(k-maxDist);
	}
} 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[] house = new int[n];
		for(int i=0;i<n;i++){
			house[i]=sc.nextInt();
		}
		int maxDist = k-(house[n-1]-house[0]);
		for(int i=0;i<n-1;i++){
			if(house[i+1]-house[i]>maxDist){
				maxDist = house[i+1]-house[i];
			}
		}
		System.out.println(k-maxDist);
	}
}
