import java.util.Scanner;

/**
 * Main
 */
public 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+1];
        int maxLength = 0;

        for(int i = 1; i <= N; i++){
            A[i] = sc.nextInt();
            // if(i != 1){
                if(maxLength < A[i]-A[i-1]){
                    maxLength =  A[i]-A[i-1];
                }
            // }
        }
        if(maxLength < A[1]+K-A[N]){
            maxLength =  A[1]+K-A[N];
        }

        System.out.println(K-maxLength);    
    }
} import java.util.Scanner; Scanner java.util public 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+1];
        int maxLength = 0;

        for(int i = 1; i <= N; i++){
            A[i] = sc.nextInt();
            // if(i != 1){
                if(maxLength < A[i]-A[i-1]){
                    maxLength =  A[i]-A[i-1];
                }
            // }
        }
        if(maxLength < A[1]+K-A[N]){
            maxLength =  A[1]+K-A[N];
        }

        System.out.println(K-maxLength);    
    }
} 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+1];
        int maxLength = 0;

        for(int i = 1; i <= N; i++){
            A[i] = sc.nextInt();
            // if(i != 1){
                if(maxLength < A[i]-A[i-1]){
                    maxLength =  A[i]-A[i-1];
                }
            // }
        }
        if(maxLength < A[1]+K-A[N]){
            maxLength =  A[1]+K-A[N];
        }

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

        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] A = new int[N+1];
        int maxLength = 0;

        for(int i = 1; i <= N; i++){
            A[i] = sc.nextInt();
            // if(i != 1){
                if(maxLength < A[i]-A[i-1]){
                    maxLength =  A[i]-A[i-1];
                }
            // }
        }
        if(maxLength < A[1]+K-A[N]){
            maxLength =  A[1]+K-A[N];
        }

        System.out.println(K-maxLength);    
    } 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+1]; A new int[N+1] N+1 N 1 int maxLength = 0; maxLength 0 for(int i = 1; i <= N; i++){
            A[i] = sc.nextInt();
            // if(i != 1){
                if(maxLength < A[i]-A[i-1]){
                    maxLength =  A[i]-A[i-1];
                }
            // }
        } int i = 1; int i = 1; i 1 i <= N i N i++ i++ i {
            A[i] = sc.nextInt();
            // if(i != 1){
                if(maxLength < A[i]-A[i-1]){
                    maxLength =  A[i]-A[i-1];
                }
            // }
        } {
            A[i] = sc.nextInt();
            // if(i != 1){
                if(maxLength < A[i]-A[i-1]){
                    maxLength =  A[i]-A[i-1];
                }
            // }
        } A[i] = sc.nextInt() A[i] A i sc.nextInt() sc.nextInt sc if(maxLength < A[i]-A[i-1]){
                    maxLength =  A[i]-A[i-1];
                } maxLength < A[i]-A[i-1] maxLength A[i]-A[i-1] A[i] A i A[i-1] A i-1 i 1 {
                    maxLength =  A[i]-A[i-1];
                } maxLength =  A[i]-A[i-1] maxLength A[i]-A[i-1] A[i] A i A[i-1] A i-1 i 1 if(maxLength < A[1]+K-A[N]){
            maxLength =  A[1]+K-A[N];
        } maxLength < A[1]+K-A[N] maxLength A[1]+K-A[N] A[1]+K A[1] A 1 K A[N] A N {
            maxLength =  A[1]+K-A[N];
        } maxLength =  A[1]+K-A[N] maxLength A[1]+K-A[N] A[1]+K A[1] A 1 K A[N] A N System.out.println(K-maxLength) System.out.println System.out System System.out K-maxLength K maxLength String[] args args public 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+1];
        int maxLength = 0;

        for(int i = 1; i <= N; i++){
            A[i] = sc.nextInt();
            // if(i != 1){
                if(maxLength < A[i]-A[i-1]){
                    maxLength =  A[i]-A[i-1];
                }
            // }
        }
        if(maxLength < A[1]+K-A[N]){
            maxLength =  A[1]+K-A[N];
        }

        System.out.println(K-maxLength);    
    }
} public 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+1];
        int maxLength = 0;

        for(int i = 1; i <= N; i++){
            A[i] = sc.nextInt();
            // if(i != 1){
                if(maxLength < A[i]-A[i-1]){
                    maxLength =  A[i]-A[i-1];
                }
            // }
        }
        if(maxLength < A[1]+K-A[N]){
            maxLength =  A[1]+K-A[N];
        }

        System.out.println(K-maxLength);    
    }
} Main
import java.util.Scanner;

/**
 * Main
 */
public 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+1];
        int maxLength = 0;

        for(int i = 1; i <= N; i++){
            A[i] = sc.nextInt();
            // if(i != 1){
                if(maxLength < A[i]-A[i-1]){
                    maxLength =  A[i]-A[i-1];
                }
            // }
        }
        if(maxLength < A[1]+K-A[N]){
            maxLength =  A[1]+K-A[N];
        }

        System.out.println(K-maxLength);    
    }
}