import java.util.Arrays;
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];
		for(int i =0;i<N;i++) {
			A[i]=scan.nextInt();
		}

		int[] L = new int[N];

		for(int i =0;i<N-1;i++) {
			L[i]=A[i+1]-A[i];
//			System.out.println("i:"+i);
//			System.out.println("A[i+1]:"+A[i+1]);
//			System.out.println("A[i]:"+A[i]);
		}
		L[N-1]=K-A[N-1]+A[0];

//		for(int i =0;i<N;i++) {
//			System.out.println(L[i]);
//		}

		Arrays.sort(L);

		int sum = 0;

		for(int i =0;i<N-1;i++) {
			sum += L[i];
		}

		System.out.println(sum);
	}
} import java.util.Arrays; Arrays java.util 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];
		for(int i =0;i<N;i++) {
			A[i]=scan.nextInt();
		}

		int[] L = new int[N];

		for(int i =0;i<N-1;i++) {
			L[i]=A[i+1]-A[i];
//			System.out.println("i:"+i);
//			System.out.println("A[i+1]:"+A[i+1]);
//			System.out.println("A[i]:"+A[i]);
		}
		L[N-1]=K-A[N-1]+A[0];

//		for(int i =0;i<N;i++) {
//			System.out.println(L[i]);
//		}

		Arrays.sort(L);

		int sum = 0;

		for(int i =0;i<N-1;i++) {
			sum += L[i];
		}

		System.out.println(sum);
	}
} 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];
		for(int i =0;i<N;i++) {
			A[i]=scan.nextInt();
		}

		int[] L = new int[N];

		for(int i =0;i<N-1;i++) {
			L[i]=A[i+1]-A[i];
//			System.out.println("i:"+i);
//			System.out.println("A[i+1]:"+A[i+1]);
//			System.out.println("A[i]:"+A[i]);
		}
		L[N-1]=K-A[N-1]+A[0];

//		for(int i =0;i<N;i++) {
//			System.out.println(L[i]);
//		}

		Arrays.sort(L);

		int sum = 0;

		for(int i =0;i<N-1;i++) {
			sum += L[i];
		}

		System.out.println(sum);
	} main {
		Scanner scan = new Scanner(System.in);
		int K = scan.nextInt();
		int N = scan.nextInt();
		int[] A = new int[N];
		for(int i =0;i<N;i++) {
			A[i]=scan.nextInt();
		}

		int[] L = new int[N];

		for(int i =0;i<N-1;i++) {
			L[i]=A[i+1]-A[i];
//			System.out.println("i:"+i);
//			System.out.println("A[i+1]:"+A[i+1]);
//			System.out.println("A[i]:"+A[i]);
		}
		L[N-1]=K-A[N-1]+A[0];

//		for(int i =0;i<N;i++) {
//			System.out.println(L[i]);
//		}

		Arrays.sort(L);

		int sum = 0;

		for(int i =0;i<N-1;i++) {
			sum += L[i];
		}

		System.out.println(sum);
	} 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 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 int[] L = new int[N]; L new int[N] N for(int i =0;i<N-1;i++) {
			L[i]=A[i+1]-A[i];
//			System.out.println("i:"+i);
//			System.out.println("A[i+1]:"+A[i+1]);
//			System.out.println("A[i]:"+A[i]);
		} int i =0; int i =0; i 0 i<N-1 i N-1 N 1 i++ i++ i {
			L[i]=A[i+1]-A[i];
//			System.out.println("i:"+i);
//			System.out.println("A[i+1]:"+A[i+1]);
//			System.out.println("A[i]:"+A[i]);
		} {
			L[i]=A[i+1]-A[i];
//			System.out.println("i:"+i);
//			System.out.println("A[i+1]:"+A[i+1]);
//			System.out.println("A[i]:"+A[i]);
		} L[i]=A[i+1]-A[i] L[i] L i A[i+1]-A[i] A[i+1] A i+1 i 1 A[i] A i L[N-1]=K-A[N-1]+A[0] L[N-1] L 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 Arrays.sort(L) Arrays.sort Arrays L int sum = 0; sum 0 for(int i =0;i<N-1;i++) {
			sum += L[i];
		} int i =0; int i =0; i 0 i<N-1 i N-1 N 1 i++ i++ i {
			sum += L[i];
		} {
			sum += L[i];
		} sum += L[i] sum L[i] L i System.out.println(sum) System.out.println System.out System System.out sum 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];
		for(int i =0;i<N;i++) {
			A[i]=scan.nextInt();
		}

		int[] L = new int[N];

		for(int i =0;i<N-1;i++) {
			L[i]=A[i+1]-A[i];
//			System.out.println("i:"+i);
//			System.out.println("A[i+1]:"+A[i+1]);
//			System.out.println("A[i]:"+A[i]);
		}
		L[N-1]=K-A[N-1]+A[0];

//		for(int i =0;i<N;i++) {
//			System.out.println(L[i]);
//		}

		Arrays.sort(L);

		int sum = 0;

		for(int i =0;i<N-1;i++) {
			sum += L[i];
		}

		System.out.println(sum);
	}
} 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];
		for(int i =0;i<N;i++) {
			A[i]=scan.nextInt();
		}

		int[] L = new int[N];

		for(int i =0;i<N-1;i++) {
			L[i]=A[i+1]-A[i];
//			System.out.println("i:"+i);
//			System.out.println("A[i+1]:"+A[i+1]);
//			System.out.println("A[i]:"+A[i]);
		}
		L[N-1]=K-A[N-1]+A[0];

//		for(int i =0;i<N;i++) {
//			System.out.println(L[i]);
//		}

		Arrays.sort(L);

		int sum = 0;

		for(int i =0;i<N-1;i++) {
			sum += L[i];
		}

		System.out.println(sum);
	}
} Main
import java.util.Arrays;
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];
		for(int i =0;i<N;i++) {
			A[i]=scan.nextInt();
		}

		int[] L = new int[N];

		for(int i =0;i<N-1;i++) {
			L[i]=A[i+1]-A[i];
//			System.out.println("i:"+i);
//			System.out.println("A[i+1]:"+A[i+1]);
//			System.out.println("A[i]:"+A[i]);
		}
		L[N-1]=K-A[N-1]+A[0];

//		for(int i =0;i<N;i++) {
//			System.out.println(L[i]);
//		}

		Arrays.sort(L);

		int sum = 0;

		for(int i =0;i<N-1;i++) {
			sum += L[i];
		}

		System.out.println(sum);
	}
}