import java.util.Scanner;
import java.util.stream.IntStream;

public class Main {

    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);

        final int k = sc.nextInt();
        final int n = sc.nextInt();

        final int[] d = new int[n];
        final int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        for (int i = 0; i < n-1; i++) {
            d[i] = a[i+1] - a[i];
        }

        d[n-1] = k + a[0] - a[n-1];

        System.out.println(IntStream.of(d).sum() - IntStream.of(d).max().getAsInt());
    }
} import java.util.Scanner; Scanner java.util import java.util.stream.IntStream; IntStream java.util.stream public class Main {

    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);

        final int k = sc.nextInt();
        final int n = sc.nextInt();

        final int[] d = new int[n];
        final int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        for (int i = 0; i < n-1; i++) {
            d[i] = a[i+1] - a[i];
        }

        d[n-1] = k + a[0] - a[n-1];

        System.out.println(IntStream.of(d).sum() - IntStream.of(d).max().getAsInt());
    }
} Main public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);

        final int k = sc.nextInt();
        final int n = sc.nextInt();

        final int[] d = new int[n];
        final int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        for (int i = 0; i < n-1; i++) {
            d[i] = a[i+1] - a[i];
        }

        d[n-1] = k + a[0] - a[n-1];

        System.out.println(IntStream.of(d).sum() - IntStream.of(d).max().getAsInt());
    } main {
        final Scanner sc = new Scanner(System.in);

        final int k = sc.nextInt();
        final int n = sc.nextInt();

        final int[] d = new int[n];
        final int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        for (int i = 0; i < n-1; i++) {
            d[i] = a[i+1] - a[i];
        }

        d[n-1] = k + a[0] - a[n-1];

        System.out.println(IntStream.of(d).sum() - IntStream.of(d).max().getAsInt());
    } final Scanner sc = new Scanner(System.in); sc new Scanner(System.in) final int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc final int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc final int[] d = new int[n]; d new int[n] n final 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 for (int i = 0; i < n-1; i++) {
            d[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 {
            d[i] = a[i+1] - a[i];
        } {
            d[i] = a[i+1] - a[i];
        } d[i] = a[i+1] - a[i] d[i] d i a[i+1] - a[i] a[i+1] a i+1 i 1 a[i] a i d[n-1] = k + a[0] - a[n-1] d[n-1] d n-1 n 1 k + a[0] - a[n-1] k + a[0] k a[0] a 0 a[n-1] a n-1 n 1 System.out.println(IntStream.of(d).sum() - IntStream.of(d).max().getAsInt()) System.out.println System.out System System.out IntStream.of(d).sum() - IntStream.of(d).max().getAsInt() IntStream.of(d).sum() IntStream.of(d).sum IntStream.of(d) IntStream.of IntStream d IntStream.of(d).max().getAsInt() IntStream.of(d).max().getAsInt IntStream.of(d).max() IntStream.of(d).max IntStream.of(d) IntStream.of IntStream d String[] args args public class Main {

    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);

        final int k = sc.nextInt();
        final int n = sc.nextInt();

        final int[] d = new int[n];
        final int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        for (int i = 0; i < n-1; i++) {
            d[i] = a[i+1] - a[i];
        }

        d[n-1] = k + a[0] - a[n-1];

        System.out.println(IntStream.of(d).sum() - IntStream.of(d).max().getAsInt());
    }
} public class Main {

    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);

        final int k = sc.nextInt();
        final int n = sc.nextInt();

        final int[] d = new int[n];
        final int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        for (int i = 0; i < n-1; i++) {
            d[i] = a[i+1] - a[i];
        }

        d[n-1] = k + a[0] - a[n-1];

        System.out.println(IntStream.of(d).sum() - IntStream.of(d).max().getAsInt());
    }
} Main
import java.util.Scanner;
import java.util.stream.IntStream;

public class Main {

    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);

        final int k = sc.nextInt();
        final int n = sc.nextInt();

        final int[] d = new int[n];
        final int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        for (int i = 0; i < n-1; i++) {
            d[i] = a[i+1] - a[i];
        }

        d[n-1] = k + a[0] - a[n-1];

        System.out.println(IntStream.of(d).sum() - IntStream.of(d).max().getAsInt());
    }
}
