

import java.io.IOException;
import java.util.Scanner;

public class Main {
    static class Solution {
        public void find(int perimeter, int[] arr) {

            arr[arr.length-1] = perimeter;

            int res = Integer.MAX_VALUE;
            int antiClock = (arr[0] + perimeter)%perimeter;

            res = Math.min(res, perimeter-(perimeter - arr[arr.length-2])- antiClock);
            res = Math.min(res, perimeter - (arr[1]-arr[0]));

            for(int i=1; i<arr.length-1; i++) {
                res = Math.min(res, perimeter - (arr[i+1]-arr[i])); //clock
                res = Math.min(res, perimeter - (arr[i]-arr[i-1])); //anti
            }
            System.out.println(res);

        }

    }

    public static void main(String[] args) throws IOException {
        Solution sol = new Solution();
        Scanner sc = new Scanner(System.in);
        int perimeter = sc.nextInt();
        int n = sc.nextInt();
        int[] arr = new int[n+1];
        for(int i=0; i<n; i++) {
            arr[i] = sc.nextInt();
        }
        sol.find(perimeter, arr);
    }
} import java.io.IOException; IOException java.io import java.util.Scanner; Scanner java.util public class Main {
    static class Solution {
        public void find(int perimeter, int[] arr) {

            arr[arr.length-1] = perimeter;

            int res = Integer.MAX_VALUE;
            int antiClock = (arr[0] + perimeter)%perimeter;

            res = Math.min(res, perimeter-(perimeter - arr[arr.length-2])- antiClock);
            res = Math.min(res, perimeter - (arr[1]-arr[0]));

            for(int i=1; i<arr.length-1; i++) {
                res = Math.min(res, perimeter - (arr[i+1]-arr[i])); //clock
                res = Math.min(res, perimeter - (arr[i]-arr[i-1])); //anti
            }
            System.out.println(res);

        }

    }

    public static void main(String[] args) throws IOException {
        Solution sol = new Solution();
        Scanner sc = new Scanner(System.in);
        int perimeter = sc.nextInt();
        int n = sc.nextInt();
        int[] arr = new int[n+1];
        for(int i=0; i<n; i++) {
            arr[i] = sc.nextInt();
        }
        sol.find(perimeter, arr);
    }
} Main public static void main(String[] args) throws IOException {
        Solution sol = new Solution();
        Scanner sc = new Scanner(System.in);
        int perimeter = sc.nextInt();
        int n = sc.nextInt();
        int[] arr = new int[n+1];
        for(int i=0; i<n; i++) {
            arr[i] = sc.nextInt();
        }
        sol.find(perimeter, arr);
    } main {
        Solution sol = new Solution();
        Scanner sc = new Scanner(System.in);
        int perimeter = sc.nextInt();
        int n = sc.nextInt();
        int[] arr = new int[n+1];
        for(int i=0; i<n; i++) {
            arr[i] = sc.nextInt();
        }
        sol.find(perimeter, arr);
    } Solution sol = new Solution(); sol new Solution() Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int perimeter = sc.nextInt(); perimeter sc.nextInt() sc.nextInt sc int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] arr = new int[n+1]; arr new int[n+1] n+1 n 1 for(int i=0; i<n; i++) {
            arr[i] = sc.nextInt();
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
            arr[i] = sc.nextInt();
        } {
            arr[i] = sc.nextInt();
        } arr[i] = sc.nextInt() arr[i] arr i sc.nextInt() sc.nextInt sc sol.find(perimeter, arr) sol.find sol perimeter arr String[] args args static class Solution {
        public void find(int perimeter, int[] arr) {

            arr[arr.length-1] = perimeter;

            int res = Integer.MAX_VALUE;
            int antiClock = (arr[0] + perimeter)%perimeter;

            res = Math.min(res, perimeter-(perimeter - arr[arr.length-2])- antiClock);
            res = Math.min(res, perimeter - (arr[1]-arr[0]));

            for(int i=1; i<arr.length-1; i++) {
                res = Math.min(res, perimeter - (arr[i+1]-arr[i])); //clock
                res = Math.min(res, perimeter - (arr[i]-arr[i-1])); //anti
            }
            System.out.println(res);

        }

    } Solution public void find(int perimeter, int[] arr) {

            arr[arr.length-1] = perimeter;

            int res = Integer.MAX_VALUE;
            int antiClock = (arr[0] + perimeter)%perimeter;

            res = Math.min(res, perimeter-(perimeter - arr[arr.length-2])- antiClock);
            res = Math.min(res, perimeter - (arr[1]-arr[0]));

            for(int i=1; i<arr.length-1; i++) {
                res = Math.min(res, perimeter - (arr[i+1]-arr[i])); //clock
                res = Math.min(res, perimeter - (arr[i]-arr[i-1])); //anti
            }
            System.out.println(res);

        } find {

            arr[arr.length-1] = perimeter;

            int res = Integer.MAX_VALUE;
            int antiClock = (arr[0] + perimeter)%perimeter;

            res = Math.min(res, perimeter-(perimeter - arr[arr.length-2])- antiClock);
            res = Math.min(res, perimeter - (arr[1]-arr[0]));

            for(int i=1; i<arr.length-1; i++) {
                res = Math.min(res, perimeter - (arr[i+1]-arr[i])); //clock
                res = Math.min(res, perimeter - (arr[i]-arr[i-1])); //anti
            }
            System.out.println(res);

        } arr[arr.length-1] = perimeter arr[arr.length-1] arr arr.length-1 arr.length arr arr.length 1 perimeter int res = Integer.MAX_VALUE; res Integer.MAX_VALUE Integer Integer.MAX_VALUE int antiClock = (arr[0] + perimeter)%perimeter; antiClock (arr[0] + perimeter)%perimeter (arr[0] + perimeter) arr[0] arr 0 perimeter perimeter res = Math.min(res, perimeter-(perimeter - arr[arr.length-2])- antiClock) res Math.min(res, perimeter-(perimeter - arr[arr.length-2])- antiClock) Math.min Math res perimeter-(perimeter - arr[arr.length-2])- antiClock perimeter-(perimeter - arr[arr.length-2])- antiClock perimeter (perimeter - arr[arr.length-2]) perimeter arr[arr.length-2] arr arr.length-2 arr.length arr arr.length 2 antiClock res = Math.min(res, perimeter - (arr[1]-arr[0])) res Math.min(res, perimeter - (arr[1]-arr[0])) Math.min Math res perimeter - (arr[1]-arr[0]) perimeter (arr[1]-arr[0]) arr[1] arr 1 arr[0] arr 0 for(int i=1; i<arr.length-1; i++) {
                res = Math.min(res, perimeter - (arr[i+1]-arr[i])); //clock
                res = Math.min(res, perimeter - (arr[i]-arr[i-1])); //anti
            } int i=1; int i=1; i 1 i<arr.length-1 i arr.length-1 arr.length arr arr.length 1 i++ i++ i {
                res = Math.min(res, perimeter - (arr[i+1]-arr[i])); //clock
                res = Math.min(res, perimeter - (arr[i]-arr[i-1])); //anti
            } {
                res = Math.min(res, perimeter - (arr[i+1]-arr[i])); //clock
                res = Math.min(res, perimeter - (arr[i]-arr[i-1])); //anti
            } res = Math.min(res, perimeter - (arr[i+1]-arr[i])) res Math.min(res, perimeter - (arr[i+1]-arr[i])) Math.min Math res perimeter - (arr[i+1]-arr[i]) perimeter (arr[i+1]-arr[i]) arr[i+1] arr i+1 i 1 arr[i] arr i res = Math.min(res, perimeter - (arr[i]-arr[i-1])) res Math.min(res, perimeter - (arr[i]-arr[i-1])) Math.min Math res perimeter - (arr[i]-arr[i-1]) perimeter (arr[i]-arr[i-1]) arr[i] arr i arr[i-1] arr i-1 i 1 System.out.println(res) System.out.println System.out System System.out res int perimeter perimeter int[] arr arr public class Main {
    static class Solution {
        public void find(int perimeter, int[] arr) {

            arr[arr.length-1] = perimeter;

            int res = Integer.MAX_VALUE;
            int antiClock = (arr[0] + perimeter)%perimeter;

            res = Math.min(res, perimeter-(perimeter - arr[arr.length-2])- antiClock);
            res = Math.min(res, perimeter - (arr[1]-arr[0]));

            for(int i=1; i<arr.length-1; i++) {
                res = Math.min(res, perimeter - (arr[i+1]-arr[i])); //clock
                res = Math.min(res, perimeter - (arr[i]-arr[i-1])); //anti
            }
            System.out.println(res);

        }

    }

    public static void main(String[] args) throws IOException {
        Solution sol = new Solution();
        Scanner sc = new Scanner(System.in);
        int perimeter = sc.nextInt();
        int n = sc.nextInt();
        int[] arr = new int[n+1];
        for(int i=0; i<n; i++) {
            arr[i] = sc.nextInt();
        }
        sol.find(perimeter, arr);
    }
} public class Main {
    static class Solution {
        public void find(int perimeter, int[] arr) {

            arr[arr.length-1] = perimeter;

            int res = Integer.MAX_VALUE;
            int antiClock = (arr[0] + perimeter)%perimeter;

            res = Math.min(res, perimeter-(perimeter - arr[arr.length-2])- antiClock);
            res = Math.min(res, perimeter - (arr[1]-arr[0]));

            for(int i=1; i<arr.length-1; i++) {
                res = Math.min(res, perimeter - (arr[i+1]-arr[i])); //clock
                res = Math.min(res, perimeter - (arr[i]-arr[i-1])); //anti
            }
            System.out.println(res);

        }

    }

    public static void main(String[] args) throws IOException {
        Solution sol = new Solution();
        Scanner sc = new Scanner(System.in);
        int perimeter = sc.nextInt();
        int n = sc.nextInt();
        int[] arr = new int[n+1];
        for(int i=0; i<n; i++) {
            arr[i] = sc.nextInt();
        }
        sol.find(perimeter, arr);
    }
} Main


import java.io.IOException;
import java.util.Scanner;

public class Main {
    static class Solution {
        public void find(int perimeter, int[] arr) {

            arr[arr.length-1] = perimeter;

            int res = Integer.MAX_VALUE;
            int antiClock = (arr[0] + perimeter)%perimeter;

            res = Math.min(res, perimeter-(perimeter - arr[arr.length-2])- antiClock);
            res = Math.min(res, perimeter - (arr[1]-arr[0]));

            for(int i=1; i<arr.length-1; i++) {
                res = Math.min(res, perimeter - (arr[i+1]-arr[i])); //clock
                res = Math.min(res, perimeter - (arr[i]-arr[i-1])); //anti
            }
            System.out.println(res);

        }

    }

    public static void main(String[] args) throws IOException {
        Solution sol = new Solution();
        Scanner sc = new Scanner(System.in);
        int perimeter = sc.nextInt();
        int n = sc.nextInt();
        int[] arr = new int[n+1];
        for(int i=0; i<n; i++) {
            arr[i] = sc.nextInt();
        }
        sol.find(perimeter, arr);
    }
}
