import java.util.*; 
import java.lang.*; 

public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		
		long K = scan.nextInt();
		int N = scan.nextInt();
			
		long[] A = new long[N];
		long[] P = new long[N+1];
		
		long Ans = 0;
		
		for(int i=0; i<N;i++){
			A[i] = scan.nextInt();
		}
		
		for(int i=1; i<N;i++){
			P[i-1] = A[i] -A[i-1];
		}
		
		P[N-1] = K - A[N-1] + A[0];
		
		long max = 0;
		
		for(int i=0; i<=N;i++){
			if(max<=P[i]){
				max = P[i];
			}
		}
		
		for(int i=0; i<=N;i++){
			Ans = Ans + P[i];
		}
		
		Ans = Ans - max;
			
		System.out.println(Ans);
	}
} import java.util.*; util.* java import java.lang.*; lang.* java public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		
		long K = scan.nextInt();
		int N = scan.nextInt();
			
		long[] A = new long[N];
		long[] P = new long[N+1];
		
		long Ans = 0;
		
		for(int i=0; i<N;i++){
			A[i] = scan.nextInt();
		}
		
		for(int i=1; i<N;i++){
			P[i-1] = A[i] -A[i-1];
		}
		
		P[N-1] = K - A[N-1] + A[0];
		
		long max = 0;
		
		for(int i=0; i<=N;i++){
			if(max<=P[i]){
				max = P[i];
			}
		}
		
		for(int i=0; i<=N;i++){
			Ans = Ans + P[i];
		}
		
		Ans = Ans - max;
			
		System.out.println(Ans);
	}
} Main public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		
		long K = scan.nextInt();
		int N = scan.nextInt();
			
		long[] A = new long[N];
		long[] P = new long[N+1];
		
		long Ans = 0;
		
		for(int i=0; i<N;i++){
			A[i] = scan.nextInt();
		}
		
		for(int i=1; i<N;i++){
			P[i-1] = A[i] -A[i-1];
		}
		
		P[N-1] = K - A[N-1] + A[0];
		
		long max = 0;
		
		for(int i=0; i<=N;i++){
			if(max<=P[i]){
				max = P[i];
			}
		}
		
		for(int i=0; i<=N;i++){
			Ans = Ans + P[i];
		}
		
		Ans = Ans - max;
			
		System.out.println(Ans);
	} main {
		Scanner scan = new Scanner(System.in);
		
		long K = scan.nextInt();
		int N = scan.nextInt();
			
		long[] A = new long[N];
		long[] P = new long[N+1];
		
		long Ans = 0;
		
		for(int i=0; i<N;i++){
			A[i] = scan.nextInt();
		}
		
		for(int i=1; i<N;i++){
			P[i-1] = A[i] -A[i-1];
		}
		
		P[N-1] = K - A[N-1] + A[0];
		
		long max = 0;
		
		for(int i=0; i<=N;i++){
			if(max<=P[i]){
				max = P[i];
			}
		}
		
		for(int i=0; i<=N;i++){
			Ans = Ans + P[i];
		}
		
		Ans = Ans - max;
			
		System.out.println(Ans);
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) long K = scan.nextInt(); K scan.nextInt() scan.nextInt scan int N = scan.nextInt(); N scan.nextInt() scan.nextInt scan long[] A = new long[N]; A new long[N] N long[] P = new long[N+1]; P new long[N+1] N+1 N 1 long 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=1; i<N;i++){
			P[i-1] = A[i] -A[i-1];
		} int i=1; int i=1; i 1 i<N i N i++ i++ i {
			P[i-1] = A[i] -A[i-1];
		} {
			P[i-1] = A[i] -A[i-1];
		} P[i-1] = A[i] -A[i-1] P[i-1] P i-1 i 1 A[i] -A[i-1] A[i] A i A[i-1] A i-1 i 1 P[N-1] = K - A[N-1] + A[0] P[N-1] P N-1 N 1 K - A[N-1] + A[0] K - A[N-1] K A[N-1] A N-1 N 1 A[0] A 0 long max = 0; max 0 for(int i=0; i<=N;i++){
			if(max<=P[i]){
				max = P[i];
			}
		} int i=0; int i=0; i 0 i<=N i N i++ i++ i {
			if(max<=P[i]){
				max = P[i];
			}
		} {
			if(max<=P[i]){
				max = P[i];
			}
		} if(max<=P[i]){
				max = P[i];
			} max<=P[i] max P[i] P i {
				max = P[i];
			} max = P[i] max P[i] P i for(int i=0; i<=N;i++){
			Ans = Ans + P[i];
		} int i=0; int i=0; i 0 i<=N i N i++ i++ i {
			Ans = Ans + P[i];
		} {
			Ans = Ans + P[i];
		} Ans = Ans + P[i] Ans Ans + P[i] Ans P[i] P i Ans = Ans - max Ans Ans - max Ans max 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);
		
		long K = scan.nextInt();
		int N = scan.nextInt();
			
		long[] A = new long[N];
		long[] P = new long[N+1];
		
		long Ans = 0;
		
		for(int i=0; i<N;i++){
			A[i] = scan.nextInt();
		}
		
		for(int i=1; i<N;i++){
			P[i-1] = A[i] -A[i-1];
		}
		
		P[N-1] = K - A[N-1] + A[0];
		
		long max = 0;
		
		for(int i=0; i<=N;i++){
			if(max<=P[i]){
				max = P[i];
			}
		}
		
		for(int i=0; i<=N;i++){
			Ans = Ans + P[i];
		}
		
		Ans = Ans - max;
			
		System.out.println(Ans);
	}
} public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		
		long K = scan.nextInt();
		int N = scan.nextInt();
			
		long[] A = new long[N];
		long[] P = new long[N+1];
		
		long Ans = 0;
		
		for(int i=0; i<N;i++){
			A[i] = scan.nextInt();
		}
		
		for(int i=1; i<N;i++){
			P[i-1] = A[i] -A[i-1];
		}
		
		P[N-1] = K - A[N-1] + A[0];
		
		long max = 0;
		
		for(int i=0; i<=N;i++){
			if(max<=P[i]){
				max = P[i];
			}
		}
		
		for(int i=0; i<=N;i++){
			Ans = Ans + P[i];
		}
		
		Ans = Ans - max;
			
		System.out.println(Ans);
	}
} Main
import java.util.*; 
import java.lang.*; 

public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		
		long K = scan.nextInt();
		int N = scan.nextInt();
			
		long[] A = new long[N];
		long[] P = new long[N+1];
		
		long Ans = 0;
		
		for(int i=0; i<N;i++){
			A[i] = scan.nextInt();
		}
		
		for(int i=1; i<N;i++){
			P[i-1] = A[i] -A[i-1];
		}
		
		P[N-1] = K - A[N-1] + A[0];
		
		long max = 0;
		
		for(int i=0; i<=N;i++){
			if(max<=P[i]){
				max = P[i];
			}
		}
		
		for(int i=0; i<=N;i++){
			Ans = Ans + P[i];
		}
		
		Ans = Ans - max;
			
		System.out.println(Ans);
	}
}