import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    static long TIME_START, TIME_END;

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
        TIME_START = System.currentTimeMillis();
        solve(sc, pw);
        TIME_END = System.currentTimeMillis();
        pw.close();
        System.err.println("Time used: " + (TIME_END - TIME_START) + ".");

    }

    static void solve(Scanner sc, PrintWriter pw) throws IOException {
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];

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

        // 5 10 15
        // 20-15 + 5 , 5 , 5
        // 0 5 15
        // 20-15 + 0 5 10

        int[] diff = new int[n];
        diff[0] = k - a[n - 1] + a[0];
        for (int i = 1; i < n; i++)
            diff[i] = a[i] - a[i - 1];

        Arrays.sort(diff);
        int sum = 0;
        for (int i = 0; i < n - 1; i++)
            sum += diff[i];

        pw.println(sum);

    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public Scanner(FileReader s) throws FileNotFoundException {
            br = new BufferedReader(s);
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

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

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

        public String nextLine() throws IOException {
            return br.readLine();
        }

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

        public boolean ready() throws IOException {
            return br.ready();
        }
    }

} import java.io.*; io.* java import java.util.Arrays; Arrays java.util import java.util.StringTokenizer; StringTokenizer java.util public class Main {
    static long TIME_START, TIME_END;

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
        TIME_START = System.currentTimeMillis();
        solve(sc, pw);
        TIME_END = System.currentTimeMillis();
        pw.close();
        System.err.println("Time used: " + (TIME_END - TIME_START) + ".");

    }

    static void solve(Scanner sc, PrintWriter pw) throws IOException {
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];

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

        // 5 10 15
        // 20-15 + 5 , 5 , 5
        // 0 5 15
        // 20-15 + 0 5 10

        int[] diff = new int[n];
        diff[0] = k - a[n - 1] + a[0];
        for (int i = 1; i < n; i++)
            diff[i] = a[i] - a[i - 1];

        Arrays.sort(diff);
        int sum = 0;
        for (int i = 0; i < n - 1; i++)
            sum += diff[i];

        pw.println(sum);

    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public Scanner(FileReader s) throws FileNotFoundException {
            br = new BufferedReader(s);
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

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

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

        public String nextLine() throws IOException {
            return br.readLine();
        }

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

        public boolean ready() throws IOException {
            return br.ready();
        }
    }

} Main static long TIME_START TIME_START TIME_END; TIME_END public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
        TIME_START = System.currentTimeMillis();
        solve(sc, pw);
        TIME_END = System.currentTimeMillis();
        pw.close();
        System.err.println("Time used: " + (TIME_END - TIME_START) + ".");

    } main {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
        TIME_START = System.currentTimeMillis();
        solve(sc, pw);
        TIME_END = System.currentTimeMillis();
        pw.close();
        System.err.println("Time used: " + (TIME_END - TIME_START) + ".");

    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out)); pw new PrintWriter(new BufferedOutputStream(System.out)) TIME_START = System.currentTimeMillis() TIME_START System.currentTimeMillis() System.currentTimeMillis System solve(sc, pw) solve sc pw TIME_END = System.currentTimeMillis() TIME_END System.currentTimeMillis() System.currentTimeMillis System pw.close() pw.close pw System.err.println("Time used: " + (TIME_END - TIME_START) + ".") System.err.println System.err System System.err "Time used: " + (TIME_END - TIME_START) + "." "Time used: " + (TIME_END - TIME_START) + "." "Time used: " (TIME_END - TIME_START) TIME_END TIME_START "." String[] args args static void solve(Scanner sc, PrintWriter pw) throws IOException {
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];

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

        // 5 10 15
        // 20-15 + 5 , 5 , 5
        // 0 5 15
        // 20-15 + 0 5 10

        int[] diff = new int[n];
        diff[0] = k - a[n - 1] + a[0];
        for (int i = 1; i < n; i++)
            diff[i] = a[i] - a[i - 1];

        Arrays.sort(diff);
        int sum = 0;
        for (int i = 0; i < n - 1; i++)
            sum += diff[i];

        pw.println(sum);

    } solve {
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];

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

        // 5 10 15
        // 20-15 + 5 , 5 , 5
        // 0 5 15
        // 20-15 + 0 5 10

        int[] diff = new int[n];
        diff[0] = k - a[n - 1] + a[0];
        for (int i = 1; i < n; i++)
            diff[i] = a[i] - a[i - 1];

        Arrays.sort(diff);
        int sum = 0;
        for (int i = 0; i < n - 1; i++)
            sum += diff[i];

        pw.println(sum);

    } 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] a i sc.nextInt() sc.nextInt sc int[] diff = new int[n]; diff new int[n] n diff[0] = k - a[n - 1] + a[0] diff[0] diff 0 k - a[n - 1] + a[0] k - a[n - 1] k a[n - 1] a n - 1 n 1 a[0] a 0 for (int i = 1; i < n; i++)
            diff[i] = a[i] - a[i - 1]; int i = 1; int i = 1; i 1 i < n i n i++ i++ i diff[i] = a[i] - a[i - 1]; diff[i] = a[i] - a[i - 1] diff[i] diff i a[i] - a[i - 1] a[i] a i a[i - 1] a i - 1 i 1 Arrays.sort(diff) Arrays.sort Arrays diff int sum = 0; sum 0 for (int i = 0; i < n - 1; i++)
            sum += diff[i]; int i = 0; int i = 0; i 0 i < n - 1 i n - 1 n 1 i++ i++ i sum += diff[i]; sum += diff[i] sum diff[i] diff i pw.println(sum) pw.println pw sum Scanner sc sc PrintWriter pw pw static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public Scanner(FileReader s) throws FileNotFoundException {
            br = new BufferedReader(s);
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

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

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

        public String nextLine() throws IOException {
            return br.readLine();
        }

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

        public boolean ready() throws IOException {
            return br.ready();
        }
    } Scanner StringTokenizer st; st BufferedReader br; br public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        } Scanner {
            br = new BufferedReader(new InputStreamReader(s));
        } br = new BufferedReader(new InputStreamReader(s)) br new BufferedReader(new InputStreamReader(s)) InputStream s s public Scanner(FileReader s) throws FileNotFoundException {
            br = new BufferedReader(s);
        } Scanner {
            br = new BufferedReader(s);
        } br = new BufferedReader(s) br new BufferedReader(s) FileReader s s public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        } next {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        } while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine()); st == null || !st.hasMoreTokens() st == null st null !st.hasMoreTokens() st.hasMoreTokens() st.hasMoreTokens st st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st public int nextInt() throws IOException {
            return Integer.parseInt(next());
        } nextInt {
            return Integer.parseInt(next());
        } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next public long nextLong() throws IOException {
            return Long.parseLong(next());
        } nextLong {
            return Long.parseLong(next());
        } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next public String nextLine() throws IOException {
            return br.readLine();
        } nextLine {
            return br.readLine();
        } return br.readLine(); br.readLine() br.readLine br public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        } nextDouble {
            return Double.parseDouble(next());
        } return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next public boolean ready() throws IOException {
            return br.ready();
        } ready {
            return br.ready();
        } return br.ready(); br.ready() br.ready br public class Main {
    static long TIME_START, TIME_END;

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
        TIME_START = System.currentTimeMillis();
        solve(sc, pw);
        TIME_END = System.currentTimeMillis();
        pw.close();
        System.err.println("Time used: " + (TIME_END - TIME_START) + ".");

    }

    static void solve(Scanner sc, PrintWriter pw) throws IOException {
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];

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

        // 5 10 15
        // 20-15 + 5 , 5 , 5
        // 0 5 15
        // 20-15 + 0 5 10

        int[] diff = new int[n];
        diff[0] = k - a[n - 1] + a[0];
        for (int i = 1; i < n; i++)
            diff[i] = a[i] - a[i - 1];

        Arrays.sort(diff);
        int sum = 0;
        for (int i = 0; i < n - 1; i++)
            sum += diff[i];

        pw.println(sum);

    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public Scanner(FileReader s) throws FileNotFoundException {
            br = new BufferedReader(s);
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

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

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

        public String nextLine() throws IOException {
            return br.readLine();
        }

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

        public boolean ready() throws IOException {
            return br.ready();
        }
    }

} public class Main {
    static long TIME_START, TIME_END;

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
        TIME_START = System.currentTimeMillis();
        solve(sc, pw);
        TIME_END = System.currentTimeMillis();
        pw.close();
        System.err.println("Time used: " + (TIME_END - TIME_START) + ".");

    }

    static void solve(Scanner sc, PrintWriter pw) throws IOException {
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];

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

        // 5 10 15
        // 20-15 + 5 , 5 , 5
        // 0 5 15
        // 20-15 + 0 5 10

        int[] diff = new int[n];
        diff[0] = k - a[n - 1] + a[0];
        for (int i = 1; i < n; i++)
            diff[i] = a[i] - a[i - 1];

        Arrays.sort(diff);
        int sum = 0;
        for (int i = 0; i < n - 1; i++)
            sum += diff[i];

        pw.println(sum);

    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public Scanner(FileReader s) throws FileNotFoundException {
            br = new BufferedReader(s);
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

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

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

        public String nextLine() throws IOException {
            return br.readLine();
        }

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

        public boolean ready() throws IOException {
            return br.ready();
        }
    }

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

public class Main {
    static long TIME_START, TIME_END;

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
        TIME_START = System.currentTimeMillis();
        solve(sc, pw);
        TIME_END = System.currentTimeMillis();
        pw.close();
        System.err.println("Time used: " + (TIME_END - TIME_START) + ".");

    }

    static void solve(Scanner sc, PrintWriter pw) throws IOException {
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];

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

        // 5 10 15
        // 20-15 + 5 , 5 , 5
        // 0 5 15
        // 20-15 + 0 5 10

        int[] diff = new int[n];
        diff[0] = k - a[n - 1] + a[0];
        for (int i = 1; i < n; i++)
            diff[i] = a[i] - a[i - 1];

        Arrays.sort(diff);
        int sum = 0;
        for (int i = 0; i < n - 1; i++)
            sum += diff[i];

        pw.println(sum);

    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public Scanner(FileReader s) throws FileNotFoundException {
            br = new BufferedReader(s);
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

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

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

        public String nextLine() throws IOException {
            return br.readLine();
        }

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

        public boolean ready() throws IOException {
            return br.ready();
        }
    }

}
