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

public class Main {

    public static void main(final String[] args) throws Exception {
        final Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] A = new int[N];
        for(int i = 0 ; i < N ; i++){
                A[i] = sc.nextInt();
        }

        final Solver solver = new Solver();
        solver.solve(K, N, A);
    }
}

class Solver {
    public void solve(int K, int N, int[] A) {
        long min = Long.MAX_VALUE;

        for (int i = 0; i < N; i++) {

            long backward = i == N - 1?
                    A[i] - A[0] :
                    A[i] + (K - A[i + 1]);

            long forward = i == 0 ?
                    A[N - 1] - A[i] :
                    (K - A[i]) + A[i - 1];

            long shorter = Math.min(backward, forward);
            min = Math.min(min, shorter);
        }

        System.out.println(min);
    }
} import java.io.*; io.* java import java.util.*; util.* java public class Main {

    public static void main(final String[] args) throws Exception {
        final Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] A = new int[N];
        for(int i = 0 ; i < N ; i++){
                A[i] = sc.nextInt();
        }

        final Solver solver = new Solver();
        solver.solve(K, N, A);
    }
} Main public static void main(final String[] args) throws Exception {
        final Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] A = new int[N];
        for(int i = 0 ; i < N ; i++){
                A[i] = sc.nextInt();
        }

        final Solver solver = new Solver();
        solver.solve(K, N, A);
    } main {
        final Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] A = new int[N];
        for(int i = 0 ; i < N ; i++){
                A[i] = sc.nextInt();
        }

        final Solver solver = new Solver();
        solver.solve(K, N, A);
    } final Scanner sc = new Scanner(System.in); sc new Scanner(System.in) 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 final Solver solver = new Solver(); solver new Solver() solver.solve(K, N, A) solver.solve solver K N A final String[] args args class Solver {
    public void solve(int K, int N, int[] A) {
        long min = Long.MAX_VALUE;

        for (int i = 0; i < N; i++) {

            long backward = i == N - 1?
                    A[i] - A[0] :
                    A[i] + (K - A[i + 1]);

            long forward = i == 0 ?
                    A[N - 1] - A[i] :
                    (K - A[i]) + A[i - 1];

            long shorter = Math.min(backward, forward);
            min = Math.min(min, shorter);
        }

        System.out.println(min);
    }
} Solver public void solve(int K, int N, int[] A) {
        long min = Long.MAX_VALUE;

        for (int i = 0; i < N; i++) {

            long backward = i == N - 1?
                    A[i] - A[0] :
                    A[i] + (K - A[i + 1]);

            long forward = i == 0 ?
                    A[N - 1] - A[i] :
                    (K - A[i]) + A[i - 1];

            long shorter = Math.min(backward, forward);
            min = Math.min(min, shorter);
        }

        System.out.println(min);
    } solve {
        long min = Long.MAX_VALUE;

        for (int i = 0; i < N; i++) {

            long backward = i == N - 1?
                    A[i] - A[0] :
                    A[i] + (K - A[i + 1]);

            long forward = i == 0 ?
                    A[N - 1] - A[i] :
                    (K - A[i]) + A[i - 1];

            long shorter = Math.min(backward, forward);
            min = Math.min(min, shorter);
        }

        System.out.println(min);
    } long min = Long.MAX_VALUE; min Long.MAX_VALUE Long Long.MAX_VALUE for (int i = 0; i < N; i++) {

            long backward = i == N - 1?
                    A[i] - A[0] :
                    A[i] + (K - A[i + 1]);

            long forward = i == 0 ?
                    A[N - 1] - A[i] :
                    (K - A[i]) + A[i - 1];

            long shorter = Math.min(backward, forward);
            min = Math.min(min, shorter);
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {

            long backward = i == N - 1?
                    A[i] - A[0] :
                    A[i] + (K - A[i + 1]);

            long forward = i == 0 ?
                    A[N - 1] - A[i] :
                    (K - A[i]) + A[i - 1];

            long shorter = Math.min(backward, forward);
            min = Math.min(min, shorter);
        } {

            long backward = i == N - 1?
                    A[i] - A[0] :
                    A[i] + (K - A[i + 1]);

            long forward = i == 0 ?
                    A[N - 1] - A[i] :
                    (K - A[i]) + A[i - 1];

            long shorter = Math.min(backward, forward);
            min = Math.min(min, shorter);
        } long backward = i == N - 1?
                    A[i] - A[0] :
                    A[i] + (K - A[i + 1]); backward i == N - 1?
                    A[i] - A[0] :
                    A[i] + (K - A[i + 1]) i == N - 1 i N - 1 N 1 A[i] - A[0] A[i] A i A[0] A 0 A[i] + (K - A[i + 1]) A[i] A i (K - A[i + 1]) K A[i + 1] A i + 1 i 1 long forward = i == 0 ?
                    A[N - 1] - A[i] :
                    (K - A[i]) + A[i - 1]; forward i == 0 ?
                    A[N - 1] - A[i] :
                    (K - A[i]) + A[i - 1] i == 0 i 0 A[N - 1] - A[i] A[N - 1] A N - 1 N 1 A[i] A i (K - A[i]) + A[i - 1] (K - A[i]) K A[i] A i A[i - 1] A i - 1 i 1 long shorter = Math.min(backward, forward); shorter Math.min(backward, forward) Math.min Math backward forward min = Math.min(min, shorter) min Math.min(min, shorter) Math.min Math min shorter System.out.println(min) System.out.println System.out System System.out min int K K int N N int[] A A public class Main {

    public static void main(final String[] args) throws Exception {
        final Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] A = new int[N];
        for(int i = 0 ; i < N ; i++){
                A[i] = sc.nextInt();
        }

        final Solver solver = new Solver();
        solver.solve(K, N, A);
    }
} public class Main {

    public static void main(final String[] args) throws Exception {
        final Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] A = new int[N];
        for(int i = 0 ; i < N ; i++){
                A[i] = sc.nextInt();
        }

        final Solver solver = new Solver();
        solver.solve(K, N, A);
    }
} Main
import java.io.*;
import java.util.*;

public class Main {

    public static void main(final String[] args) throws Exception {
        final Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] A = new int[N];
        for(int i = 0 ; i < N ; i++){
                A[i] = sc.nextInt();
        }

        final Solver solver = new Solver();
        solver.solve(K, N, A);
    }
}

class Solver {
    public void solve(int K, int N, int[] A) {
        long min = Long.MAX_VALUE;

        for (int i = 0; i < N; i++) {

            long backward = i == N - 1?
                    A[i] - A[0] :
                    A[i] + (K - A[i + 1]);

            long forward = i == 0 ?
                    A[N - 1] - A[i] :
                    (K - A[i]) + A[i - 1];

            long shorter = Math.min(backward, forward);
            min = Math.min(min, shorter);
        }

        System.out.println(min);
    }
}
