import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Integer K = input.nextInt();
        Integer N = input.nextInt();
        Integer A[] = new Integer[N];
        Integer distances[] = new Integer[N];
        Integer sumDistance = 0;
        Integer maxDistance = 0;

        for (int i = 0; i < N; i++) {
            A[i] = input.nextInt();
        }
        Arrays.sort(A);

        for (int i = 0; i < N - 1; i++) {
            distances[i+1] = A[i+1] - A[i];
        }
        distances[0] = (K - A[N-1]) + A[0];

        for (Integer distance : distances) {
            sumDistance += distance;
            maxDistance = Math.max(maxDistance, distance);
        }

        System.out.println(sumDistance - maxDistance);
    }
} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Integer K = input.nextInt();
        Integer N = input.nextInt();
        Integer A[] = new Integer[N];
        Integer distances[] = new Integer[N];
        Integer sumDistance = 0;
        Integer maxDistance = 0;

        for (int i = 0; i < N; i++) {
            A[i] = input.nextInt();
        }
        Arrays.sort(A);

        for (int i = 0; i < N - 1; i++) {
            distances[i+1] = A[i+1] - A[i];
        }
        distances[0] = (K - A[N-1]) + A[0];

        for (Integer distance : distances) {
            sumDistance += distance;
            maxDistance = Math.max(maxDistance, distance);
        }

        System.out.println(sumDistance - maxDistance);
    }
} Main public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Integer K = input.nextInt();
        Integer N = input.nextInt();
        Integer A[] = new Integer[N];
        Integer distances[] = new Integer[N];
        Integer sumDistance = 0;
        Integer maxDistance = 0;

        for (int i = 0; i < N; i++) {
            A[i] = input.nextInt();
        }
        Arrays.sort(A);

        for (int i = 0; i < N - 1; i++) {
            distances[i+1] = A[i+1] - A[i];
        }
        distances[0] = (K - A[N-1]) + A[0];

        for (Integer distance : distances) {
            sumDistance += distance;
            maxDistance = Math.max(maxDistance, distance);
        }

        System.out.println(sumDistance - maxDistance);
    } main {
        Scanner input = new Scanner(System.in);
        Integer K = input.nextInt();
        Integer N = input.nextInt();
        Integer A[] = new Integer[N];
        Integer distances[] = new Integer[N];
        Integer sumDistance = 0;
        Integer maxDistance = 0;

        for (int i = 0; i < N; i++) {
            A[i] = input.nextInt();
        }
        Arrays.sort(A);

        for (int i = 0; i < N - 1; i++) {
            distances[i+1] = A[i+1] - A[i];
        }
        distances[0] = (K - A[N-1]) + A[0];

        for (Integer distance : distances) {
            sumDistance += distance;
            maxDistance = Math.max(maxDistance, distance);
        }

        System.out.println(sumDistance - maxDistance);
    } Scanner input = new Scanner(System.in); input new Scanner(System.in) Integer K = input.nextInt(); K input.nextInt() input.nextInt input Integer N = input.nextInt(); N input.nextInt() input.nextInt input Integer A[] = new Integer[N]; A new Integer[N] N Integer distances[] = new Integer[N]; distances new Integer[N] N Integer sumDistance = 0; sumDistance 0 Integer maxDistance = 0; maxDistance 0 for (int i = 0; i < N; i++) {
            A[i] = input.nextInt();
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            A[i] = input.nextInt();
        } {
            A[i] = input.nextInt();
        } A[i] = input.nextInt() A[i] A i input.nextInt() input.nextInt input Arrays.sort(A) Arrays.sort Arrays A for (int i = 0; i < N - 1; i++) {
            distances[i+1] = A[i+1] - A[i];
        } int i = 0; int i = 0; i 0 i < N - 1 i N - 1 N 1 i++ i++ i {
            distances[i+1] = A[i+1] - A[i];
        } {
            distances[i+1] = A[i+1] - A[i];
        } distances[i+1] = A[i+1] - A[i] distances[i+1] distances i+1 i 1 A[i+1] - A[i] A[i+1] A i+1 i 1 A[i] A i distances[0] = (K - A[N-1]) + A[0] distances[0] distances 0 (K - A[N-1]) + A[0] (K - A[N-1]) K A[N-1] A N-1 N 1 A[0] A 0 for (Integer distance : distances) {
            sumDistance += distance;
            maxDistance = Math.max(maxDistance, distance);
        } Integer distance distances {
            sumDistance += distance;
            maxDistance = Math.max(maxDistance, distance);
        } {
            sumDistance += distance;
            maxDistance = Math.max(maxDistance, distance);
        } sumDistance += distance sumDistance distance maxDistance = Math.max(maxDistance, distance) maxDistance Math.max(maxDistance, distance) Math.max Math maxDistance distance System.out.println(sumDistance - maxDistance) System.out.println System.out System System.out sumDistance - maxDistance sumDistance maxDistance String[] args args public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Integer K = input.nextInt();
        Integer N = input.nextInt();
        Integer A[] = new Integer[N];
        Integer distances[] = new Integer[N];
        Integer sumDistance = 0;
        Integer maxDistance = 0;

        for (int i = 0; i < N; i++) {
            A[i] = input.nextInt();
        }
        Arrays.sort(A);

        for (int i = 0; i < N - 1; i++) {
            distances[i+1] = A[i+1] - A[i];
        }
        distances[0] = (K - A[N-1]) + A[0];

        for (Integer distance : distances) {
            sumDistance += distance;
            maxDistance = Math.max(maxDistance, distance);
        }

        System.out.println(sumDistance - maxDistance);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Integer K = input.nextInt();
        Integer N = input.nextInt();
        Integer A[] = new Integer[N];
        Integer distances[] = new Integer[N];
        Integer sumDistance = 0;
        Integer maxDistance = 0;

        for (int i = 0; i < N; i++) {
            A[i] = input.nextInt();
        }
        Arrays.sort(A);

        for (int i = 0; i < N - 1; i++) {
            distances[i+1] = A[i+1] - A[i];
        }
        distances[0] = (K - A[N-1]) + A[0];

        for (Integer distance : distances) {
            sumDistance += distance;
            maxDistance = Math.max(maxDistance, distance);
        }

        System.out.println(sumDistance - maxDistance);
    }
} Main
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Integer K = input.nextInt();
        Integer N = input.nextInt();
        Integer A[] = new Integer[N];
        Integer distances[] = new Integer[N];
        Integer sumDistance = 0;
        Integer maxDistance = 0;

        for (int i = 0; i < N; i++) {
            A[i] = input.nextInt();
        }
        Arrays.sort(A);

        for (int i = 0; i < N - 1; i++) {
            distances[i+1] = A[i+1] - A[i];
        }
        distances[0] = (K - A[N-1]) + A[0];

        for (Integer distance : distances) {
            sumDistance += distance;
            maxDistance = Math.max(maxDistance, distance);
        }

        System.out.println(sumDistance - maxDistance);
    }
}