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