import java.util.Scanner;
class Main {
  public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    int k = scanner.nextInt();
    int n = scanner.nextInt();
    int[] a = new int[n];
    for(int i = 0;i < n;i++){
      a[i] = scanner.nextInt();
    }
    int difference = 0;
    for(int i = 0;i < n - 1;i++){
      if(a[i+1] - a[i] > difference)difference = a[i+1] - a[i];
    }
    if(a[0] - a[n-1] + k > difference)difference = a[0] - a[n-1] + k;
    System.out.println(k - difference);
  }
} import java.util.Scanner; Scanner java.util class Main {
  public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    int k = scanner.nextInt();
    int n = scanner.nextInt();
    int[] a = new int[n];
    for(int i = 0;i < n;i++){
      a[i] = scanner.nextInt();
    }
    int difference = 0;
    for(int i = 0;i < n - 1;i++){
      if(a[i+1] - a[i] > difference)difference = a[i+1] - a[i];
    }
    if(a[0] - a[n-1] + k > difference)difference = a[0] - a[n-1] + k;
    System.out.println(k - difference);
  }
} Main public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    int k = scanner.nextInt();
    int n = scanner.nextInt();
    int[] a = new int[n];
    for(int i = 0;i < n;i++){
      a[i] = scanner.nextInt();
    }
    int difference = 0;
    for(int i = 0;i < n - 1;i++){
      if(a[i+1] - a[i] > difference)difference = a[i+1] - a[i];
    }
    if(a[0] - a[n-1] + k > difference)difference = a[0] - a[n-1] + k;
    System.out.println(k - difference);
  } main {
    Scanner scanner = new Scanner(System.in);
    int k = scanner.nextInt();
    int n = scanner.nextInt();
    int[] a = new int[n];
    for(int i = 0;i < n;i++){
      a[i] = scanner.nextInt();
    }
    int difference = 0;
    for(int i = 0;i < n - 1;i++){
      if(a[i+1] - a[i] > difference)difference = a[i+1] - a[i];
    }
    if(a[0] - a[n-1] + k > difference)difference = a[0] - a[n-1] + k;
    System.out.println(k - difference);
  } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int k = scanner.nextInt(); k scanner.nextInt() scanner.nextInt scanner int n = scanner.nextInt(); n scanner.nextInt() scanner.nextInt scanner int[] a = new int[n]; a new int[n] n for(int i = 0;i < n;i++){
      a[i] = scanner.nextInt();
    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
      a[i] = scanner.nextInt();
    } {
      a[i] = scanner.nextInt();
    } a[i] = scanner.nextInt() a[i] a i scanner.nextInt() scanner.nextInt scanner int difference = 0; difference 0 for(int i = 0;i < n - 1;i++){
      if(a[i+1] - a[i] > difference)difference = a[i+1] - a[i];
    } int i = 0; int i = 0; i 0 i < n - 1 i n - 1 n 1 i++ i++ i {
      if(a[i+1] - a[i] > difference)difference = a[i+1] - a[i];
    } {
      if(a[i+1] - a[i] > difference)difference = a[i+1] - a[i];
    } if(a[i+1] - a[i] > difference)difference = a[i+1] - a[i]; a[i+1] - a[i] > difference a[i+1] - a[i] a[i+1] a i+1 i 1 a[i] a i difference difference = a[i+1] - a[i] difference a[i+1] - a[i] a[i+1] a i+1 i 1 a[i] a i if(a[0] - a[n-1] + k > difference)difference = a[0] - a[n-1] + k; a[0] - a[n-1] + k > difference a[0] - a[n-1] + k a[0] - a[n-1] a[0] a 0 a[n-1] a n-1 n 1 k difference difference = a[0] - a[n-1] + k difference a[0] - a[n-1] + k a[0] - a[n-1] a[0] a 0 a[n-1] a n-1 n 1 k System.out.println(k - difference) System.out.println System.out System System.out k - difference k difference String[] args args
import java.util.Scanner;
class Main {
  public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    int k = scanner.nextInt();
    int n = scanner.nextInt();
    int[] a = new int[n];
    for(int i = 0;i < n;i++){
      a[i] = scanner.nextInt();
    }
    int difference = 0;
    for(int i = 0;i < n - 1;i++){
      if(a[i+1] - a[i] > difference)difference = a[i+1] - a[i];
    }
    if(a[0] - a[n-1] + k > difference)difference = a[0] - a[n-1] + k;
    System.out.println(k - difference);
  }
}