import java.util.Scanner;
import java.util.LinkedList;
import java.lang.Math;
import java.util.Arrays;
import java.util.Collections;

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];

        for(int i=0;i <n;i++){
            A[i]=sc.nextInt();
        }
        sc.close();

        Arrays.sort(A);

        int[] B = new int[n];
        
        for(int i=0;i<n-1;i++){
            B[i]=A[i+1]-A[i];
        }
        B[n-1] = A[0]+k-A[n-1];

        int max = B[0];

        for(int i=0;i<B.length;i++){
            if(max<B[i]){
                max = B[i];
            }
        }
        k=k-max;
        System.out.print(k);
        
    //     for(int i=0;i<A.length;i++){
    //         System.out.println(A[i]);
    //     }
    //     for(int i=0;i<B.length;i++){
    //         System.out.println(B[i]);
    //     }
    }
} import java.util.Scanner; Scanner java.util import java.util.LinkedList; LinkedList java.util import java.lang.Math; Math java.lang import java.util.Arrays; Arrays java.util import java.util.Collections; Collections java.util 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];

        for(int i=0;i <n;i++){
            A[i]=sc.nextInt();
        }
        sc.close();

        Arrays.sort(A);

        int[] B = new int[n];
        
        for(int i=0;i<n-1;i++){
            B[i]=A[i+1]-A[i];
        }
        B[n-1] = A[0]+k-A[n-1];

        int max = B[0];

        for(int i=0;i<B.length;i++){
            if(max<B[i]){
                max = B[i];
            }
        }
        k=k-max;
        System.out.print(k);
        
    //     for(int i=0;i<A.length;i++){
    //         System.out.println(A[i]);
    //     }
    //     for(int i=0;i<B.length;i++){
    //         System.out.println(B[i]);
    //     }
    }
} 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];

        for(int i=0;i <n;i++){
            A[i]=sc.nextInt();
        }
        sc.close();

        Arrays.sort(A);

        int[] B = new int[n];
        
        for(int i=0;i<n-1;i++){
            B[i]=A[i+1]-A[i];
        }
        B[n-1] = A[0]+k-A[n-1];

        int max = B[0];

        for(int i=0;i<B.length;i++){
            if(max<B[i]){
                max = B[i];
            }
        }
        k=k-max;
        System.out.print(k);
        
    //     for(int i=0;i<A.length;i++){
    //         System.out.println(A[i]);
    //     }
    //     for(int i=0;i<B.length;i++){
    //         System.out.println(B[i]);
    //     }
    } main {
        Scanner sc = new Scanner(System.in);

        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] A = new int[n];

        for(int i=0;i <n;i++){
            A[i]=sc.nextInt();
        }
        sc.close();

        Arrays.sort(A);

        int[] B = new int[n];
        
        for(int i=0;i<n-1;i++){
            B[i]=A[i+1]-A[i];
        }
        B[n-1] = A[0]+k-A[n-1];

        int max = B[0];

        for(int i=0;i<B.length;i++){
            if(max<B[i]){
                max = B[i];
            }
        }
        k=k-max;
        System.out.print(k);
        
    //     for(int i=0;i<A.length;i++){
    //         System.out.println(A[i]);
    //     }
    //     for(int i=0;i<B.length;i++){
    //         System.out.println(B[i]);
    //     }
    } 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]; A new int[n] n 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 sc.close() sc.close sc Arrays.sort(A) Arrays.sort Arrays A int[] B = new int[n]; B new int[n] n for(int i=0;i<n-1;i++){
            B[i]=A[i+1]-A[i];
        } int i=0; int i=0; i 0 i<n-1 i n-1 n 1 i++ i++ i {
            B[i]=A[i+1]-A[i];
        } {
            B[i]=A[i+1]-A[i];
        } B[i]=A[i+1]-A[i] B[i] B i A[i+1]-A[i] A[i+1] A i+1 i 1 A[i] A i B[n-1] = A[0]+k-A[n-1] B[n-1] B n-1 n 1 A[0]+k-A[n-1] A[0]+k A[0] A 0 k A[n-1] A n-1 n 1 int max = B[0]; max B[0] B 0 for(int i=0;i<B.length;i++){
            if(max<B[i]){
                max = B[i];
            }
        } int i=0; int i=0; i 0 i<B.length i B.length B B.length i++ i++ i {
            if(max<B[i]){
                max = B[i];
            }
        } {
            if(max<B[i]){
                max = B[i];
            }
        } if(max<B[i]){
                max = B[i];
            } max<B[i] max B[i] B i {
                max = B[i];
            } max = B[i] max B[i] B i k=k-max k k-max k max System.out.print(k) System.out.print System.out System System.out k String[] args args
import java.util.Scanner;
import java.util.LinkedList;
import java.lang.Math;
import java.util.Arrays;
import java.util.Collections;

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];

        for(int i=0;i <n;i++){
            A[i]=sc.nextInt();
        }
        sc.close();

        Arrays.sort(A);

        int[] B = new int[n];
        
        for(int i=0;i<n-1;i++){
            B[i]=A[i+1]-A[i];
        }
        B[n-1] = A[0]+k-A[n-1];

        int max = B[0];

        for(int i=0;i<B.length;i++){
            if(max<B[i]){
                max = B[i];
            }
        }
        k=k-max;
        System.out.print(k);
        
    //     for(int i=0;i<A.length;i++){
    //         System.out.println(A[i]);
    //     }
    //     for(int i=0;i<B.length;i++){
    //         System.out.println(B[i]);
    //     }
    }
}