import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
      	int temp[];
      	temp = new int[N];
      	int dist[];
      	dist = new int[N];
      	int result = 0;
      	for (int i=0; i<N; i++){
          temp[i] = sc.nextInt();
        }
   		dist[0] = temp[0];
      	for(int i=1; i<N; i++){
          dist[i] = temp[i]-temp[i-1];
        }
      	dist[0] += (K-temp[N-1]);
      	Arrays.sort(dist);
      	// result
      	for(int i=0; i<N-1; i++){
          result += dist[i];
        }
      	System.out.println(result);
    }
} import java.util.*; util.* java class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
      	int temp[];
      	temp = new int[N];
      	int dist[];
      	dist = new int[N];
      	int result = 0;
      	for (int i=0; i<N; i++){
          temp[i] = sc.nextInt();
        }
   		dist[0] = temp[0];
      	for(int i=1; i<N; i++){
          dist[i] = temp[i]-temp[i-1];
        }
      	dist[0] += (K-temp[N-1]);
      	Arrays.sort(dist);
      	// result
      	for(int i=0; i<N-1; i++){
          result += dist[i];
        }
      	System.out.println(result);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
      	int temp[];
      	temp = new int[N];
      	int dist[];
      	dist = new int[N];
      	int result = 0;
      	for (int i=0; i<N; i++){
          temp[i] = sc.nextInt();
        }
   		dist[0] = temp[0];
      	for(int i=1; i<N; i++){
          dist[i] = temp[i]-temp[i-1];
        }
      	dist[0] += (K-temp[N-1]);
      	Arrays.sort(dist);
      	// result
      	for(int i=0; i<N-1; i++){
          result += dist[i];
        }
      	System.out.println(result);
    } main {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
      	int temp[];
      	temp = new int[N];
      	int dist[];
      	dist = new int[N];
      	int result = 0;
      	for (int i=0; i<N; i++){
          temp[i] = sc.nextInt();
        }
   		dist[0] = temp[0];
      	for(int i=1; i<N; i++){
          dist[i] = temp[i]-temp[i-1];
        }
      	dist[0] += (K-temp[N-1]);
      	Arrays.sort(dist);
      	// result
      	for(int i=0; i<N-1; i++){
          result += dist[i];
        }
      	System.out.println(result);
    } 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 temp[]; temp temp = new int[N] temp new int[N] N int dist[]; dist dist = new int[N] dist new int[N] N int result = 0; result 0 for (int i=0; i<N; i++){
          temp[i] = sc.nextInt();
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
          temp[i] = sc.nextInt();
        } {
          temp[i] = sc.nextInt();
        } temp[i] = sc.nextInt() temp[i] temp i sc.nextInt() sc.nextInt sc dist[0] = temp[0] dist[0] dist 0 temp[0] temp 0 for(int i=1; i<N; i++){
          dist[i] = temp[i]-temp[i-1];
        } int i=1; int i=1; i 1 i<N i N i++ i++ i {
          dist[i] = temp[i]-temp[i-1];
        } {
          dist[i] = temp[i]-temp[i-1];
        } dist[i] = temp[i]-temp[i-1] dist[i] dist i temp[i]-temp[i-1] temp[i] temp i temp[i-1] temp i-1 i 1 dist[0] += (K-temp[N-1]) dist[0] dist 0 (K-temp[N-1]) K temp[N-1] temp N-1 N 1 Arrays.sort(dist) Arrays.sort Arrays dist for(int i=0; i<N-1; i++){
          result += dist[i];
        } int i=0; int i=0; i 0 i<N-1 i N-1 N 1 i++ i++ i {
          result += dist[i];
        } {
          result += dist[i];
        } result += dist[i] result dist[i] dist i System.out.println(result) System.out.println System.out System System.out result String[] args args
import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
      	int temp[];
      	temp = new int[N];
      	int dist[];
      	dist = new int[N];
      	int result = 0;
      	for (int i=0; i<N; i++){
          temp[i] = sc.nextInt();
        }
   		dist[0] = temp[0];
      	for(int i=1; i<N; i++){
          dist[i] = temp[i]-temp[i-1];
        }
      	dist[0] += (K-temp[N-1]);
      	Arrays.sort(dist);
      	// result
      	for(int i=0; i<N-1; i++){
          result += dist[i];
        }
      	System.out.println(result);
    }
}