import java.util.*;

class Main {
  public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
    
    long K = sc.nextLong();//next(文字列), nextDouble, nextLine(1行)とか
    int N = sc.nextInt();
    long[] S = new long[N];
    for (int i = 0; i < N;i++){
      S[i] = sc.nextLong();
    }
    long sum = 0;
    long max =0;
    for (int i = 0; i<N-1;i++){
      sum += S[i+1] - S[i];
      max = Math.max(max,(S[i+1]-S[i]));
    }
    sum += K-S[N-1]+S[0];
    max = Math.max(max,K-S[N-1]+S[0]);
    sum -= max;
    System.out.println(sum);      
  }    
}
 import java.util.*; util.* java class Main {
  public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
    
    long K = sc.nextLong();//next(文字列), nextDouble, nextLine(1行)とか
    int N = sc.nextInt();
    long[] S = new long[N];
    for (int i = 0; i < N;i++){
      S[i] = sc.nextLong();
    }
    long sum = 0;
    long max =0;
    for (int i = 0; i<N-1;i++){
      sum += S[i+1] - S[i];
      max = Math.max(max,(S[i+1]-S[i]));
    }
    sum += K-S[N-1]+S[0];
    max = Math.max(max,K-S[N-1]+S[0]);
    sum -= max;
    System.out.println(sum);      
  }    
} Main public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
    
    long K = sc.nextLong();//next(文字列), nextDouble, nextLine(1行)とか
    int N = sc.nextInt();
    long[] S = new long[N];
    for (int i = 0; i < N;i++){
      S[i] = sc.nextLong();
    }
    long sum = 0;
    long max =0;
    for (int i = 0; i<N-1;i++){
      sum += S[i+1] - S[i];
      max = Math.max(max,(S[i+1]-S[i]));
    }
    sum += K-S[N-1]+S[0];
    max = Math.max(max,K-S[N-1]+S[0]);
    sum -= max;
    System.out.println(sum);      
  } main {
	Scanner sc = new Scanner(System.in);
    
    long K = sc.nextLong();//next(文字列), nextDouble, nextLine(1行)とか
    int N = sc.nextInt();
    long[] S = new long[N];
    for (int i = 0; i < N;i++){
      S[i] = sc.nextLong();
    }
    long sum = 0;
    long max =0;
    for (int i = 0; i<N-1;i++){
      sum += S[i+1] - S[i];
      max = Math.max(max,(S[i+1]-S[i]));
    }
    sum += K-S[N-1]+S[0];
    max = Math.max(max,K-S[N-1]+S[0]);
    sum -= max;
    System.out.println(sum);      
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long K = sc.nextLong(); K sc.nextLong() sc.nextLong sc int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc long[] S = new long[N]; S new long[N] N for (int i = 0; i < N;i++){
      S[i] = sc.nextLong();
    } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
      S[i] = sc.nextLong();
    } {
      S[i] = sc.nextLong();
    } S[i] = sc.nextLong() S[i] S i sc.nextLong() sc.nextLong sc long sum = 0; sum 0 long max =0; max 0 for (int i = 0; i<N-1;i++){
      sum += S[i+1] - S[i];
      max = Math.max(max,(S[i+1]-S[i]));
    } int i = 0; int i = 0; i 0 i<N-1 i N-1 N 1 i++ i++ i {
      sum += S[i+1] - S[i];
      max = Math.max(max,(S[i+1]-S[i]));
    } {
      sum += S[i+1] - S[i];
      max = Math.max(max,(S[i+1]-S[i]));
    } sum += S[i+1] - S[i] sum S[i+1] - S[i] S[i+1] S i+1 i 1 S[i] S i max = Math.max(max,(S[i+1]-S[i])) max Math.max(max,(S[i+1]-S[i])) Math.max Math max (S[i+1]-S[i]) S[i+1] S i+1 i 1 S[i] S i sum += K-S[N-1]+S[0] sum K-S[N-1]+S[0] K-S[N-1] K S[N-1] S N-1 N 1 S[0] S 0 max = Math.max(max,K-S[N-1]+S[0]) max Math.max(max,K-S[N-1]+S[0]) Math.max Math max K-S[N-1]+S[0] K-S[N-1] K S[N-1] S N-1 N 1 S[0] S 0 sum -= max sum max System.out.println(sum) System.out.println System.out System System.out sum String[] args args
import java.util.*;

class Main {
  public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
    
    long K = sc.nextLong();//next(文字列), nextDouble, nextLine(1行)とか
    int N = sc.nextInt();
    long[] S = new long[N];
    for (int i = 0; i < N;i++){
      S[i] = sc.nextLong();
    }
    long sum = 0;
    long max =0;
    for (int i = 0; i<N-1;i++){
      sum += S[i+1] - S[i];
      max = Math.max(max,(S[i+1]-S[i]));
    }
    sum += K-S[N-1]+S[0];
    max = Math.max(max,K-S[N-1]+S[0]);
    sum -= max;
    System.out.println(sum);      
  }    
}

