import java.util.*;
import java.io.*;

public class Main {	
	public static void main(String[] args) {
		FastScanner fs = new FastScanner();
        int K = fs.nextInt();
        int N = fs.nextInt();
        List<Integer> list = new ArrayList<>();
        long max = 0;

        for(int i=0; i<N; i++){
            int dis = fs.nextInt();
            list.add(dis);
        }

        if(list.contains(0)){
            max = K-list.get(N-1);
        }else{
            max = K-list.get(N-1)+list.get(0);
        }

        for(int i=1; i<N; i++){
            long distance = list.get(i)-list.get(i-1);
            if(distance>max){
                max = distance;
            }
        }
        
		System.out.println(K-max);
	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");

		public String next() {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) {
				a[i]=nextInt();
			}
			return a;
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}

	}
} import java.util.*; util.* java import java.io.*; io.* java public class Main {	
	public static void main(String[] args) {
		FastScanner fs = new FastScanner();
        int K = fs.nextInt();
        int N = fs.nextInt();
        List<Integer> list = new ArrayList<>();
        long max = 0;

        for(int i=0; i<N; i++){
            int dis = fs.nextInt();
            list.add(dis);
        }

        if(list.contains(0)){
            max = K-list.get(N-1);
        }else{
            max = K-list.get(N-1)+list.get(0);
        }

        for(int i=1; i<N; i++){
            long distance = list.get(i)-list.get(i-1);
            if(distance>max){
                max = distance;
            }
        }
        
		System.out.println(K-max);
	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");

		public String next() {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) {
				a[i]=nextInt();
			}
			return a;
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}

	}
} Main public static void main(String[] args) {
		FastScanner fs = new FastScanner();
        int K = fs.nextInt();
        int N = fs.nextInt();
        List<Integer> list = new ArrayList<>();
        long max = 0;

        for(int i=0; i<N; i++){
            int dis = fs.nextInt();
            list.add(dis);
        }

        if(list.contains(0)){
            max = K-list.get(N-1);
        }else{
            max = K-list.get(N-1)+list.get(0);
        }

        for(int i=1; i<N; i++){
            long distance = list.get(i)-list.get(i-1);
            if(distance>max){
                max = distance;
            }
        }
        
		System.out.println(K-max);
	} main {
		FastScanner fs = new FastScanner();
        int K = fs.nextInt();
        int N = fs.nextInt();
        List<Integer> list = new ArrayList<>();
        long max = 0;

        for(int i=0; i<N; i++){
            int dis = fs.nextInt();
            list.add(dis);
        }

        if(list.contains(0)){
            max = K-list.get(N-1);
        }else{
            max = K-list.get(N-1)+list.get(0);
        }

        for(int i=1; i<N; i++){
            long distance = list.get(i)-list.get(i-1);
            if(distance>max){
                max = distance;
            }
        }
        
		System.out.println(K-max);
	} FastScanner fs = new FastScanner(); fs new FastScanner() int K = fs.nextInt(); K fs.nextInt() fs.nextInt fs int N = fs.nextInt(); N fs.nextInt() fs.nextInt fs List<Integer> list = new ArrayList<>(); list new ArrayList<>() long max = 0; max 0 for(int i=0; i<N; i++){
            int dis = fs.nextInt();
            list.add(dis);
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            int dis = fs.nextInt();
            list.add(dis);
        } {
            int dis = fs.nextInt();
            list.add(dis);
        } int dis = fs.nextInt(); dis fs.nextInt() fs.nextInt fs list.add(dis) list.add list dis if(list.contains(0)){
            max = K-list.get(N-1);
        }else{
            max = K-list.get(N-1)+list.get(0);
        } list.contains(0) list.contains list 0 {
            max = K-list.get(N-1);
        } max = K-list.get(N-1) max K-list.get(N-1) K list.get(N-1) list.get list N-1 N 1 {
            max = K-list.get(N-1)+list.get(0);
        } max = K-list.get(N-1)+list.get(0) max K-list.get(N-1)+list.get(0) K-list.get(N-1) K list.get(N-1) list.get list N-1 N 1 list.get(0) list.get list 0 for(int i=1; i<N; i++){
            long distance = list.get(i)-list.get(i-1);
            if(distance>max){
                max = distance;
            }
        } int i=1; int i=1; i 1 i<N i N i++ i++ i {
            long distance = list.get(i)-list.get(i-1);
            if(distance>max){
                max = distance;
            }
        } {
            long distance = list.get(i)-list.get(i-1);
            if(distance>max){
                max = distance;
            }
        } long distance = list.get(i)-list.get(i-1); distance list.get(i)-list.get(i-1) list.get(i) list.get list i list.get(i-1) list.get list i-1 i 1 if(distance>max){
                max = distance;
            } distance>max distance max {
                max = distance;
            } max = distance max distance System.out.println(K-max) System.out.println System.out System System.out K-max K max String[] args args static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");

		public String next() {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) {
				a[i]=nextInt();
			}
			return a;
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}

	} FastScanner BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) StringTokenizer st=new StringTokenizer(""); st new StringTokenizer("") public String next() {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		} next {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		} while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				} !st.hasMoreElements() st.hasMoreElements() st.hasMoreElements st try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} IOException e {
					e.printStackTrace();
				} e.printStackTrace() e.printStackTrace e {
					st=new StringTokenizer(br.readLine());
				} st=new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st public int nextInt() {
			return Integer.parseInt(next());
		} nextInt {
			return Integer.parseInt(next());
		} return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next public long nextLong() {
			return Long.parseLong(next());
		} nextLong {
			return Long.parseLong(next());
		} return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next public int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) {
				a[i]=nextInt();
			}
			return a;
		} readArray {
			int[] a=new int[n];
			for (int i=0; i<n; i++) {
				a[i]=nextInt();
			}
			return a;
		} int[] a=new int[n]; a new int[n] n for (int i=0; i<n; i++) {
				a[i]=nextInt();
			} int i=0; int i=0; i 0 i<n i n i++ i++ i {
				a[i]=nextInt();
			} {
				a[i]=nextInt();
			} a[i]=nextInt() a[i] a i nextInt() nextInt return a; a int n n public double nextDouble() {
			return Double.parseDouble(next());
		} nextDouble {
			return Double.parseDouble(next());
		} return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next public class Main {	
	public static void main(String[] args) {
		FastScanner fs = new FastScanner();
        int K = fs.nextInt();
        int N = fs.nextInt();
        List<Integer> list = new ArrayList<>();
        long max = 0;

        for(int i=0; i<N; i++){
            int dis = fs.nextInt();
            list.add(dis);
        }

        if(list.contains(0)){
            max = K-list.get(N-1);
        }else{
            max = K-list.get(N-1)+list.get(0);
        }

        for(int i=1; i<N; i++){
            long distance = list.get(i)-list.get(i-1);
            if(distance>max){
                max = distance;
            }
        }
        
		System.out.println(K-max);
	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");

		public String next() {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) {
				a[i]=nextInt();
			}
			return a;
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}

	}
} public class Main {	
	public static void main(String[] args) {
		FastScanner fs = new FastScanner();
        int K = fs.nextInt();
        int N = fs.nextInt();
        List<Integer> list = new ArrayList<>();
        long max = 0;

        for(int i=0; i<N; i++){
            int dis = fs.nextInt();
            list.add(dis);
        }

        if(list.contains(0)){
            max = K-list.get(N-1);
        }else{
            max = K-list.get(N-1)+list.get(0);
        }

        for(int i=1; i<N; i++){
            long distance = list.get(i)-list.get(i-1);
            if(distance>max){
                max = distance;
            }
        }
        
		System.out.println(K-max);
	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");

		public String next() {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) {
				a[i]=nextInt();
			}
			return a;
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}

	}
} Main
import java.util.*;
import java.io.*;

public class Main {	
	public static void main(String[] args) {
		FastScanner fs = new FastScanner();
        int K = fs.nextInt();
        int N = fs.nextInt();
        List<Integer> list = new ArrayList<>();
        long max = 0;

        for(int i=0; i<N; i++){
            int dis = fs.nextInt();
            list.add(dis);
        }

        if(list.contains(0)){
            max = K-list.get(N-1);
        }else{
            max = K-list.get(N-1)+list.get(0);
        }

        for(int i=1; i<N; i++){
            long distance = list.get(i)-list.get(i-1);
            if(distance>max){
                max = distance;
            }
        }
        
		System.out.println(K-max);
	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");

		public String next() {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) {
				a[i]=nextInt();
			}
			return a;
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}

	}
}
