import java.util.Scanner;

class Main {

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

        int K=stdIn.nextInt(); //1周の距離
        int N=stdIn.nextInt(); //家の軒数
        
        int min=0;
        int []a=new int[N];

        for(int i=0; i<N; i++){
            a[i]=stdIn.nextInt();
        }

        for(int i=0; i<N-1; i++){
            if(a[i+1]-a[i]>min){
                min=a[i+1]-a[i];
            }
        }
        if(K-a[N-1]+a[0]>min){
            min=K-a[N-1]+a[0];
        }
        System.out.println(K-min);
        

        
    }

} import java.util.Scanner; Scanner java.util class Main {

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

        int K=stdIn.nextInt(); //1周の距離
        int N=stdIn.nextInt(); //家の軒数
        
        int min=0;
        int []a=new int[N];

        for(int i=0; i<N; i++){
            a[i]=stdIn.nextInt();
        }

        for(int i=0; i<N-1; i++){
            if(a[i+1]-a[i]>min){
                min=a[i+1]-a[i];
            }
        }
        if(K-a[N-1]+a[0]>min){
            min=K-a[N-1]+a[0];
        }
        System.out.println(K-min);
        

        
    }

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

        int K=stdIn.nextInt(); //1周の距離
        int N=stdIn.nextInt(); //家の軒数
        
        int min=0;
        int []a=new int[N];

        for(int i=0; i<N; i++){
            a[i]=stdIn.nextInt();
        }

        for(int i=0; i<N-1; i++){
            if(a[i+1]-a[i]>min){
                min=a[i+1]-a[i];
            }
        }
        if(K-a[N-1]+a[0]>min){
            min=K-a[N-1]+a[0];
        }
        System.out.println(K-min);
        

        
    } main {
        Scanner stdIn=new Scanner(System.in);

        int K=stdIn.nextInt(); //1周の距離
        int N=stdIn.nextInt(); //家の軒数
        
        int min=0;
        int []a=new int[N];

        for(int i=0; i<N; i++){
            a[i]=stdIn.nextInt();
        }

        for(int i=0; i<N-1; i++){
            if(a[i+1]-a[i]>min){
                min=a[i+1]-a[i];
            }
        }
        if(K-a[N-1]+a[0]>min){
            min=K-a[N-1]+a[0];
        }
        System.out.println(K-min);
        

        
    } Scanner stdIn=new Scanner(System.in); stdIn new Scanner(System.in) int K=stdIn.nextInt(); K stdIn.nextInt() stdIn.nextInt stdIn int N=stdIn.nextInt(); N stdIn.nextInt() stdIn.nextInt stdIn int min=0; min 0 int []a=new int[N]; a new int[N] N for(int i=0; i<N; i++){
            a[i]=stdIn.nextInt();
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            a[i]=stdIn.nextInt();
        } {
            a[i]=stdIn.nextInt();
        } a[i]=stdIn.nextInt() a[i] a i stdIn.nextInt() stdIn.nextInt stdIn for(int i=0; i<N-1; i++){
            if(a[i+1]-a[i]>min){
                min=a[i+1]-a[i];
            }
        } int i=0; int i=0; i 0 i<N-1 i N-1 N 1 i++ i++ i {
            if(a[i+1]-a[i]>min){
                min=a[i+1]-a[i];
            }
        } {
            if(a[i+1]-a[i]>min){
                min=a[i+1]-a[i];
            }
        } if(a[i+1]-a[i]>min){
                min=a[i+1]-a[i];
            } a[i+1]-a[i]>min a[i+1]-a[i] a[i+1] a i+1 i 1 a[i] a i min {
                min=a[i+1]-a[i];
            } min=a[i+1]-a[i] min a[i+1]-a[i] a[i+1] a i+1 i 1 a[i] a i if(K-a[N-1]+a[0]>min){
            min=K-a[N-1]+a[0];
        } K-a[N-1]+a[0]>min K-a[N-1]+a[0] K-a[N-1] K a[N-1] a N-1 N 1 a[0] a 0 min {
            min=K-a[N-1]+a[0];
        } min=K-a[N-1]+a[0] min K-a[N-1]+a[0] K-a[N-1] K a[N-1] a N-1 N 1 a[0] a 0 System.out.println(K-min) System.out.println System.out System System.out K-min K min String[] args args
import java.util.Scanner;

class Main {

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

        int K=stdIn.nextInt(); //1周の距離
        int N=stdIn.nextInt(); //家の軒数
        
        int min=0;
        int []a=new int[N];

        for(int i=0; i<N; i++){
            a[i]=stdIn.nextInt();
        }

        for(int i=0; i<N-1; i++){
            if(a[i+1]-a[i]>min){
                min=a[i+1]-a[i];
            }
        }
        if(K-a[N-1]+a[0]>min){
            min=K-a[N-1]+a[0];
        }
        System.out.println(K-min);
        

        
    }

}