import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());
        int N = Integer.parseInt(sc.next());
        int[] A = new int[N];
        Integer[] dist = new Integer[N];
        int time = 0;

        for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(sc.next());
        }

        for (int i = 0; i < N - 1; i++) {
            dist[i] = Math.abs(A[i + 1] - A[i]);
        }

        dist[N - 1] = Math.abs(A[0] + K - A[N - 1]);

        Arrays.sort(dist, Collections.reverseOrder());
        for (int i = 1; i < N; i++) {
            time += dist[i];
        }

        PrintWriter out = new PrintWriter(System.out);
        out.println(time);
        out.flush();
        sc.close();
    }
} import java.io.PrintWriter; PrintWriter java.io import java.util.Arrays; Arrays java.util import java.util.Collections; Collections java.util import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());
        int N = Integer.parseInt(sc.next());
        int[] A = new int[N];
        Integer[] dist = new Integer[N];
        int time = 0;

        for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(sc.next());
        }

        for (int i = 0; i < N - 1; i++) {
            dist[i] = Math.abs(A[i + 1] - A[i]);
        }

        dist[N - 1] = Math.abs(A[0] + K - A[N - 1]);

        Arrays.sort(dist, Collections.reverseOrder());
        for (int i = 1; i < N; i++) {
            time += dist[i];
        }

        PrintWriter out = new PrintWriter(System.out);
        out.println(time);
        out.flush();
        sc.close();
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());
        int N = Integer.parseInt(sc.next());
        int[] A = new int[N];
        Integer[] dist = new Integer[N];
        int time = 0;

        for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(sc.next());
        }

        for (int i = 0; i < N - 1; i++) {
            dist[i] = Math.abs(A[i + 1] - A[i]);
        }

        dist[N - 1] = Math.abs(A[0] + K - A[N - 1]);

        Arrays.sort(dist, Collections.reverseOrder());
        for (int i = 1; i < N; i++) {
            time += dist[i];
        }

        PrintWriter out = new PrintWriter(System.out);
        out.println(time);
        out.flush();
        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());
        int N = Integer.parseInt(sc.next());
        int[] A = new int[N];
        Integer[] dist = new Integer[N];
        int time = 0;

        for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(sc.next());
        }

        for (int i = 0; i < N - 1; i++) {
            dist[i] = Math.abs(A[i + 1] - A[i]);
        }

        dist[N - 1] = Math.abs(A[0] + K - A[N - 1]);

        Arrays.sort(dist, Collections.reverseOrder());
        for (int i = 1; i < N; i++) {
            time += dist[i];
        }

        PrintWriter out = new PrintWriter(System.out);
        out.println(time);
        out.flush();
        sc.close();
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = Integer.parseInt(sc.next()); K Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int N = Integer.parseInt(sc.next()); N Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int[] A = new int[N]; A new int[N] N Integer[] dist = new Integer[N]; dist new Integer[N] N int time = 0; time 0 for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(sc.next());
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            A[i] = Integer.parseInt(sc.next());
        } {
            A[i] = Integer.parseInt(sc.next());
        } A[i] = Integer.parseInt(sc.next()) A[i] A i Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc for (int i = 0; i < N - 1; i++) {
            dist[i] = Math.abs(A[i + 1] - A[i]);
        } int i = 0; int i = 0; i 0 i < N - 1 i N - 1 N 1 i++ i++ i {
            dist[i] = Math.abs(A[i + 1] - A[i]);
        } {
            dist[i] = Math.abs(A[i + 1] - A[i]);
        } dist[i] = Math.abs(A[i + 1] - A[i]) dist[i] dist i Math.abs(A[i + 1] - A[i]) Math.abs Math A[i + 1] - A[i] A[i + 1] A i + 1 i 1 A[i] A i dist[N - 1] = Math.abs(A[0] + K - A[N - 1]) dist[N - 1] dist N - 1 N 1 Math.abs(A[0] + K - A[N - 1]) Math.abs Math A[0] + K - A[N - 1] A[0] + K A[0] A 0 K A[N - 1] A N - 1 N 1 Arrays.sort(dist, Collections.reverseOrder()) Arrays.sort Arrays dist Collections.reverseOrder() Collections.reverseOrder Collections for (int i = 1; i < N; i++) {
            time += dist[i];
        } int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
            time += dist[i];
        } {
            time += dist[i];
        } time += dist[i] time dist[i] dist i PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) out.println(time) out.println out time out.flush() out.flush out sc.close() sc.close sc String[] args args public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());
        int N = Integer.parseInt(sc.next());
        int[] A = new int[N];
        Integer[] dist = new Integer[N];
        int time = 0;

        for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(sc.next());
        }

        for (int i = 0; i < N - 1; i++) {
            dist[i] = Math.abs(A[i + 1] - A[i]);
        }

        dist[N - 1] = Math.abs(A[0] + K - A[N - 1]);

        Arrays.sort(dist, Collections.reverseOrder());
        for (int i = 1; i < N; i++) {
            time += dist[i];
        }

        PrintWriter out = new PrintWriter(System.out);
        out.println(time);
        out.flush();
        sc.close();
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());
        int N = Integer.parseInt(sc.next());
        int[] A = new int[N];
        Integer[] dist = new Integer[N];
        int time = 0;

        for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(sc.next());
        }

        for (int i = 0; i < N - 1; i++) {
            dist[i] = Math.abs(A[i + 1] - A[i]);
        }

        dist[N - 1] = Math.abs(A[0] + K - A[N - 1]);

        Arrays.sort(dist, Collections.reverseOrder());
        for (int i = 1; i < N; i++) {
            time += dist[i];
        }

        PrintWriter out = new PrintWriter(System.out);
        out.println(time);
        out.flush();
        sc.close();
    }
} Main
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());
        int N = Integer.parseInt(sc.next());
        int[] A = new int[N];
        Integer[] dist = new Integer[N];
        int time = 0;

        for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(sc.next());
        }

        for (int i = 0; i < N - 1; i++) {
            dist[i] = Math.abs(A[i + 1] - A[i]);
        }

        dist[N - 1] = Math.abs(A[0] + K - A[N - 1]);

        Arrays.sort(dist, Collections.reverseOrder());
        for (int i = 1; i < N; i++) {
            time += dist[i];
        }

        PrintWriter out = new PrintWriter(System.out);
        out.println(time);
        out.flush();
        sc.close();
    }
}