import java.util.*;
public class Main {

  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int para = s.nextInt();
    int n = s.nextInt();
    int[] arr = new int[n];

    for(int i=0;i<n;i++) {
      arr[i]=s.nextInt();
    }
    int[] d = new int[n];

    d[0]=arr[0]+para-arr[n-1];
    for(int i=1;i<n;i++) {
      d[i] =  arr[i]-arr[i-1];
    }
    int max = 0;
    for(int i=0;i<n;i++) {
      if(d[i]>max) 
        max = d[i];
    }
    System.out.println(para-max);
  }

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

  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int para = s.nextInt();
    int n = s.nextInt();
    int[] arr = new int[n];

    for(int i=0;i<n;i++) {
      arr[i]=s.nextInt();
    }
    int[] d = new int[n];

    d[0]=arr[0]+para-arr[n-1];
    for(int i=1;i<n;i++) {
      d[i] =  arr[i]-arr[i-1];
    }
    int max = 0;
    for(int i=0;i<n;i++) {
      if(d[i]>max) 
        max = d[i];
    }
    System.out.println(para-max);
  }

 } Main public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int para = s.nextInt();
    int n = s.nextInt();
    int[] arr = new int[n];

    for(int i=0;i<n;i++) {
      arr[i]=s.nextInt();
    }
    int[] d = new int[n];

    d[0]=arr[0]+para-arr[n-1];
    for(int i=1;i<n;i++) {
      d[i] =  arr[i]-arr[i-1];
    }
    int max = 0;
    for(int i=0;i<n;i++) {
      if(d[i]>max) 
        max = d[i];
    }
    System.out.println(para-max);
  } main {
    Scanner s = new Scanner(System.in);
    int para = s.nextInt();
    int n = s.nextInt();
    int[] arr = new int[n];

    for(int i=0;i<n;i++) {
      arr[i]=s.nextInt();
    }
    int[] d = new int[n];

    d[0]=arr[0]+para-arr[n-1];
    for(int i=1;i<n;i++) {
      d[i] =  arr[i]-arr[i-1];
    }
    int max = 0;
    for(int i=0;i<n;i++) {
      if(d[i]>max) 
        max = d[i];
    }
    System.out.println(para-max);
  } Scanner s = new Scanner(System.in); s new Scanner(System.in) int para = s.nextInt(); para s.nextInt() s.nextInt s int n = s.nextInt(); n s.nextInt() s.nextInt s int[] arr = new int[n]; arr new int[n] n for(int i=0;i<n;i++) {
      arr[i]=s.nextInt();
    } int i=0; int i=0; i 0 i<n i n i++ i++ i {
      arr[i]=s.nextInt();
    } {
      arr[i]=s.nextInt();
    } arr[i]=s.nextInt() arr[i] arr i s.nextInt() s.nextInt s int[] d = new int[n]; d new int[n] n d[0]=arr[0]+para-arr[n-1] d[0] d 0 arr[0]+para-arr[n-1] arr[0]+para arr[0] arr 0 para arr[n-1] arr n-1 n 1 for(int i=1;i<n;i++) {
      d[i] =  arr[i]-arr[i-1];
    } int i=1; int i=1; i 1 i<n i n i++ i++ i {
      d[i] =  arr[i]-arr[i-1];
    } {
      d[i] =  arr[i]-arr[i-1];
    } d[i] =  arr[i]-arr[i-1] d[i] d i arr[i]-arr[i-1] arr[i] arr i arr[i-1] arr i-1 i 1 int max = 0; max 0 for(int i=0;i<n;i++) {
      if(d[i]>max) 
        max = d[i];
    } int i=0; int i=0; i 0 i<n i n i++ i++ i {
      if(d[i]>max) 
        max = d[i];
    } {
      if(d[i]>max) 
        max = d[i];
    } if(d[i]>max) 
        max = d[i]; d[i]>max d[i] d i max max = d[i] max d[i] d i System.out.println(para-max) System.out.println System.out System System.out para-max para max String[] args args public class Main {

  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int para = s.nextInt();
    int n = s.nextInt();
    int[] arr = new int[n];

    for(int i=0;i<n;i++) {
      arr[i]=s.nextInt();
    }
    int[] d = new int[n];

    d[0]=arr[0]+para-arr[n-1];
    for(int i=1;i<n;i++) {
      d[i] =  arr[i]-arr[i-1];
    }
    int max = 0;
    for(int i=0;i<n;i++) {
      if(d[i]>max) 
        max = d[i];
    }
    System.out.println(para-max);
  }

 } public class Main {

  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int para = s.nextInt();
    int n = s.nextInt();
    int[] arr = new int[n];

    for(int i=0;i<n;i++) {
      arr[i]=s.nextInt();
    }
    int[] d = new int[n];

    d[0]=arr[0]+para-arr[n-1];
    for(int i=1;i<n;i++) {
      d[i] =  arr[i]-arr[i-1];
    }
    int max = 0;
    for(int i=0;i<n;i++) {
      if(d[i]>max) 
        max = d[i];
    }
    System.out.println(para-max);
  }

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

  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int para = s.nextInt();
    int n = s.nextInt();
    int[] arr = new int[n];

    for(int i=0;i<n;i++) {
      arr[i]=s.nextInt();
    }
    int[] d = new int[n];

    d[0]=arr[0]+para-arr[n-1];
    for(int i=1;i<n;i++) {
      d[i] =  arr[i]-arr[i-1];
    }
    int max = 0;
    for(int i=0;i<n;i++) {
      if(d[i]>max) 
        max = d[i];
    }
    System.out.println(para-max);
  }

 }