import java.util.*;
class Main{
    static Scanner sc=new Scanner(System.in);
    public static void main(String[] args){
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] A =new int[N];
        int[] differ=new int[N];
        for(int i=0;i<N;i++)
            A[i] = sc.nextInt();
        Arrays.sort(A);
        for(int i=0;i<N;i++){
            if(i==(N-1))
            differ[i]=K-(A[i]-A[0]);
            else
            differ[i]=A[i+1]-A[i];
        }
        Arrays.sort(differ);
        System.out.println(K-differ[N-1]);
    }
} import java.util.*; util.* java class Main{
    static Scanner sc=new Scanner(System.in);
    public static void main(String[] args){
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] A =new int[N];
        int[] differ=new int[N];
        for(int i=0;i<N;i++)
            A[i] = sc.nextInt();
        Arrays.sort(A);
        for(int i=0;i<N;i++){
            if(i==(N-1))
            differ[i]=K-(A[i]-A[0]);
            else
            differ[i]=A[i+1]-A[i];
        }
        Arrays.sort(differ);
        System.out.println(K-differ[N-1]);
    }
} Main static Scanner sc=new Scanner(System.in); sc new Scanner(System.in) public static void main(String[] args){
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] A =new int[N];
        int[] differ=new int[N];
        for(int i=0;i<N;i++)
            A[i] = sc.nextInt();
        Arrays.sort(A);
        for(int i=0;i<N;i++){
            if(i==(N-1))
            differ[i]=K-(A[i]-A[0]);
            else
            differ[i]=A[i+1]-A[i];
        }
        Arrays.sort(differ);
        System.out.println(K-differ[N-1]);
    } main {
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] A =new int[N];
        int[] differ=new int[N];
        for(int i=0;i<N;i++)
            A[i] = sc.nextInt();
        Arrays.sort(A);
        for(int i=0;i<N;i++){
            if(i==(N-1))
            differ[i]=K-(A[i]-A[0]);
            else
            differ[i]=A[i+1]-A[i];
        }
        Arrays.sort(differ);
        System.out.println(K-differ[N-1]);
    } 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 int[] differ=new int[N]; differ 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] A i sc.nextInt() sc.nextInt sc Arrays.sort(A) Arrays.sort Arrays A for(int i=0;i<N;i++){
            if(i==(N-1))
            differ[i]=K-(A[i]-A[0]);
            else
            differ[i]=A[i+1]-A[i];
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            if(i==(N-1))
            differ[i]=K-(A[i]-A[0]);
            else
            differ[i]=A[i+1]-A[i];
        } {
            if(i==(N-1))
            differ[i]=K-(A[i]-A[0]);
            else
            differ[i]=A[i+1]-A[i];
        } if(i==(N-1))
            differ[i]=K-(A[i]-A[0]);
            else
            differ[i]=A[i+1]-A[i]; i==(N-1) i (N-1) N 1 differ[i]=K-(A[i]-A[0]) differ[i] differ i K-(A[i]-A[0]) K (A[i]-A[0]) A[i] A i A[0] A 0 differ[i]=A[i+1]-A[i] differ[i] differ i A[i+1]-A[i] A[i+1] A i+1 i 1 A[i] A i Arrays.sort(differ) Arrays.sort Arrays differ System.out.println(K-differ[N-1]) System.out.println System.out System System.out K-differ[N-1] K differ[N-1] differ N-1 N 1 String[] args args
import java.util.*;
class Main{
    static Scanner sc=new Scanner(System.in);
    public static void main(String[] args){
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] A =new int[N];
        int[] differ=new int[N];
        for(int i=0;i<N;i++)
            A[i] = sc.nextInt();
        Arrays.sort(A);
        for(int i=0;i<N;i++){
            if(i==(N-1))
            differ[i]=K-(A[i]-A[0]);
            else
            differ[i]=A[i+1]-A[i];
        }
        Arrays.sort(differ);
        System.out.println(K-differ[N-1]);
    }
}