import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        abc160_c solver = new abc160_c();
        solver.solve(1, in, out);
        out.close();
    }

    static class abc160_c {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int K = in.nextInt();
            int N = in.nextInt();
            long A[] = new long[N];
            long seg[] = new long[N];
            long max_far = 0;
            long max_far_seg = -1;
            for (int i = 0; i < N; i++) {
                A[i] = in.nextInt();
            }
            max_far = (K - A[N - 1]) + A[0];
            max_far_seg = 0;
            seg[0] = max_far;
            for (int i = 1; i < N; i++) {
                long far = A[i] - A[i - 1];
                seg[i] = far;
                if (max_far < far) {
                    max_far = far;
                    max_far_seg = i;
                }
            }
            long ttldist = 0;
            for (int i = 0; i < N; i++) {
                if (max_far_seg != i) {
                    ttldist += seg[i];
                }
            }
            out.println(ttldist);
        }

    }
}
 import java.io.OutputStream; OutputStream java.io import java.io.IOException; IOException java.io import java.io.InputStream; InputStream java.io import java.io.PrintWriter; PrintWriter java.io import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        abc160_c solver = new abc160_c();
        solver.solve(1, in, out);
        out.close();
    }

    static class abc160_c {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int K = in.nextInt();
            int N = in.nextInt();
            long A[] = new long[N];
            long seg[] = new long[N];
            long max_far = 0;
            long max_far_seg = -1;
            for (int i = 0; i < N; i++) {
                A[i] = in.nextInt();
            }
            max_far = (K - A[N - 1]) + A[0];
            max_far_seg = 0;
            seg[0] = max_far;
            for (int i = 1; i < N; i++) {
                long far = A[i] - A[i - 1];
                seg[i] = far;
                if (max_far < far) {
                    max_far = far;
                    max_far_seg = i;
                }
            }
            long ttldist = 0;
            for (int i = 0; i < N; i++) {
                if (max_far_seg != i) {
                    ttldist += seg[i];
                }
            }
            out.println(ttldist);
        }

    }
} Main public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        abc160_c solver = new abc160_c();
        solver.solve(1, in, out);
        out.close();
    } main {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        abc160_c solver = new abc160_c();
        solver.solve(1, in, out);
        out.close();
    } InputStream inputStream = System.in; inputStream System.in System System.in OutputStream outputStream = System.out; outputStream System.out System System.out Scanner in = new Scanner(inputStream); in new Scanner(inputStream) PrintWriter out = new PrintWriter(outputStream); out new PrintWriter(outputStream) abc160_c solver = new abc160_c(); solver new abc160_c() solver.solve(1, in, out) solver.solve solver 1 in out out.close() out.close out String[] args args static class abc160_c {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int K = in.nextInt();
            int N = in.nextInt();
            long A[] = new long[N];
            long seg[] = new long[N];
            long max_far = 0;
            long max_far_seg = -1;
            for (int i = 0; i < N; i++) {
                A[i] = in.nextInt();
            }
            max_far = (K - A[N - 1]) + A[0];
            max_far_seg = 0;
            seg[0] = max_far;
            for (int i = 1; i < N; i++) {
                long far = A[i] - A[i - 1];
                seg[i] = far;
                if (max_far < far) {
                    max_far = far;
                    max_far_seg = i;
                }
            }
            long ttldist = 0;
            for (int i = 0; i < N; i++) {
                if (max_far_seg != i) {
                    ttldist += seg[i];
                }
            }
            out.println(ttldist);
        }

    } abc160_c public void solve(int testNumber, Scanner in, PrintWriter out) {
            int K = in.nextInt();
            int N = in.nextInt();
            long A[] = new long[N];
            long seg[] = new long[N];
            long max_far = 0;
            long max_far_seg = -1;
            for (int i = 0; i < N; i++) {
                A[i] = in.nextInt();
            }
            max_far = (K - A[N - 1]) + A[0];
            max_far_seg = 0;
            seg[0] = max_far;
            for (int i = 1; i < N; i++) {
                long far = A[i] - A[i - 1];
                seg[i] = far;
                if (max_far < far) {
                    max_far = far;
                    max_far_seg = i;
                }
            }
            long ttldist = 0;
            for (int i = 0; i < N; i++) {
                if (max_far_seg != i) {
                    ttldist += seg[i];
                }
            }
            out.println(ttldist);
        } solve {
            int K = in.nextInt();
            int N = in.nextInt();
            long A[] = new long[N];
            long seg[] = new long[N];
            long max_far = 0;
            long max_far_seg = -1;
            for (int i = 0; i < N; i++) {
                A[i] = in.nextInt();
            }
            max_far = (K - A[N - 1]) + A[0];
            max_far_seg = 0;
            seg[0] = max_far;
            for (int i = 1; i < N; i++) {
                long far = A[i] - A[i - 1];
                seg[i] = far;
                if (max_far < far) {
                    max_far = far;
                    max_far_seg = i;
                }
            }
            long ttldist = 0;
            for (int i = 0; i < N; i++) {
                if (max_far_seg != i) {
                    ttldist += seg[i];
                }
            }
            out.println(ttldist);
        } int K = in.nextInt(); K in.nextInt() in.nextInt in int N = in.nextInt(); N in.nextInt() in.nextInt in long A[] = new long[N]; A new long[N] N long seg[] = new long[N]; seg new long[N] N long max_far = 0; max_far 0 long max_far_seg = -1; max_far_seg -1 1 for (int i = 0; i < N; i++) {
                A[i] = in.nextInt();
            } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
                A[i] = in.nextInt();
            } {
                A[i] = in.nextInt();
            } A[i] = in.nextInt() A[i] A i in.nextInt() in.nextInt in max_far = (K - A[N - 1]) + A[0] max_far (K - A[N - 1]) + A[0] (K - A[N - 1]) K A[N - 1] A N - 1 N 1 A[0] A 0 max_far_seg = 0 max_far_seg 0 seg[0] = max_far seg[0] seg 0 max_far for (int i = 1; i < N; i++) {
                long far = A[i] - A[i - 1];
                seg[i] = far;
                if (max_far < far) {
                    max_far = far;
                    max_far_seg = i;
                }
            } int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
                long far = A[i] - A[i - 1];
                seg[i] = far;
                if (max_far < far) {
                    max_far = far;
                    max_far_seg = i;
                }
            } {
                long far = A[i] - A[i - 1];
                seg[i] = far;
                if (max_far < far) {
                    max_far = far;
                    max_far_seg = i;
                }
            } long far = A[i] - A[i - 1]; far A[i] - A[i - 1] A[i] A i A[i - 1] A i - 1 i 1 seg[i] = far seg[i] seg i far if (max_far < far) {
                    max_far = far;
                    max_far_seg = i;
                } max_far < far max_far far {
                    max_far = far;
                    max_far_seg = i;
                } max_far = far max_far far max_far_seg = i max_far_seg i long ttldist = 0; ttldist 0 for (int i = 0; i < N; i++) {
                if (max_far_seg != i) {
                    ttldist += seg[i];
                }
            } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
                if (max_far_seg != i) {
                    ttldist += seg[i];
                }
            } {
                if (max_far_seg != i) {
                    ttldist += seg[i];
                }
            } if (max_far_seg != i) {
                    ttldist += seg[i];
                } max_far_seg != i max_far_seg i {
                    ttldist += seg[i];
                } ttldist += seg[i] ttldist seg[i] seg i out.println(ttldist) out.println out ttldist int testNumber testNumber Scanner in in PrintWriter out out public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        abc160_c solver = new abc160_c();
        solver.solve(1, in, out);
        out.close();
    }

    static class abc160_c {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int K = in.nextInt();
            int N = in.nextInt();
            long A[] = new long[N];
            long seg[] = new long[N];
            long max_far = 0;
            long max_far_seg = -1;
            for (int i = 0; i < N; i++) {
                A[i] = in.nextInt();
            }
            max_far = (K - A[N - 1]) + A[0];
            max_far_seg = 0;
            seg[0] = max_far;
            for (int i = 1; i < N; i++) {
                long far = A[i] - A[i - 1];
                seg[i] = far;
                if (max_far < far) {
                    max_far = far;
                    max_far_seg = i;
                }
            }
            long ttldist = 0;
            for (int i = 0; i < N; i++) {
                if (max_far_seg != i) {
                    ttldist += seg[i];
                }
            }
            out.println(ttldist);
        }

    }
} public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        abc160_c solver = new abc160_c();
        solver.solve(1, in, out);
        out.close();
    }

    static class abc160_c {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int K = in.nextInt();
            int N = in.nextInt();
            long A[] = new long[N];
            long seg[] = new long[N];
            long max_far = 0;
            long max_far_seg = -1;
            for (int i = 0; i < N; i++) {
                A[i] = in.nextInt();
            }
            max_far = (K - A[N - 1]) + A[0];
            max_far_seg = 0;
            seg[0] = max_far;
            for (int i = 1; i < N; i++) {
                long far = A[i] - A[i - 1];
                seg[i] = far;
                if (max_far < far) {
                    max_far = far;
                    max_far_seg = i;
                }
            }
            long ttldist = 0;
            for (int i = 0; i < N; i++) {
                if (max_far_seg != i) {
                    ttldist += seg[i];
                }
            }
            out.println(ttldist);
        }

    }
} Main
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        abc160_c solver = new abc160_c();
        solver.solve(1, in, out);
        out.close();
    }

    static class abc160_c {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int K = in.nextInt();
            int N = in.nextInt();
            long A[] = new long[N];
            long seg[] = new long[N];
            long max_far = 0;
            long max_far_seg = -1;
            for (int i = 0; i < N; i++) {
                A[i] = in.nextInt();
            }
            max_far = (K - A[N - 1]) + A[0];
            max_far_seg = 0;
            seg[0] = max_far;
            for (int i = 1; i < N; i++) {
                long far = A[i] - A[i - 1];
                seg[i] = far;
                if (max_far < far) {
                    max_far = far;
                    max_far_seg = i;
                }
            }
            long ttldist = 0;
            for (int i = 0; i < N; i++) {
                if (max_far_seg != i) {
                    ttldist += seg[i];
                }
            }
            out.println(ttldist);
        }

    }
}

