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 alist[] = new int[n];
    for(int i = 0; i < n; i++){
      alist[i] = sc.nextInt();
    }
    
    int distMax = 0;
    
    for(int i = 0; i < n-1; i++){
      distMax = Math.max(alist[i+1]-alist[i], distMax);
    }
    distMax = Math.max(alist[0]-(alist[n-1]-k), distMax);
    System.out.println(k - distMax);
  }
} 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 alist[] = new int[n];
    for(int i = 0; i < n; i++){
      alist[i] = sc.nextInt();
    }
    
    int distMax = 0;
    
    for(int i = 0; i < n-1; i++){
      distMax = Math.max(alist[i+1]-alist[i], distMax);
    }
    distMax = Math.max(alist[0]-(alist[n-1]-k), distMax);
    System.out.println(k - distMax);
  }
} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
	int k = sc.nextInt();
    int n = sc.nextInt();
    int alist[] = new int[n];
    for(int i = 0; i < n; i++){
      alist[i] = sc.nextInt();
    }
    
    int distMax = 0;
    
    for(int i = 0; i < n-1; i++){
      distMax = Math.max(alist[i+1]-alist[i], distMax);
    }
    distMax = Math.max(alist[0]-(alist[n-1]-k), distMax);
    System.out.println(k - distMax);
  } main {
    Scanner sc = new Scanner(System.in);
	int k = sc.nextInt();
    int n = sc.nextInt();
    int alist[] = new int[n];
    for(int i = 0; i < n; i++){
      alist[i] = sc.nextInt();
    }
    
    int distMax = 0;
    
    for(int i = 0; i < n-1; i++){
      distMax = Math.max(alist[i+1]-alist[i], distMax);
    }
    distMax = Math.max(alist[0]-(alist[n-1]-k), distMax);
    System.out.println(k - distMax);
  } 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 alist[] = new int[n]; alist new int[n] n for(int i = 0; i < n; i++){
      alist[i] = sc.nextInt();
    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
      alist[i] = sc.nextInt();
    } {
      alist[i] = sc.nextInt();
    } alist[i] = sc.nextInt() alist[i] alist i sc.nextInt() sc.nextInt sc int distMax = 0; distMax 0 for(int i = 0; i < n-1; i++){
      distMax = Math.max(alist[i+1]-alist[i], distMax);
    } int i = 0; int i = 0; i 0 i < n-1 i n-1 n 1 i++ i++ i {
      distMax = Math.max(alist[i+1]-alist[i], distMax);
    } {
      distMax = Math.max(alist[i+1]-alist[i], distMax);
    } distMax = Math.max(alist[i+1]-alist[i], distMax) distMax Math.max(alist[i+1]-alist[i], distMax) Math.max Math alist[i+1]-alist[i] alist[i+1] alist i+1 i 1 alist[i] alist i distMax distMax = Math.max(alist[0]-(alist[n-1]-k), distMax) distMax Math.max(alist[0]-(alist[n-1]-k), distMax) Math.max Math alist[0]-(alist[n-1]-k) alist[0] alist 0 (alist[n-1]-k) alist[n-1] alist n-1 n 1 k distMax System.out.println(k - distMax) System.out.println System.out System System.out k - distMax k distMax 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 alist[] = new int[n];
    for(int i = 0; i < n; i++){
      alist[i] = sc.nextInt();
    }
    
    int distMax = 0;
    
    for(int i = 0; i < n-1; i++){
      distMax = Math.max(alist[i+1]-alist[i], distMax);
    }
    distMax = Math.max(alist[0]-(alist[n-1]-k), distMax);
    System.out.println(k - distMax);
  }
} public class Main {
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
	int k = sc.nextInt();
    int n = sc.nextInt();
    int alist[] = new int[n];
    for(int i = 0; i < n; i++){
      alist[i] = sc.nextInt();
    }
    
    int distMax = 0;
    
    for(int i = 0; i < n-1; i++){
      distMax = Math.max(alist[i+1]-alist[i], distMax);
    }
    distMax = Math.max(alist[0]-(alist[n-1]-k), distMax);
    System.out.println(k - distMax);
  }
} 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 alist[] = new int[n];
    for(int i = 0; i < n; i++){
      alist[i] = sc.nextInt();
    }
    
    int distMax = 0;
    
    for(int i = 0; i < n-1; i++){
      distMax = Math.max(alist[i+1]-alist[i], distMax);
    }
    distMax = Math.max(alist[0]-(alist[n-1]-k), distMax);
    System.out.println(k - distMax);
  }
}