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