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

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] diff = new int[n];

        int max_diff = 0;
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
            if(i > 0) {
                diff[i] = a[i] - a[i-1];
                max_diff = Math.max(max_diff, diff[i]);
            }
        }
        diff[0] = a[0] + k - a[a.length-1];
        max_diff = Math.max(max_diff, diff[0]);
        System.out.println(Arrays.stream(diff).sum() - max_diff);
    }
} import java.util.Arrays; Arrays 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 = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] diff = new int[n];

        int max_diff = 0;
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
            if(i > 0) {
                diff[i] = a[i] - a[i-1];
                max_diff = Math.max(max_diff, diff[i]);
            }
        }
        diff[0] = a[0] + k - a[a.length-1];
        max_diff = Math.max(max_diff, diff[0]);
        System.out.println(Arrays.stream(diff).sum() - max_diff);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] diff = new int[n];

        int max_diff = 0;
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
            if(i > 0) {
                diff[i] = a[i] - a[i-1];
                max_diff = Math.max(max_diff, diff[i]);
            }
        }
        diff[0] = a[0] + k - a[a.length-1];
        max_diff = Math.max(max_diff, diff[0]);
        System.out.println(Arrays.stream(diff).sum() - max_diff);
    } main {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] diff = new int[n];

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

        int max_diff = 0;
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
            if(i > 0) {
                diff[i] = a[i] - a[i-1];
                max_diff = Math.max(max_diff, diff[i]);
            }
        }
        diff[0] = a[0] + k - a[a.length-1];
        max_diff = Math.max(max_diff, diff[0]);
        System.out.println(Arrays.stream(diff).sum() - max_diff);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] diff = new int[n];

        int max_diff = 0;
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
            if(i > 0) {
                diff[i] = a[i] - a[i-1];
                max_diff = Math.max(max_diff, diff[i]);
            }
        }
        diff[0] = a[0] + k - a[a.length-1];
        max_diff = Math.max(max_diff, diff[0]);
        System.out.println(Arrays.stream(diff).sum() - max_diff);
    }
} Main
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] diff = new int[n];

        int max_diff = 0;
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
            if(i > 0) {
                diff[i] = a[i] - a[i-1];
                max_diff = Math.max(max_diff, diff[i]);
            }
        }
        diff[0] = a[0] + k - a[a.length-1];
        max_diff = Math.max(max_diff, diff[0]);
        System.out.println(Arrays.stream(diff).sum() - max_diff);
    }
}