

import java.util.Scanner;

public class Main {
    static Scanner sc = new Scanner(System.in);


    public static void main(String[] args) {
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        int max = Integer.MIN_VALUE;
        int count = 0;
        for (int i = 1; i < n; i++) {
            int value = a[i] - a[i - 1];
            count += value;
            max = Math.max(max, value);
        }
        int value = k - a[n - 1] + a[0];
        count += value;
        max = Math.max(max, value);
        System.out.println(count - max);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    static Scanner sc = new Scanner(System.in);


    public static void main(String[] args) {
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        int max = Integer.MIN_VALUE;
        int count = 0;
        for (int i = 1; i < n; i++) {
            int value = a[i] - a[i - 1];
            count += value;
            max = Math.max(max, value);
        }
        int value = k - a[n - 1] + a[0];
        count += value;
        max = Math.max(max, value);
        System.out.println(count - max);
    }
} Main static Scanner sc = new Scanner(System.in); sc new Scanner(System.in) public static void main(String[] args) {
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        int max = Integer.MIN_VALUE;
        int count = 0;
        for (int i = 1; i < n; i++) {
            int value = a[i] - a[i - 1];
            count += value;
            max = Math.max(max, value);
        }
        int value = k - a[n - 1] + a[0];
        count += value;
        max = Math.max(max, value);
        System.out.println(count - max);
    } main {
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        int max = Integer.MIN_VALUE;
        int count = 0;
        for (int i = 1; i < n; i++) {
            int value = a[i] - a[i - 1];
            count += value;
            max = Math.max(max, value);
        }
        int value = k - a[n - 1] + a[0];
        count += value;
        max = Math.max(max, value);
        System.out.println(count - max);
    } 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 int max = Integer.MIN_VALUE; max Integer.MIN_VALUE Integer Integer.MIN_VALUE int count = 0; count 0 for (int i = 1; i < n; i++) {
            int value = a[i] - a[i - 1];
            count += value;
            max = Math.max(max, value);
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            int value = a[i] - a[i - 1];
            count += value;
            max = Math.max(max, value);
        } {
            int value = a[i] - a[i - 1];
            count += value;
            max = Math.max(max, value);
        } int value = a[i] - a[i - 1]; value a[i] - a[i - 1] a[i] a i a[i - 1] a i - 1 i 1 count += value count value max = Math.max(max, value) max Math.max(max, value) Math.max Math max value int value = k - a[n - 1] + a[0]; value k - a[n - 1] + a[0] k - a[n - 1] k a[n - 1] a n - 1 n 1 a[0] a 0 count += value count value max = Math.max(max, value) max Math.max(max, value) Math.max Math max value System.out.println(count - max) System.out.println System.out System System.out count - max count max String[] args args public class Main {
    static Scanner sc = new Scanner(System.in);


    public static void main(String[] args) {
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        int max = Integer.MIN_VALUE;
        int count = 0;
        for (int i = 1; i < n; i++) {
            int value = a[i] - a[i - 1];
            count += value;
            max = Math.max(max, value);
        }
        int value = k - a[n - 1] + a[0];
        count += value;
        max = Math.max(max, value);
        System.out.println(count - max);
    }
} public class Main {
    static Scanner sc = new Scanner(System.in);


    public static void main(String[] args) {
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        int max = Integer.MIN_VALUE;
        int count = 0;
        for (int i = 1; i < n; i++) {
            int value = a[i] - a[i - 1];
            count += value;
            max = Math.max(max, value);
        }
        int value = k - a[n - 1] + a[0];
        count += value;
        max = Math.max(max, value);
        System.out.println(count - max);
    }
} Main


import java.util.Scanner;

public class Main {
    static Scanner sc = new Scanner(System.in);


    public static void main(String[] args) {
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        int max = Integer.MIN_VALUE;
        int count = 0;
        for (int i = 1; i < n; i++) {
            int value = a[i] - a[i - 1];
            count += value;
            max = Math.max(max, value);
        }
        int value = k - a[n - 1] + a[0];
        count += value;
        max = Math.max(max, value);
        System.out.println(count - max);
    }
}
