import java.util.Arrays;
import java.util.Map;
import java.util.Scanner;
public class Main {


    public static void main(String[] args) {


        Scanner scan = new Scanner(System.in);
       int K=scan.nextInt();
        int N=scan.nextInt();
        int A[]=new int [N];

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

        Arrays.sort(A);
int M=0;
for(int i1=0;i1<N-1;i1++){if(A[i1+1]-A[i1]>M){M=A[i1+1]-A[i1];}}
if(A[0]+K-A[N-1]>M){M=A[0]+K-A[N-1];}
int L=K-M;
        System.out.println(L);


    }

}
 import java.util.Arrays; Arrays java.util import java.util.Map; Map java.util import java.util.Scanner; Scanner java.util public class Main {


    public static void main(String[] args) {


        Scanner scan = new Scanner(System.in);
       int K=scan.nextInt();
        int N=scan.nextInt();
        int A[]=new int [N];

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

        Arrays.sort(A);
int M=0;
for(int i1=0;i1<N-1;i1++){if(A[i1+1]-A[i1]>M){M=A[i1+1]-A[i1];}}
if(A[0]+K-A[N-1]>M){M=A[0]+K-A[N-1];}
int L=K-M;
        System.out.println(L);


    }

} Main public static void main(String[] args) {


        Scanner scan = new Scanner(System.in);
       int K=scan.nextInt();
        int N=scan.nextInt();
        int A[]=new int [N];

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

        Arrays.sort(A);
int M=0;
for(int i1=0;i1<N-1;i1++){if(A[i1+1]-A[i1]>M){M=A[i1+1]-A[i1];}}
if(A[0]+K-A[N-1]>M){M=A[0]+K-A[N-1];}
int L=K-M;
        System.out.println(L);


    } main {


        Scanner scan = new Scanner(System.in);
       int K=scan.nextInt();
        int N=scan.nextInt();
        int A[]=new int [N];

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

        Arrays.sort(A);
int M=0;
for(int i1=0;i1<N-1;i1++){if(A[i1+1]-A[i1]>M){M=A[i1+1]-A[i1];}}
if(A[0]+K-A[N-1]>M){M=A[0]+K-A[N-1];}
int L=K-M;
        System.out.println(L);


    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int K=scan.nextInt(); K scan.nextInt() scan.nextInt scan int N=scan.nextInt(); N scan.nextInt() scan.nextInt scan int A[]=new int [N]; A new int [N] N for(int i=0;i<N;i++){A[i]=scan.nextInt();} int i=0; int i=0; i 0 i<N i N i++ i++ i {A[i]=scan.nextInt();} {A[i]=scan.nextInt();} A[i]=scan.nextInt() A[i] A i scan.nextInt() scan.nextInt scan Arrays.sort(A) Arrays.sort Arrays A int M=0; M 0 for(int i1=0;i1<N-1;i1++){if(A[i1+1]-A[i1]>M){M=A[i1+1]-A[i1];}} int i1=0; int i1=0; i1 0 i1<N-1 i1 N-1 N 1 i1++ i1++ i1 {if(A[i1+1]-A[i1]>M){M=A[i1+1]-A[i1];}} {if(A[i1+1]-A[i1]>M){M=A[i1+1]-A[i1];}} if(A[i1+1]-A[i1]>M){M=A[i1+1]-A[i1];} A[i1+1]-A[i1]>M A[i1+1]-A[i1] A[i1+1] A i1+1 i1 1 A[i1] A i1 M {M=A[i1+1]-A[i1];} M=A[i1+1]-A[i1] M A[i1+1]-A[i1] A[i1+1] A i1+1 i1 1 A[i1] A i1 if(A[0]+K-A[N-1]>M){M=A[0]+K-A[N-1];} A[0]+K-A[N-1]>M A[0]+K-A[N-1] A[0]+K A[0] A 0 K A[N-1] A N-1 N 1 M {M=A[0]+K-A[N-1];} M=A[0]+K-A[N-1] M A[0]+K-A[N-1] A[0]+K A[0] A 0 K A[N-1] A N-1 N 1 int L=K-M; L K-M K M System.out.println(L) System.out.println System.out System System.out L String[] args args public class Main {


    public static void main(String[] args) {


        Scanner scan = new Scanner(System.in);
       int K=scan.nextInt();
        int N=scan.nextInt();
        int A[]=new int [N];

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

        Arrays.sort(A);
int M=0;
for(int i1=0;i1<N-1;i1++){if(A[i1+1]-A[i1]>M){M=A[i1+1]-A[i1];}}
if(A[0]+K-A[N-1]>M){M=A[0]+K-A[N-1];}
int L=K-M;
        System.out.println(L);


    }

} public class Main {


    public static void main(String[] args) {


        Scanner scan = new Scanner(System.in);
       int K=scan.nextInt();
        int N=scan.nextInt();
        int A[]=new int [N];

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

        Arrays.sort(A);
int M=0;
for(int i1=0;i1<N-1;i1++){if(A[i1+1]-A[i1]>M){M=A[i1+1]-A[i1];}}
if(A[0]+K-A[N-1]>M){M=A[0]+K-A[N-1];}
int L=K-M;
        System.out.println(L);


    }

} Main
import java.util.Arrays;
import java.util.Map;
import java.util.Scanner;
public class Main {


    public static void main(String[] args) {


        Scanner scan = new Scanner(System.in);
       int K=scan.nextInt();
        int N=scan.nextInt();
        int A[]=new int [N];

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

        Arrays.sort(A);
int M=0;
for(int i1=0;i1<N-1;i1++){if(A[i1+1]-A[i1]>M){M=A[i1+1]-A[i1];}}
if(A[0]+K-A[N-1]>M){M=A[0]+K-A[N-1];}
int L=K-M;
        System.out.println(L);


    }

}

