import java.util.*;

class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    String str_kn = scan.nextLine();
    String str_a = scan.nextLine();
    String[] kn = str_kn.split(" ");
    int k = Integer.parseInt(kn[0]);
    int n = Integer.parseInt(kn[1]);
    String[] str_a_array = str_a.split(" ");
    int[] a = new int[n];
    for(int i=0; i < n; i++){
      a[i] = Integer.parseInt(str_a_array[i]);
    }
    int dst = 0;
    for(int i=0; i < n-1; i++){
      if(dst < a[i+1]-a[i]) dst = a[i+1]-a[i];
    }
    if(dst < k - a[n-1] + a[0]) dst = k - a[n-1] + a[0];
    System.out.println(k-dst);
  }
} import java.util.*; util.* java class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    String str_kn = scan.nextLine();
    String str_a = scan.nextLine();
    String[] kn = str_kn.split(" ");
    int k = Integer.parseInt(kn[0]);
    int n = Integer.parseInt(kn[1]);
    String[] str_a_array = str_a.split(" ");
    int[] a = new int[n];
    for(int i=0; i < n; i++){
      a[i] = Integer.parseInt(str_a_array[i]);
    }
    int dst = 0;
    for(int i=0; i < n-1; i++){
      if(dst < a[i+1]-a[i]) dst = a[i+1]-a[i];
    }
    if(dst < k - a[n-1] + a[0]) dst = k - a[n-1] + a[0];
    System.out.println(k-dst);
  }
} Main public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    String str_kn = scan.nextLine();
    String str_a = scan.nextLine();
    String[] kn = str_kn.split(" ");
    int k = Integer.parseInt(kn[0]);
    int n = Integer.parseInt(kn[1]);
    String[] str_a_array = str_a.split(" ");
    int[] a = new int[n];
    for(int i=0; i < n; i++){
      a[i] = Integer.parseInt(str_a_array[i]);
    }
    int dst = 0;
    for(int i=0; i < n-1; i++){
      if(dst < a[i+1]-a[i]) dst = a[i+1]-a[i];
    }
    if(dst < k - a[n-1] + a[0]) dst = k - a[n-1] + a[0];
    System.out.println(k-dst);
  } main {
    Scanner scan = new Scanner(System.in);
    String str_kn = scan.nextLine();
    String str_a = scan.nextLine();
    String[] kn = str_kn.split(" ");
    int k = Integer.parseInt(kn[0]);
    int n = Integer.parseInt(kn[1]);
    String[] str_a_array = str_a.split(" ");
    int[] a = new int[n];
    for(int i=0; i < n; i++){
      a[i] = Integer.parseInt(str_a_array[i]);
    }
    int dst = 0;
    for(int i=0; i < n-1; i++){
      if(dst < a[i+1]-a[i]) dst = a[i+1]-a[i];
    }
    if(dst < k - a[n-1] + a[0]) dst = k - a[n-1] + a[0];
    System.out.println(k-dst);
  } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) String str_kn = scan.nextLine(); str_kn scan.nextLine() scan.nextLine scan String str_a = scan.nextLine(); str_a scan.nextLine() scan.nextLine scan String[] kn = str_kn.split(" "); kn str_kn.split(" ") str_kn.split str_kn " " int k = Integer.parseInt(kn[0]); k Integer.parseInt(kn[0]) Integer.parseInt Integer kn[0] kn 0 int n = Integer.parseInt(kn[1]); n Integer.parseInt(kn[1]) Integer.parseInt Integer kn[1] kn 1 String[] str_a_array = str_a.split(" "); str_a_array str_a.split(" ") str_a.split str_a " " int[] a = new int[n]; a new int[n] n for(int i=0; i < n; i++){
      a[i] = Integer.parseInt(str_a_array[i]);
    } int i=0; int i=0; i 0 i < n i n i++ i++ i {
      a[i] = Integer.parseInt(str_a_array[i]);
    } {
      a[i] = Integer.parseInt(str_a_array[i]);
    } a[i] = Integer.parseInt(str_a_array[i]) a[i] a i Integer.parseInt(str_a_array[i]) Integer.parseInt Integer str_a_array[i] str_a_array i int dst = 0; dst 0 for(int i=0; i < n-1; i++){
      if(dst < a[i+1]-a[i]) dst = 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(dst < a[i+1]-a[i]) dst = a[i+1]-a[i];
    } {
      if(dst < a[i+1]-a[i]) dst = a[i+1]-a[i];
    } if(dst < a[i+1]-a[i]) dst = a[i+1]-a[i]; dst < a[i+1]-a[i] dst a[i+1]-a[i] a[i+1] a i+1 i 1 a[i] a i dst = a[i+1]-a[i] dst a[i+1]-a[i] a[i+1] a i+1 i 1 a[i] a i if(dst < k - a[n-1] + a[0]) dst = k - a[n-1] + a[0]; dst < k - a[n-1] + a[0] dst k - a[n-1] + a[0] k - a[n-1] k a[n-1] a n-1 n 1 a[0] a 0 dst = k - a[n-1] + a[0] dst k - a[n-1] + a[0] k - a[n-1] k a[n-1] a n-1 n 1 a[0] a 0 System.out.println(k-dst) System.out.println System.out System System.out k-dst k dst String[] args args
import java.util.*;

class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    String str_kn = scan.nextLine();
    String str_a = scan.nextLine();
    String[] kn = str_kn.split(" ");
    int k = Integer.parseInt(kn[0]);
    int n = Integer.parseInt(kn[1]);
    String[] str_a_array = str_a.split(" ");
    int[] a = new int[n];
    for(int i=0; i < n; i++){
      a[i] = Integer.parseInt(str_a_array[i]);
    }
    int dst = 0;
    for(int i=0; i < n-1; i++){
      if(dst < a[i+1]-a[i]) dst = a[i+1]-a[i];
    }
    if(dst < k - a[n-1] + a[0]) dst = k - a[n-1] + a[0];
    System.out.println(k-dst);
  }
}
