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 a[]=new int[n+1];
    for (int i=0;i<n;i++) {
      a[i]=sc.nextInt();
    }
    a[n]=k+a[0]-a[n-1];

    for (int i=n-1;i>0;i--) {
      a[i]=a[i]-a[i-1];
      // System.out.println(a[i]);
    }

    Arrays.sort(a);


    System.out.println(k-a[n]);
  }
} 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 a[]=new int[n+1];
    for (int i=0;i<n;i++) {
      a[i]=sc.nextInt();
    }
    a[n]=k+a[0]-a[n-1];

    for (int i=n-1;i>0;i--) {
      a[i]=a[i]-a[i-1];
      // System.out.println(a[i]);
    }

    Arrays.sort(a);


    System.out.println(k-a[n]);
  }
} Main public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int k=sc.nextInt();
    int n=sc.nextInt();
    int a[]=new int[n+1];
    for (int i=0;i<n;i++) {
      a[i]=sc.nextInt();
    }
    a[n]=k+a[0]-a[n-1];

    for (int i=n-1;i>0;i--) {
      a[i]=a[i]-a[i-1];
      // System.out.println(a[i]);
    }

    Arrays.sort(a);


    System.out.println(k-a[n]);
  } main {
    Scanner sc=new Scanner(System.in);
    int k=sc.nextInt();
    int n=sc.nextInt();
    int a[]=new int[n+1];
    for (int i=0;i<n;i++) {
      a[i]=sc.nextInt();
    }
    a[n]=k+a[0]-a[n-1];

    for (int i=n-1;i>0;i--) {
      a[i]=a[i]-a[i-1];
      // System.out.println(a[i]);
    }

    Arrays.sort(a);


    System.out.println(k-a[n]);
  } 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 a[]=new int[n+1]; a new int[n+1] n+1 n 1 for (int i=0;i<n;i++) {
      a[i]=sc.nextInt();
    } int i=0; int i=0; i 0 i<n i n i++ i++ i {
      a[i]=sc.nextInt();
    } {
      a[i]=sc.nextInt();
    } a[i]=sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc a[n]=k+a[0]-a[n-1] a[n] a n k+a[0]-a[n-1] k+a[0] k a[0] a 0 a[n-1] a n-1 n 1 for (int i=n-1;i>0;i--) {
      a[i]=a[i]-a[i-1];
      // System.out.println(a[i]);
    } int i=n-1; int i=n-1; i n-1 n 1 i>0 i 0 i-- i-- i {
      a[i]=a[i]-a[i-1];
      // System.out.println(a[i]);
    } {
      a[i]=a[i]-a[i-1];
      // System.out.println(a[i]);
    } a[i]=a[i]-a[i-1] a[i] a i a[i]-a[i-1] a[i] a i a[i-1] a i-1 i 1 Arrays.sort(a) Arrays.sort Arrays a System.out.println(k-a[n]) System.out.println System.out System System.out k-a[n] k a[n] a n 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 a[]=new int[n+1];
    for (int i=0;i<n;i++) {
      a[i]=sc.nextInt();
    }
    a[n]=k+a[0]-a[n-1];

    for (int i=n-1;i>0;i--) {
      a[i]=a[i]-a[i-1];
      // System.out.println(a[i]);
    }

    Arrays.sort(a);


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

    for (int i=n-1;i>0;i--) {
      a[i]=a[i]-a[i-1];
      // System.out.println(a[i]);
    }

    Arrays.sort(a);


    System.out.println(k-a[n]);
  }
} 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 a[]=new int[n+1];
    for (int i=0;i<n;i++) {
      a[i]=sc.nextInt();
    }
    a[n]=k+a[0]-a[n-1];

    for (int i=n-1;i>0;i--) {
      a[i]=a[i]-a[i-1];
      // System.out.println(a[i]);
    }

    Arrays.sort(a);


    System.out.println(k-a[n]);
  }
}
