import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String[] str = in.readLine().split(" ");
    int k = Integer.parseInt(str[0]);
    int n = Integer.parseInt(str[1]);
    int[] a = new int[n];
    str = in.readLine().split(" ");
    for(int i=0; i<n; i++) {
      a[i] = Integer.parseInt(str[i]);
    }
    int sep = k-a[n-1]+a[0];
    for(int i=1; i<n; i++) {
      sep = Math.max(sep,a[i] - a[i-1]);
    }
    System.out.println(k-sep);
  }
} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io import java.io.IOException; IOException java.io public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String[] str = in.readLine().split(" ");
    int k = Integer.parseInt(str[0]);
    int n = Integer.parseInt(str[1]);
    int[] a = new int[n];
    str = in.readLine().split(" ");
    for(int i=0; i<n; i++) {
      a[i] = Integer.parseInt(str[i]);
    }
    int sep = k-a[n-1]+a[0];
    for(int i=1; i<n; i++) {
      sep = Math.max(sep,a[i] - a[i-1]);
    }
    System.out.println(k-sep);
  }
} Main public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String[] str = in.readLine().split(" ");
    int k = Integer.parseInt(str[0]);
    int n = Integer.parseInt(str[1]);
    int[] a = new int[n];
    str = in.readLine().split(" ");
    for(int i=0; i<n; i++) {
      a[i] = Integer.parseInt(str[i]);
    }
    int sep = k-a[n-1]+a[0];
    for(int i=1; i<n; i++) {
      sep = Math.max(sep,a[i] - a[i-1]);
    }
    System.out.println(k-sep);
  } main {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String[] str = in.readLine().split(" ");
    int k = Integer.parseInt(str[0]);
    int n = Integer.parseInt(str[1]);
    int[] a = new int[n];
    str = in.readLine().split(" ");
    for(int i=0; i<n; i++) {
      a[i] = Integer.parseInt(str[i]);
    }
    int sep = k-a[n-1]+a[0];
    for(int i=1; i<n; i++) {
      sep = Math.max(sep,a[i] - a[i-1]);
    }
    System.out.println(k-sep);
  } BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); in new BufferedReader(new InputStreamReader(System.in)) String[] str = in.readLine().split(" "); str in.readLine().split(" ") in.readLine().split in.readLine() in.readLine in " " int k = Integer.parseInt(str[0]); k Integer.parseInt(str[0]) Integer.parseInt Integer str[0] str 0 int n = Integer.parseInt(str[1]); n Integer.parseInt(str[1]) Integer.parseInt Integer str[1] str 1 int[] a = new int[n]; a new int[n] n str = in.readLine().split(" ") str in.readLine().split(" ") in.readLine().split in.readLine() in.readLine in " " for(int i=0; i<n; i++) {
      a[i] = Integer.parseInt(str[i]);
    } int i=0; int i=0; i 0 i<n i n i++ i++ i {
      a[i] = Integer.parseInt(str[i]);
    } {
      a[i] = Integer.parseInt(str[i]);
    } a[i] = Integer.parseInt(str[i]) a[i] a i Integer.parseInt(str[i]) Integer.parseInt Integer str[i] str i int sep = k-a[n-1]+a[0]; sep k-a[n-1]+a[0] k-a[n-1] k a[n-1] a n-1 n 1 a[0] a 0 for(int i=1; i<n; i++) {
      sep = Math.max(sep,a[i] - a[i-1]);
    } int i=1; int i=1; i 1 i<n i n i++ i++ i {
      sep = Math.max(sep,a[i] - a[i-1]);
    } {
      sep = Math.max(sep,a[i] - a[i-1]);
    } sep = Math.max(sep,a[i] - a[i-1]) sep Math.max(sep,a[i] - a[i-1]) Math.max Math sep a[i] - a[i-1] a[i] a i a[i-1] a i-1 i 1 System.out.println(k-sep) System.out.println System.out System System.out k-sep k sep String[] args args public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String[] str = in.readLine().split(" ");
    int k = Integer.parseInt(str[0]);
    int n = Integer.parseInt(str[1]);
    int[] a = new int[n];
    str = in.readLine().split(" ");
    for(int i=0; i<n; i++) {
      a[i] = Integer.parseInt(str[i]);
    }
    int sep = k-a[n-1]+a[0];
    for(int i=1; i<n; i++) {
      sep = Math.max(sep,a[i] - a[i-1]);
    }
    System.out.println(k-sep);
  }
} public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String[] str = in.readLine().split(" ");
    int k = Integer.parseInt(str[0]);
    int n = Integer.parseInt(str[1]);
    int[] a = new int[n];
    str = in.readLine().split(" ");
    for(int i=0; i<n; i++) {
      a[i] = Integer.parseInt(str[i]);
    }
    int sep = k-a[n-1]+a[0];
    for(int i=1; i<n; i++) {
      sep = Math.max(sep,a[i] - a[i-1]);
    }
    System.out.println(k-sep);
  }
} Main
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String[] str = in.readLine().split(" ");
    int k = Integer.parseInt(str[0]);
    int n = Integer.parseInt(str[1]);
    int[] a = new int[n];
    str = in.readLine().split(" ");
    for(int i=0; i<n; i++) {
      a[i] = Integer.parseInt(str[i]);
    }
    int sep = k-a[n-1]+a[0];
    for(int i=1; i<n; i++) {
      sep = Math.max(sep,a[i] - a[i-1]);
    }
    System.out.println(k-sep);
  }
}