import java.io.*;
import java.lang.*;
import java.util.*;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] s=br.readLine().split(" ");
        int K=Integer.parseInt(s[0]);
        int N=Integer.parseInt(s[1]);
        int arr[]=new int[2*N];
        StringTokenizer st=new StringTokenizer(br.readLine()," ");
        for(int i=0;st.hasMoreTokens();i++){
            arr[i]=Integer.parseInt(st.nextToken());
            arr[N+i]=K+arr[i];
        }
        int ans=Integer.MAX_VALUE;
        for(int i=0;i<N;i++){
            int dist=arr[N-1+i]-arr[i];
            if(dist<ans) ans=dist;
        }
        System.out.println(ans);
    }
} import java.io.*; io.* java import java.lang.*; lang.* java import java.util.*; util.* java public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] s=br.readLine().split(" ");
        int K=Integer.parseInt(s[0]);
        int N=Integer.parseInt(s[1]);
        int arr[]=new int[2*N];
        StringTokenizer st=new StringTokenizer(br.readLine()," ");
        for(int i=0;st.hasMoreTokens();i++){
            arr[i]=Integer.parseInt(st.nextToken());
            arr[N+i]=K+arr[i];
        }
        int ans=Integer.MAX_VALUE;
        for(int i=0;i<N;i++){
            int dist=arr[N-1+i]-arr[i];
            if(dist<ans) ans=dist;
        }
        System.out.println(ans);
    }
} Main public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] s=br.readLine().split(" ");
        int K=Integer.parseInt(s[0]);
        int N=Integer.parseInt(s[1]);
        int arr[]=new int[2*N];
        StringTokenizer st=new StringTokenizer(br.readLine()," ");
        for(int i=0;st.hasMoreTokens();i++){
            arr[i]=Integer.parseInt(st.nextToken());
            arr[N+i]=K+arr[i];
        }
        int ans=Integer.MAX_VALUE;
        for(int i=0;i<N;i++){
            int dist=arr[N-1+i]-arr[i];
            if(dist<ans) ans=dist;
        }
        System.out.println(ans);
    } main {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] s=br.readLine().split(" ");
        int K=Integer.parseInt(s[0]);
        int N=Integer.parseInt(s[1]);
        int arr[]=new int[2*N];
        StringTokenizer st=new StringTokenizer(br.readLine()," ");
        for(int i=0;st.hasMoreTokens();i++){
            arr[i]=Integer.parseInt(st.nextToken());
            arr[N+i]=K+arr[i];
        }
        int ans=Integer.MAX_VALUE;
        for(int i=0;i<N;i++){
            int dist=arr[N-1+i]-arr[i];
            if(dist<ans) ans=dist;
        }
        System.out.println(ans);
    } BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] s=br.readLine().split(" "); s br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int K=Integer.parseInt(s[0]); K Integer.parseInt(s[0]) Integer.parseInt Integer s[0] s 0 int N=Integer.parseInt(s[1]); N Integer.parseInt(s[1]) Integer.parseInt Integer s[1] s 1 int arr[]=new int[2*N]; arr new int[2*N] 2*N 2 N StringTokenizer st=new StringTokenizer(br.readLine()," "); st new StringTokenizer(br.readLine()," ") for(int i=0;st.hasMoreTokens();i++){
            arr[i]=Integer.parseInt(st.nextToken());
            arr[N+i]=K+arr[i];
        } int i=0; int i=0; i 0 st.hasMoreTokens() st.hasMoreTokens st i++ i++ i {
            arr[i]=Integer.parseInt(st.nextToken());
            arr[N+i]=K+arr[i];
        } {
            arr[i]=Integer.parseInt(st.nextToken());
            arr[N+i]=K+arr[i];
        } arr[i]=Integer.parseInt(st.nextToken()) arr[i] arr i Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st arr[N+i]=K+arr[i] arr[N+i] arr N+i N i K+arr[i] K arr[i] arr i int ans=Integer.MAX_VALUE; ans Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int i=0;i<N;i++){
            int dist=arr[N-1+i]-arr[i];
            if(dist<ans) ans=dist;
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            int dist=arr[N-1+i]-arr[i];
            if(dist<ans) ans=dist;
        } {
            int dist=arr[N-1+i]-arr[i];
            if(dist<ans) ans=dist;
        } int dist=arr[N-1+i]-arr[i]; dist arr[N-1+i]-arr[i] arr[N-1+i] arr N-1+i N-1 N 1 i arr[i] arr i if(dist<ans) ans=dist; dist<ans dist ans ans=dist ans dist System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] s=br.readLine().split(" ");
        int K=Integer.parseInt(s[0]);
        int N=Integer.parseInt(s[1]);
        int arr[]=new int[2*N];
        StringTokenizer st=new StringTokenizer(br.readLine()," ");
        for(int i=0;st.hasMoreTokens();i++){
            arr[i]=Integer.parseInt(st.nextToken());
            arr[N+i]=K+arr[i];
        }
        int ans=Integer.MAX_VALUE;
        for(int i=0;i<N;i++){
            int dist=arr[N-1+i]-arr[i];
            if(dist<ans) ans=dist;
        }
        System.out.println(ans);
    }
} public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] s=br.readLine().split(" ");
        int K=Integer.parseInt(s[0]);
        int N=Integer.parseInt(s[1]);
        int arr[]=new int[2*N];
        StringTokenizer st=new StringTokenizer(br.readLine()," ");
        for(int i=0;st.hasMoreTokens();i++){
            arr[i]=Integer.parseInt(st.nextToken());
            arr[N+i]=K+arr[i];
        }
        int ans=Integer.MAX_VALUE;
        for(int i=0;i<N;i++){
            int dist=arr[N-1+i]-arr[i];
            if(dist<ans) ans=dist;
        }
        System.out.println(ans);
    }
} Main
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] s=br.readLine().split(" ");
        int K=Integer.parseInt(s[0]);
        int N=Integer.parseInt(s[1]);
        int arr[]=new int[2*N];
        StringTokenizer st=new StringTokenizer(br.readLine()," ");
        for(int i=0;st.hasMoreTokens();i++){
            arr[i]=Integer.parseInt(st.nextToken());
            arr[N+i]=K+arr[i];
        }
        int ans=Integer.MAX_VALUE;
        for(int i=0;i<N;i++){
            int dist=arr[N-1+i]-arr[i];
            if(dist<ans) ans=dist;
        }
        System.out.println(ans);
    }
}