import java.util.*;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		int k = scan.nextInt();
		int n = scan.nextInt();
		int a[] = new int[n];
		int dis[] = new int[n];
		int ans = 0;
		for(int i=0;i<n;i++) {
			a[i] = scan.nextInt();
		}
		
		for(int i=0;i<n;i++) {
			if(i!=n-1) {
				dis[i] = a[i+1]-a[i];
			}else {
				dis[i] = a[0]+k-a[i];
			}
		}
		
		Arrays.sort(dis);
		
		for(int i=0;i<n-1;i++) {
			ans += dis[i];
		}
		
		System.out.println(ans);
	}	
	
	
	//最小公約数
	/*
	static int gcd (int a, int b) {
		int temp;
		while((temp = a%b)!=0) {
			a = b;
			b = temp;
		}
		return b;
	}
	*/
	
	//入れ替え
	/*
	static int swap (int a, int b) {
		int swap_arr[] = new int[2];
		
		swap[0] = b;
		swap[1] = a;
		
		return swap_arr;
	}
	*/
	
} import java.util.*; util.* java import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		int k = scan.nextInt();
		int n = scan.nextInt();
		int a[] = new int[n];
		int dis[] = new int[n];
		int ans = 0;
		for(int i=0;i<n;i++) {
			a[i] = scan.nextInt();
		}
		
		for(int i=0;i<n;i++) {
			if(i!=n-1) {
				dis[i] = a[i+1]-a[i];
			}else {
				dis[i] = a[0]+k-a[i];
			}
		}
		
		Arrays.sort(dis);
		
		for(int i=0;i<n-1;i++) {
			ans += dis[i];
		}
		
		System.out.println(ans);
	}	
	
	
	//最小公約数
	/*
	static int gcd (int a, int b) {
		int temp;
		while((temp = a%b)!=0) {
			a = b;
			b = temp;
		}
		return b;
	}
	*/
	
	//入れ替え
	/*
	static int swap (int a, int b) {
		int swap_arr[] = new int[2];
		
		swap[0] = b;
		swap[1] = a;
		
		return swap_arr;
	}
	*/
	
} Main public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		int k = scan.nextInt();
		int n = scan.nextInt();
		int a[] = new int[n];
		int dis[] = new int[n];
		int ans = 0;
		for(int i=0;i<n;i++) {
			a[i] = scan.nextInt();
		}
		
		for(int i=0;i<n;i++) {
			if(i!=n-1) {
				dis[i] = a[i+1]-a[i];
			}else {
				dis[i] = a[0]+k-a[i];
			}
		}
		
		Arrays.sort(dis);
		
		for(int i=0;i<n-1;i++) {
			ans += dis[i];
		}
		
		System.out.println(ans);
	} main {
		Scanner scan = new Scanner(System.in);
		
		int k = scan.nextInt();
		int n = scan.nextInt();
		int a[] = new int[n];
		int dis[] = new int[n];
		int ans = 0;
		for(int i=0;i<n;i++) {
			a[i] = scan.nextInt();
		}
		
		for(int i=0;i<n;i++) {
			if(i!=n-1) {
				dis[i] = a[i+1]-a[i];
			}else {
				dis[i] = a[0]+k-a[i];
			}
		}
		
		Arrays.sort(dis);
		
		for(int i=0;i<n-1;i++) {
			ans += dis[i];
		}
		
		System.out.println(ans);
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan int n = scan.nextInt(); n scan.nextInt() scan.nextInt scan int a[] = new int[n]; a new int[n] n int dis[] = new int[n]; dis new int[n] n int ans = 0; ans 0 for(int i=0;i<n;i++) {
			a[i] = scan.nextInt();
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			a[i] = scan.nextInt();
		} {
			a[i] = scan.nextInt();
		} a[i] = scan.nextInt() a[i] a i scan.nextInt() scan.nextInt scan for(int i=0;i<n;i++) {
			if(i!=n-1) {
				dis[i] = a[i+1]-a[i];
			}else {
				dis[i] = a[0]+k-a[i];
			}
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			if(i!=n-1) {
				dis[i] = a[i+1]-a[i];
			}else {
				dis[i] = a[0]+k-a[i];
			}
		} {
			if(i!=n-1) {
				dis[i] = a[i+1]-a[i];
			}else {
				dis[i] = a[0]+k-a[i];
			}
		} if(i!=n-1) {
				dis[i] = a[i+1]-a[i];
			}else {
				dis[i] = a[0]+k-a[i];
			} i!=n-1 i n-1 n 1 {
				dis[i] = a[i+1]-a[i];
			} dis[i] = a[i+1]-a[i] dis[i] dis i a[i+1]-a[i] a[i+1] a i+1 i 1 a[i] a i {
				dis[i] = a[0]+k-a[i];
			} dis[i] = a[0]+k-a[i] dis[i] dis i a[0]+k-a[i] a[0]+k a[0] a 0 k a[i] a i Arrays.sort(dis) Arrays.sort Arrays dis for(int i=0;i<n-1;i++) {
			ans += dis[i];
		} int i=0; int i=0; i 0 i<n-1 i n-1 n 1 i++ i++ i {
			ans += dis[i];
		} {
			ans += dis[i];
		} ans += dis[i] ans dis[i] dis i System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		int k = scan.nextInt();
		int n = scan.nextInt();
		int a[] = new int[n];
		int dis[] = new int[n];
		int ans = 0;
		for(int i=0;i<n;i++) {
			a[i] = scan.nextInt();
		}
		
		for(int i=0;i<n;i++) {
			if(i!=n-1) {
				dis[i] = a[i+1]-a[i];
			}else {
				dis[i] = a[0]+k-a[i];
			}
		}
		
		Arrays.sort(dis);
		
		for(int i=0;i<n-1;i++) {
			ans += dis[i];
		}
		
		System.out.println(ans);
	}	
	
	
	//最小公約数
	/*
	static int gcd (int a, int b) {
		int temp;
		while((temp = a%b)!=0) {
			a = b;
			b = temp;
		}
		return b;
	}
	*/
	
	//入れ替え
	/*
	static int swap (int a, int b) {
		int swap_arr[] = new int[2];
		
		swap[0] = b;
		swap[1] = a;
		
		return swap_arr;
	}
	*/
	
} public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		int k = scan.nextInt();
		int n = scan.nextInt();
		int a[] = new int[n];
		int dis[] = new int[n];
		int ans = 0;
		for(int i=0;i<n;i++) {
			a[i] = scan.nextInt();
		}
		
		for(int i=0;i<n;i++) {
			if(i!=n-1) {
				dis[i] = a[i+1]-a[i];
			}else {
				dis[i] = a[0]+k-a[i];
			}
		}
		
		Arrays.sort(dis);
		
		for(int i=0;i<n-1;i++) {
			ans += dis[i];
		}
		
		System.out.println(ans);
	}	
	
	
	//最小公約数
	/*
	static int gcd (int a, int b) {
		int temp;
		while((temp = a%b)!=0) {
			a = b;
			b = temp;
		}
		return b;
	}
	*/
	
	//入れ替え
	/*
	static int swap (int a, int b) {
		int swap_arr[] = new int[2];
		
		swap[0] = b;
		swap[1] = a;
		
		return swap_arr;
	}
	*/
	
} Main
import java.util.*;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		int k = scan.nextInt();
		int n = scan.nextInt();
		int a[] = new int[n];
		int dis[] = new int[n];
		int ans = 0;
		for(int i=0;i<n;i++) {
			a[i] = scan.nextInt();
		}
		
		for(int i=0;i<n;i++) {
			if(i!=n-1) {
				dis[i] = a[i+1]-a[i];
			}else {
				dis[i] = a[0]+k-a[i];
			}
		}
		
		Arrays.sort(dis);
		
		for(int i=0;i<n-1;i++) {
			ans += dis[i];
		}
		
		System.out.println(ans);
	}	
	
	
	//最小公約数
	/*
	static int gcd (int a, int b) {
		int temp;
		while((temp = a%b)!=0) {
			a = b;
			b = temp;
		}
		return b;
	}
	*/
	
	//入れ替え
	/*
	static int swap (int a, int b) {
		int swap_arr[] = new int[2];
		
		swap[0] = b;
		swap[1] = a;
		
		return swap_arr;
	}
	*/
	
}
