import java.util.*;

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

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long k = sc.nextLong();
    int n = sc.nextInt();
    long[] a = new long[n];
    for(int i = 0; i < n; i++) {
      a[i] = sc.nextLong();
    }
    long ans = k;
    for(int i = 0; i < n; i++) {
      long t = 0;
      if(i == (n - 1)) {
        t = (k - a[n - 1]) + a[0];
      } else {
        t = a[i + 1] - a[i];
      }
      ans = Math.min(ans, k - t);
    }
    System.out.println(ans);
  }
}