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

public class Main {
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();

        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int d = k - a[n-1] + a[0];
        int max = d;
        for (int i = 1; i < n; i++) {
            d = a[i] - a[i-1];
            max = Math.max(d, max);
        }

        System.out.println(k - max);
    }

    static class MyScanner {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 1 << 15);
        StringTokenizer tokenizer;

        String next() {
            try {
                while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                    tokenizer = new StringTokenizer(reader.readLine());
                }
            } catch (IOException ignored) {
            }
            return tokenizer.nextToken();
        }

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

        long nextLong() {
            return Long.parseLong(next());
        }
    }
} import java.io.*; io.* java import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();

        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int d = k - a[n-1] + a[0];
        int max = d;
        for (int i = 1; i < n; i++) {
            d = a[i] - a[i-1];
            max = Math.max(d, max);
        }

        System.out.println(k - max);
    }

    static class MyScanner {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 1 << 15);
        StringTokenizer tokenizer;

        String next() {
            try {
                while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                    tokenizer = new StringTokenizer(reader.readLine());
                }
            } catch (IOException ignored) {
            }
            return tokenizer.nextToken();
        }

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

        long nextLong() {
            return Long.parseLong(next());
        }
    }
} Main public static void main(String[] args) {
        MyScanner sc = new MyScanner();

        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int d = k - a[n-1] + a[0];
        int max = d;
        for (int i = 1; i < n; i++) {
            d = a[i] - a[i-1];
            max = Math.max(d, max);
        }

        System.out.println(k - max);
    } main {
        MyScanner sc = new MyScanner();

        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int d = k - a[n-1] + a[0];
        int max = d;
        for (int i = 1; i < n; i++) {
            d = a[i] - a[i-1];
            max = Math.max(d, max);
        }

        System.out.println(k - max);
    } MyScanner sc = new MyScanner(); sc new MyScanner() 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 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] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc int d = k - a[n-1] + a[0]; d k - a[n-1] + a[0] k - a[n-1] k a[n-1] a n-1 n 1 a[0] a 0 int max = d; max d for (int i = 1; i < n; i++) {
            d = a[i] - a[i-1];
            max = Math.max(d, max);
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            d = a[i] - a[i-1];
            max = Math.max(d, max);
        } {
            d = a[i] - a[i-1];
            max = Math.max(d, max);
        } d = a[i] - a[i-1] d a[i] - a[i-1] a[i] a i a[i-1] a i-1 i 1 max = Math.max(d, max) max Math.max(d, max) Math.max Math d max System.out.println(k - max) System.out.println System.out System System.out k - max k max String[] args args static class MyScanner {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 1 << 15);
        StringTokenizer tokenizer;

        String next() {
            try {
                while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                    tokenizer = new StringTokenizer(reader.readLine());
                }
            } catch (IOException ignored) {
            }
            return tokenizer.nextToken();
        }

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

        long nextLong() {
            return Long.parseLong(next());
        }
    } MyScanner BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 1 << 15); reader new BufferedReader(new InputStreamReader(System.in), 1 << 15) StringTokenizer tokenizer; tokenizer String next() {
            try {
                while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                    tokenizer = new StringTokenizer(reader.readLine());
                }
            } catch (IOException ignored) {
            }
            return tokenizer.nextToken();
        } next {
            try {
                while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                    tokenizer = new StringTokenizer(reader.readLine());
                }
            } catch (IOException ignored) {
            }
            return tokenizer.nextToken();
        } try {
                while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                    tokenizer = new StringTokenizer(reader.readLine());
                }
            } catch (IOException ignored) {
            } catch (IOException ignored) {
            } IOException ignored {
            } {
                while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                    tokenizer = new StringTokenizer(reader.readLine());
                }
            } while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                    tokenizer = new StringTokenizer(reader.readLine());
                } tokenizer == null || !tokenizer.hasMoreTokens() tokenizer == null tokenizer null !tokenizer.hasMoreTokens() tokenizer.hasMoreTokens() tokenizer.hasMoreTokens tokenizer {
                    tokenizer = new StringTokenizer(reader.readLine());
                } tokenizer = new StringTokenizer(reader.readLine()) tokenizer new StringTokenizer(reader.readLine()) return tokenizer.nextToken(); tokenizer.nextToken() tokenizer.nextToken tokenizer int nextInt() {
            return Integer.parseInt(next());
        } nextInt {
            return Integer.parseInt(next());
        } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next long nextLong() {
            return Long.parseLong(next());
        } nextLong {
            return Long.parseLong(next());
        } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next public class Main {
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();

        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int d = k - a[n-1] + a[0];
        int max = d;
        for (int i = 1; i < n; i++) {
            d = a[i] - a[i-1];
            max = Math.max(d, max);
        }

        System.out.println(k - max);
    }

    static class MyScanner {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 1 << 15);
        StringTokenizer tokenizer;

        String next() {
            try {
                while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                    tokenizer = new StringTokenizer(reader.readLine());
                }
            } catch (IOException ignored) {
            }
            return tokenizer.nextToken();
        }

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

        long nextLong() {
            return Long.parseLong(next());
        }
    }
} public class Main {
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();

        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int d = k - a[n-1] + a[0];
        int max = d;
        for (int i = 1; i < n; i++) {
            d = a[i] - a[i-1];
            max = Math.max(d, max);
        }

        System.out.println(k - max);
    }

    static class MyScanner {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 1 << 15);
        StringTokenizer tokenizer;

        String next() {
            try {
                while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                    tokenizer = new StringTokenizer(reader.readLine());
                }
            } catch (IOException ignored) {
            }
            return tokenizer.nextToken();
        }

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

        long nextLong() {
            return Long.parseLong(next());
        }
    }
} Main
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();

        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int d = k - a[n-1] + a[0];
        int max = d;
        for (int i = 1; i < n; i++) {
            d = a[i] - a[i-1];
            max = Math.max(d, max);
        }

        System.out.println(k - max);
    }

    static class MyScanner {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 1 << 15);
        StringTokenizer tokenizer;

        String next() {
            try {
                while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                    tokenizer = new StringTokenizer(reader.readLine());
                }
            } catch (IOException ignored) {
            }
            return tokenizer.nextToken();
        }

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

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