import java.util.*;
import java.util.stream.Collectors;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int K = sc.nextInt();
    int max = 0;
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(sc.nextInt());
    for (int i = 1; i < K; i++) {
      list.add(sc.nextInt());
      max = Math.max(max, list.get(i) - list.get(i - 1));
    }
    int last = (N - list.get(list.size() - 1)) + list.get(0);
    int ans = N - Math.max(max, last);
    System.out.println(ans);

  }
} import java.util.*; util.* java import java.util.stream.Collectors; Collectors java.util.stream public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int K = sc.nextInt();
    int max = 0;
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(sc.nextInt());
    for (int i = 1; i < K; i++) {
      list.add(sc.nextInt());
      max = Math.max(max, list.get(i) - list.get(i - 1));
    }
    int last = (N - list.get(list.size() - 1)) + list.get(0);
    int ans = N - Math.max(max, last);
    System.out.println(ans);

  }
} Main public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int K = sc.nextInt();
    int max = 0;
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(sc.nextInt());
    for (int i = 1; i < K; i++) {
      list.add(sc.nextInt());
      max = Math.max(max, list.get(i) - list.get(i - 1));
    }
    int last = (N - list.get(list.size() - 1)) + list.get(0);
    int ans = N - Math.max(max, last);
    System.out.println(ans);

  } main {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int K = sc.nextInt();
    int max = 0;
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(sc.nextInt());
    for (int i = 1; i < K; i++) {
      list.add(sc.nextInt());
      max = Math.max(max, list.get(i) - list.get(i - 1));
    }
    int last = (N - list.get(list.size() - 1)) + list.get(0);
    int ans = N - Math.max(max, last);
    System.out.println(ans);

  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc int max = 0; max 0 ArrayList<Integer> list = new ArrayList<Integer>(); list new ArrayList<Integer>() list.add(sc.nextInt()) list.add list sc.nextInt() sc.nextInt sc for (int i = 1; i < K; i++) {
      list.add(sc.nextInt());
      max = Math.max(max, list.get(i) - list.get(i - 1));
    } int i = 1; int i = 1; i 1 i < K i K i++ i++ i {
      list.add(sc.nextInt());
      max = Math.max(max, list.get(i) - list.get(i - 1));
    } {
      list.add(sc.nextInt());
      max = Math.max(max, list.get(i) - list.get(i - 1));
    } list.add(sc.nextInt()) list.add list sc.nextInt() sc.nextInt sc max = Math.max(max, list.get(i) - list.get(i - 1)) max Math.max(max, list.get(i) - list.get(i - 1)) Math.max Math max list.get(i) - list.get(i - 1) list.get(i) list.get list i list.get(i - 1) list.get list i - 1 i 1 int last = (N - list.get(list.size() - 1)) + list.get(0); last (N - list.get(list.size() - 1)) + list.get(0) (N - list.get(list.size() - 1)) N list.get(list.size() - 1) list.get list list.size() - 1 list.size() list.size list 1 list.get(0) list.get list 0 int ans = N - Math.max(max, last); ans N - Math.max(max, last) N Math.max(max, last) Math.max Math max last 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 N = sc.nextInt();
    int K = sc.nextInt();
    int max = 0;
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(sc.nextInt());
    for (int i = 1; i < K; i++) {
      list.add(sc.nextInt());
      max = Math.max(max, list.get(i) - list.get(i - 1));
    }
    int last = (N - list.get(list.size() - 1)) + list.get(0);
    int ans = N - Math.max(max, last);
    System.out.println(ans);

  }
} public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int K = sc.nextInt();
    int max = 0;
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(sc.nextInt());
    for (int i = 1; i < K; i++) {
      list.add(sc.nextInt());
      max = Math.max(max, list.get(i) - list.get(i - 1));
    }
    int last = (N - list.get(list.size() - 1)) + list.get(0);
    int ans = N - Math.max(max, last);
    System.out.println(ans);

  }
} Main
import java.util.*;
import java.util.stream.Collectors;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int K = sc.nextInt();
    int max = 0;
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(sc.nextInt());
    for (int i = 1; i < K; i++) {
      list.add(sc.nextInt());
      max = Math.max(max, list.get(i) - list.get(i - 1));
    }
    int last = (N - list.get(list.size() - 1)) + list.get(0);
    int ans = N - Math.max(max, last);
    System.out.println(ans);

  }
}
