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