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