import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    long k = sc.nextLong();
    int n = sc.nextInt();
    long[] a = new long[n];

    for(int i = 0; i < n; i++){
      a[i] = sc.nextLong(); 
    }

    Arrays.sort(a);
    long d = k + a[0] - a[n - 1];
    
    for(int i = 1; i < n; i++){
      if(d < a[i] - a[i - 1]){
        d = a[i] - a[i - 1];
      }
    }

    System.out.println(k - d);
  }
} import java.util.*; util.* java public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    long k = sc.nextLong();
    int n = sc.nextInt();
    long[] a = new long[n];

    for(int i = 0; i < n; i++){
      a[i] = sc.nextLong(); 
    }

    Arrays.sort(a);
    long d = k + a[0] - a[n - 1];
    
    for(int i = 1; i < n; i++){
      if(d < a[i] - a[i - 1]){
        d = a[i] - a[i - 1];
      }
    }

    System.out.println(k - d);
  }
} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    long k = sc.nextLong();
    int n = sc.nextInt();
    long[] a = new long[n];

    for(int i = 0; i < n; i++){
      a[i] = sc.nextLong(); 
    }

    Arrays.sort(a);
    long d = k + a[0] - a[n - 1];
    
    for(int i = 1; i < n; i++){
      if(d < a[i] - a[i - 1]){
        d = a[i] - a[i - 1];
      }
    }

    System.out.println(k - d);
  } main {
    Scanner sc = new Scanner(System.in);

    long k = sc.nextLong();
    int n = sc.nextInt();
    long[] a = new long[n];

    for(int i = 0; i < n; i++){
      a[i] = sc.nextLong(); 
    }

    Arrays.sort(a);
    long d = k + a[0] - a[n - 1];
    
    for(int i = 1; i < n; i++){
      if(d < a[i] - a[i - 1]){
        d = a[i] - a[i - 1];
      }
    }

    System.out.println(k - d);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long k = sc.nextLong(); k sc.nextLong() sc.nextLong sc int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long[] a = new long[n]; a new long[n] n for(int i = 0; i < n; i++){
      a[i] = sc.nextLong(); 
    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
      a[i] = sc.nextLong(); 
    } {
      a[i] = sc.nextLong(); 
    } a[i] = sc.nextLong() a[i] a i sc.nextLong() sc.nextLong sc Arrays.sort(a) Arrays.sort Arrays a long d = k + a[0] - a[n - 1]; d 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 = 1; i < n; i++){
      if(d < a[i] - a[i - 1]){
        d = a[i] - a[i - 1];
      }
    } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
      if(d < a[i] - a[i - 1]){
        d = a[i] - a[i - 1];
      }
    } {
      if(d < a[i] - a[i - 1]){
        d = a[i] - a[i - 1];
      }
    } if(d < a[i] - a[i - 1]){
        d = a[i] - a[i - 1];
      } d < a[i] - a[i - 1] d a[i] - a[i - 1] a[i] a i a[i - 1] a i - 1 i 1 {
        d = a[i] - a[i - 1];
      } d = a[i] - a[i - 1] d a[i] - a[i - 1] a[i] a i a[i - 1] a i - 1 i 1 System.out.println(k - d) System.out.println System.out System System.out k - d k d String[] args args public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    long k = sc.nextLong();
    int n = sc.nextInt();
    long[] a = new long[n];

    for(int i = 0; i < n; i++){
      a[i] = sc.nextLong(); 
    }

    Arrays.sort(a);
    long d = k + a[0] - a[n - 1];
    
    for(int i = 1; i < n; i++){
      if(d < a[i] - a[i - 1]){
        d = a[i] - a[i - 1];
      }
    }

    System.out.println(k - d);
  }
} public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    long k = sc.nextLong();
    int n = sc.nextInt();
    long[] a = new long[n];

    for(int i = 0; i < n; i++){
      a[i] = sc.nextLong(); 
    }

    Arrays.sort(a);
    long d = k + a[0] - a[n - 1];
    
    for(int i = 1; i < n; i++){
      if(d < a[i] - a[i - 1]){
        d = a[i] - a[i - 1];
      }
    }

    System.out.println(k - d);
  }
} Main
import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    long k = sc.nextLong();
    int n = sc.nextInt();
    long[] a = new long[n];

    for(int i = 0; i < n; i++){
      a[i] = sc.nextLong(); 
    }

    Arrays.sort(a);
    long d = k + a[0] - a[n - 1];
    
    for(int i = 1; i < n; i++){
      if(d < a[i] - a[i - 1]){
        d = a[i] - a[i - 1];
      }
    }

    System.out.println(k - d);
  }
}
