import 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];
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(sc.next());
        }

        // 主処理
        int[] diff = new int[n];
        for (int i = 0; i < n - 1; i++) {
            diff[i] = a[i + 1] - a[i];
        }
        diff[n - 1] = a[0] + (k - a[n - 1]);

        int sum = Arrays.stream(diff).sum();
        int max = Arrays.stream(diff).max().getAsInt();
        int result = sum - max;

        // 出力
        System.out.println(result);
        sc.close();
    }
} import java.util.*; util.* java 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];
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(sc.next());
        }

        // 主処理
        int[] diff = new int[n];
        for (int i = 0; i < n - 1; i++) {
            diff[i] = a[i + 1] - a[i];
        }
        diff[n - 1] = a[0] + (k - a[n - 1]);

        int sum = Arrays.stream(diff).sum();
        int max = Arrays.stream(diff).max().getAsInt();
        int result = sum - max;

        // 出力
        System.out.println(result);
        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];
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(sc.next());
        }

        // 主処理
        int[] diff = new int[n];
        for (int i = 0; i < n - 1; i++) {
            diff[i] = a[i + 1] - a[i];
        }
        diff[n - 1] = a[0] + (k - a[n - 1]);

        int sum = Arrays.stream(diff).sum();
        int max = Arrays.stream(diff).max().getAsInt();
        int result = sum - max;

        // 出力
        System.out.println(result);
        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];
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(sc.next());
        }

        // 主処理
        int[] diff = new int[n];
        for (int i = 0; i < n - 1; i++) {
            diff[i] = a[i + 1] - a[i];
        }
        diff[n - 1] = a[0] + (k - a[n - 1]);

        int sum = Arrays.stream(diff).sum();
        int max = Arrays.stream(diff).max().getAsInt();
        int result = sum - max;

        // 出力
        System.out.println(result);
        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 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 int[] diff = new int[n]; diff new int[n] n for (int i = 0; i < n - 1; i++) {
            diff[i] = a[i + 1] - a[i];
        } int i = 0; int i = 0; i 0 i < n - 1 i n - 1 n 1 i++ i++ i {
            diff[i] = a[i + 1] - a[i];
        } {
            diff[i] = a[i + 1] - a[i];
        } diff[i] = a[i + 1] - a[i] diff[i] diff i a[i + 1] - a[i] a[i + 1] a i + 1 i 1 a[i] a i diff[n - 1] = a[0] + (k - a[n - 1]) diff[n - 1] diff n - 1 n 1 a[0] + (k - a[n - 1]) a[0] a 0 (k - a[n - 1]) k a[n - 1] a n - 1 n 1 int sum = Arrays.stream(diff).sum(); sum Arrays.stream(diff).sum() Arrays.stream(diff).sum Arrays.stream(diff) Arrays.stream Arrays diff int max = Arrays.stream(diff).max().getAsInt(); max Arrays.stream(diff).max().getAsInt() Arrays.stream(diff).max().getAsInt Arrays.stream(diff).max() Arrays.stream(diff).max Arrays.stream(diff) Arrays.stream Arrays diff int result = sum - max; result sum - max sum max System.out.println(result) System.out.println System.out System System.out result 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];
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(sc.next());
        }

        // 主処理
        int[] diff = new int[n];
        for (int i = 0; i < n - 1; i++) {
            diff[i] = a[i + 1] - a[i];
        }
        diff[n - 1] = a[0] + (k - a[n - 1]);

        int sum = Arrays.stream(diff).sum();
        int max = Arrays.stream(diff).max().getAsInt();
        int result = sum - max;

        // 出力
        System.out.println(result);
        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];
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(sc.next());
        }

        // 主処理
        int[] diff = new int[n];
        for (int i = 0; i < n - 1; i++) {
            diff[i] = a[i + 1] - a[i];
        }
        diff[n - 1] = a[0] + (k - a[n - 1]);

        int sum = Arrays.stream(diff).sum();
        int max = Arrays.stream(diff).max().getAsInt();
        int result = sum - max;

        // 出力
        System.out.println(result);
        sc.close();
    }
} Main
import 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];
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(sc.next());
        }

        // 主処理
        int[] diff = new int[n];
        for (int i = 0; i < n - 1; i++) {
            diff[i] = a[i + 1] - a[i];
        }
        diff[n - 1] = a[0] + (k - a[n - 1]);

        int sum = Arrays.stream(diff).sum();
        int max = Arrays.stream(diff).max().getAsInt();
        int result = sum - max;

        // 出力
        System.out.println(result);
        sc.close();
    }
}
