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

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Question question = new Question();
        question.solve(sc);
    }

    static class Question {
        public void solve(Scanner sc) {
            int len = sc.nextInt();
            int n = sc.nextInt();
            int[] distances = new int[n];
            for (int i = 0; i < n; ++i) {
                distances[i] = sc.nextInt();
            }
            int m = 0;
            for (int i = 0; i < n; ++i) {
                int t = (i + 1) % n;
                int dist = distances[t] - distances[i];
                m = Math.max(m, Math.min(Math.abs(dist), len - Math.abs(dist)));
            }
            System.out.println(len - m);
        }
    }
} import java.io.*; io.* java import java.util.*; util.* java public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Question question = new Question();
        question.solve(sc);
    }

    static class Question {
        public void solve(Scanner sc) {
            int len = sc.nextInt();
            int n = sc.nextInt();
            int[] distances = new int[n];
            for (int i = 0; i < n; ++i) {
                distances[i] = sc.nextInt();
            }
            int m = 0;
            for (int i = 0; i < n; ++i) {
                int t = (i + 1) % n;
                int dist = distances[t] - distances[i];
                m = Math.max(m, Math.min(Math.abs(dist), len - Math.abs(dist)));
            }
            System.out.println(len - m);
        }
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Question question = new Question();
        question.solve(sc);
    } main {
        Scanner sc = new Scanner(System.in);
        Question question = new Question();
        question.solve(sc);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) Question question = new Question(); question new Question() question.solve(sc) question.solve question sc String[] args args static class Question {
        public void solve(Scanner sc) {
            int len = sc.nextInt();
            int n = sc.nextInt();
            int[] distances = new int[n];
            for (int i = 0; i < n; ++i) {
                distances[i] = sc.nextInt();
            }
            int m = 0;
            for (int i = 0; i < n; ++i) {
                int t = (i + 1) % n;
                int dist = distances[t] - distances[i];
                m = Math.max(m, Math.min(Math.abs(dist), len - Math.abs(dist)));
            }
            System.out.println(len - m);
        }
    } Question public void solve(Scanner sc) {
            int len = sc.nextInt();
            int n = sc.nextInt();
            int[] distances = new int[n];
            for (int i = 0; i < n; ++i) {
                distances[i] = sc.nextInt();
            }
            int m = 0;
            for (int i = 0; i < n; ++i) {
                int t = (i + 1) % n;
                int dist = distances[t] - distances[i];
                m = Math.max(m, Math.min(Math.abs(dist), len - Math.abs(dist)));
            }
            System.out.println(len - m);
        } solve {
            int len = sc.nextInt();
            int n = sc.nextInt();
            int[] distances = new int[n];
            for (int i = 0; i < n; ++i) {
                distances[i] = sc.nextInt();
            }
            int m = 0;
            for (int i = 0; i < n; ++i) {
                int t = (i + 1) % n;
                int dist = distances[t] - distances[i];
                m = Math.max(m, Math.min(Math.abs(dist), len - Math.abs(dist)));
            }
            System.out.println(len - m);
        } int len = sc.nextInt(); len sc.nextInt() sc.nextInt sc int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] distances = new int[n]; distances new int[n] n for (int i = 0; i < n; ++i) {
                distances[i] = sc.nextInt();
            } int i = 0; int i = 0; i 0 i < n i n ++i ++i i {
                distances[i] = sc.nextInt();
            } {
                distances[i] = sc.nextInt();
            } distances[i] = sc.nextInt() distances[i] distances i sc.nextInt() sc.nextInt sc int m = 0; m 0 for (int i = 0; i < n; ++i) {
                int t = (i + 1) % n;
                int dist = distances[t] - distances[i];
                m = Math.max(m, Math.min(Math.abs(dist), len - Math.abs(dist)));
            } int i = 0; int i = 0; i 0 i < n i n ++i ++i i {
                int t = (i + 1) % n;
                int dist = distances[t] - distances[i];
                m = Math.max(m, Math.min(Math.abs(dist), len - Math.abs(dist)));
            } {
                int t = (i + 1) % n;
                int dist = distances[t] - distances[i];
                m = Math.max(m, Math.min(Math.abs(dist), len - Math.abs(dist)));
            } int t = (i + 1) % n; t (i + 1) % n (i + 1) i 1 n int dist = distances[t] - distances[i]; dist distances[t] - distances[i] distances[t] distances t distances[i] distances i m = Math.max(m, Math.min(Math.abs(dist), len - Math.abs(dist))) m Math.max(m, Math.min(Math.abs(dist), len - Math.abs(dist))) Math.max Math m Math.min(Math.abs(dist), len - Math.abs(dist)) Math.min Math Math.abs(dist) Math.abs Math dist len - Math.abs(dist) len Math.abs(dist) Math.abs Math dist System.out.println(len - m) System.out.println System.out System System.out len - m len m Scanner sc sc public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Question question = new Question();
        question.solve(sc);
    }

    static class Question {
        public void solve(Scanner sc) {
            int len = sc.nextInt();
            int n = sc.nextInt();
            int[] distances = new int[n];
            for (int i = 0; i < n; ++i) {
                distances[i] = sc.nextInt();
            }
            int m = 0;
            for (int i = 0; i < n; ++i) {
                int t = (i + 1) % n;
                int dist = distances[t] - distances[i];
                m = Math.max(m, Math.min(Math.abs(dist), len - Math.abs(dist)));
            }
            System.out.println(len - m);
        }
    }
} public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Question question = new Question();
        question.solve(sc);
    }

    static class Question {
        public void solve(Scanner sc) {
            int len = sc.nextInt();
            int n = sc.nextInt();
            int[] distances = new int[n];
            for (int i = 0; i < n; ++i) {
                distances[i] = sc.nextInt();
            }
            int m = 0;
            for (int i = 0; i < n; ++i) {
                int t = (i + 1) % n;
                int dist = distances[t] - distances[i];
                m = Math.max(m, Math.min(Math.abs(dist), len - Math.abs(dist)));
            }
            System.out.println(len - m);
        }
    }
} Main
import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Question question = new Question();
        question.solve(sc);
    }

    static class Question {
        public void solve(Scanner sc) {
            int len = sc.nextInt();
            int n = sc.nextInt();
            int[] distances = new int[n];
            for (int i = 0; i < n; ++i) {
                distances[i] = sc.nextInt();
            }
            int m = 0;
            for (int i = 0; i < n; ++i) {
                int t = (i + 1) % n;
                int dist = distances[t] - distances[i];
                m = Math.max(m, Math.min(Math.abs(dist), len - Math.abs(dist)));
            }
            System.out.println(len - m);
        }
    }
}
