import java.util.*;

public class Main {
  public static void main(String[] args ) throws Exception {
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int N = sc.nextInt();
    ArrayList<Integer> A = new ArrayList<Integer>();
    while(sc.hasNext()){
        A.add(sc.nextInt());
    }
    int[] right = new int[N];
    int max = 0;
    for(int i = 0;i < N-1;i++){
        if(i == N-2){
            right[i+1] = ((K - A.get(i+1)) + A.get(0)) ;
            // System.out.println("right[i+1]" + right[i+1]);
        }
        right[i] = A.get(i+1) - A.get(i);
        // System.out.println(A.get(i+1));
        // System.out.println(A.get(i));
    }   
    for(int i = 0;i < N-1;i++){
                if(max < Math.max(right[i+1],right[i]))
        max = Math.max(right[i+1],right[i]);
        // System.out.println("max" + max);
    }
    System.out.println(K - max);
  }
  
} import java.util.*; util.* java public class Main {
  public static void main(String[] args ) throws Exception {
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int N = sc.nextInt();
    ArrayList<Integer> A = new ArrayList<Integer>();
    while(sc.hasNext()){
        A.add(sc.nextInt());
    }
    int[] right = new int[N];
    int max = 0;
    for(int i = 0;i < N-1;i++){
        if(i == N-2){
            right[i+1] = ((K - A.get(i+1)) + A.get(0)) ;
            // System.out.println("right[i+1]" + right[i+1]);
        }
        right[i] = A.get(i+1) - A.get(i);
        // System.out.println(A.get(i+1));
        // System.out.println(A.get(i));
    }   
    for(int i = 0;i < N-1;i++){
                if(max < Math.max(right[i+1],right[i]))
        max = Math.max(right[i+1],right[i]);
        // System.out.println("max" + max);
    }
    System.out.println(K - max);
  }
  
} Main public static void main(String[] args ) throws Exception {
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int N = sc.nextInt();
    ArrayList<Integer> A = new ArrayList<Integer>();
    while(sc.hasNext()){
        A.add(sc.nextInt());
    }
    int[] right = new int[N];
    int max = 0;
    for(int i = 0;i < N-1;i++){
        if(i == N-2){
            right[i+1] = ((K - A.get(i+1)) + A.get(0)) ;
            // System.out.println("right[i+1]" + right[i+1]);
        }
        right[i] = A.get(i+1) - A.get(i);
        // System.out.println(A.get(i+1));
        // System.out.println(A.get(i));
    }   
    for(int i = 0;i < N-1;i++){
                if(max < Math.max(right[i+1],right[i]))
        max = Math.max(right[i+1],right[i]);
        // System.out.println("max" + max);
    }
    System.out.println(K - max);
  } main {
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int N = sc.nextInt();
    ArrayList<Integer> A = new ArrayList<Integer>();
    while(sc.hasNext()){
        A.add(sc.nextInt());
    }
    int[] right = new int[N];
    int max = 0;
    for(int i = 0;i < N-1;i++){
        if(i == N-2){
            right[i+1] = ((K - A.get(i+1)) + A.get(0)) ;
            // System.out.println("right[i+1]" + right[i+1]);
        }
        right[i] = A.get(i+1) - A.get(i);
        // System.out.println(A.get(i+1));
        // System.out.println(A.get(i));
    }   
    for(int i = 0;i < N-1;i++){
                if(max < Math.max(right[i+1],right[i]))
        max = Math.max(right[i+1],right[i]);
        // System.out.println("max" + max);
    }
    System.out.println(K - max);
  } 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 ArrayList<Integer> A = new ArrayList<Integer>(); A new ArrayList<Integer>() while(sc.hasNext()){
        A.add(sc.nextInt());
    } sc.hasNext() sc.hasNext sc {
        A.add(sc.nextInt());
    } A.add(sc.nextInt()) A.add A sc.nextInt() sc.nextInt sc int[] right = new int[N]; right new int[N] N int max = 0; max 0 for(int i = 0;i < N-1;i++){
        if(i == N-2){
            right[i+1] = ((K - A.get(i+1)) + A.get(0)) ;
            // System.out.println("right[i+1]" + right[i+1]);
        }
        right[i] = A.get(i+1) - A.get(i);
        // System.out.println(A.get(i+1));
        // System.out.println(A.get(i));
    } int i = 0; int i = 0; i 0 i < N-1 i N-1 N 1 i++ i++ i {
        if(i == N-2){
            right[i+1] = ((K - A.get(i+1)) + A.get(0)) ;
            // System.out.println("right[i+1]" + right[i+1]);
        }
        right[i] = A.get(i+1) - A.get(i);
        // System.out.println(A.get(i+1));
        // System.out.println(A.get(i));
    } {
        if(i == N-2){
            right[i+1] = ((K - A.get(i+1)) + A.get(0)) ;
            // System.out.println("right[i+1]" + right[i+1]);
        }
        right[i] = A.get(i+1) - A.get(i);
        // System.out.println(A.get(i+1));
        // System.out.println(A.get(i));
    } if(i == N-2){
            right[i+1] = ((K - A.get(i+1)) + A.get(0)) ;
            // System.out.println("right[i+1]" + right[i+1]);
        } i == N-2 i N-2 N 2 {
            right[i+1] = ((K - A.get(i+1)) + A.get(0)) ;
            // System.out.println("right[i+1]" + right[i+1]);
        } right[i+1] = ((K - A.get(i+1)) + A.get(0)) right[i+1] right i+1 i 1 ((K - A.get(i+1)) + A.get(0)) (K - A.get(i+1)) K A.get(i+1) A.get A i+1 i 1 A.get(0) A.get A 0 right[i] = A.get(i+1) - A.get(i) right[i] right i A.get(i+1) - A.get(i) A.get(i+1) A.get A i+1 i 1 A.get(i) A.get A i for(int i = 0;i < N-1;i++){
                if(max < Math.max(right[i+1],right[i]))
        max = Math.max(right[i+1],right[i]);
        // System.out.println("max" + max);
    } int i = 0; int i = 0; i 0 i < N-1 i N-1 N 1 i++ i++ i {
                if(max < Math.max(right[i+1],right[i]))
        max = Math.max(right[i+1],right[i]);
        // System.out.println("max" + max);
    } {
                if(max < Math.max(right[i+1],right[i]))
        max = Math.max(right[i+1],right[i]);
        // System.out.println("max" + max);
    } if(max < Math.max(right[i+1],right[i]))
        max = Math.max(right[i+1],right[i]); max < Math.max(right[i+1],right[i]) max Math.max(right[i+1],right[i]) Math.max Math right[i+1] right i+1 i 1 right[i] right i max = Math.max(right[i+1],right[i]) max Math.max(right[i+1],right[i]) Math.max Math right[i+1] right i+1 i 1 right[i] right i System.out.println(K - max) System.out.println System.out System System.out K - max K max String[] args args public class Main {
  public static void main(String[] args ) throws Exception {
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int N = sc.nextInt();
    ArrayList<Integer> A = new ArrayList<Integer>();
    while(sc.hasNext()){
        A.add(sc.nextInt());
    }
    int[] right = new int[N];
    int max = 0;
    for(int i = 0;i < N-1;i++){
        if(i == N-2){
            right[i+1] = ((K - A.get(i+1)) + A.get(0)) ;
            // System.out.println("right[i+1]" + right[i+1]);
        }
        right[i] = A.get(i+1) - A.get(i);
        // System.out.println(A.get(i+1));
        // System.out.println(A.get(i));
    }   
    for(int i = 0;i < N-1;i++){
                if(max < Math.max(right[i+1],right[i]))
        max = Math.max(right[i+1],right[i]);
        // System.out.println("max" + max);
    }
    System.out.println(K - max);
  }
  
} public class Main {
  public static void main(String[] args ) throws Exception {
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int N = sc.nextInt();
    ArrayList<Integer> A = new ArrayList<Integer>();
    while(sc.hasNext()){
        A.add(sc.nextInt());
    }
    int[] right = new int[N];
    int max = 0;
    for(int i = 0;i < N-1;i++){
        if(i == N-2){
            right[i+1] = ((K - A.get(i+1)) + A.get(0)) ;
            // System.out.println("right[i+1]" + right[i+1]);
        }
        right[i] = A.get(i+1) - A.get(i);
        // System.out.println(A.get(i+1));
        // System.out.println(A.get(i));
    }   
    for(int i = 0;i < N-1;i++){
                if(max < Math.max(right[i+1],right[i]))
        max = Math.max(right[i+1],right[i]);
        // System.out.println("max" + max);
    }
    System.out.println(K - max);
  }
  
} Main
import java.util.*;

public class Main {
  public static void main(String[] args ) throws Exception {
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int N = sc.nextInt();
    ArrayList<Integer> A = new ArrayList<Integer>();
    while(sc.hasNext()){
        A.add(sc.nextInt());
    }
    int[] right = new int[N];
    int max = 0;
    for(int i = 0;i < N-1;i++){
        if(i == N-2){
            right[i+1] = ((K - A.get(i+1)) + A.get(0)) ;
            // System.out.println("right[i+1]" + right[i+1]);
        }
        right[i] = A.get(i+1) - A.get(i);
        // System.out.println(A.get(i+1));
        // System.out.println(A.get(i));
    }   
    for(int i = 0;i < N-1;i++){
                if(max < Math.max(right[i+1],right[i]))
        max = Math.max(right[i+1],right[i]);
        // System.out.println("max" + max);
    }
    System.out.println(K - max);
  }
  
}