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];
        long sum = 0;
        int max = 0;
        a[0] = sc.nextInt();
        for (int i = 1; i < n; i++) {
            a[i] = sc.nextInt();
            sum += a[i] - a[i-1];
            max = Math.max(max, a[i] - a[i-1]);
        }
        int se = k - a[n-1] + a[0];
        sum += se;
        max = Math.max(max, se);
        System.out.println(sum-max);
    }
} 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];
        long sum = 0;
        int max = 0;
        a[0] = sc.nextInt();
        for (int i = 1; i < n; i++) {
            a[i] = sc.nextInt();
            sum += a[i] - a[i-1];
            max = Math.max(max, a[i] - a[i-1]);
        }
        int se = k - a[n-1] + a[0];
        sum += se;
        max = Math.max(max, se);
        System.out.println(sum-max);
    }
} 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];
        long sum = 0;
        int max = 0;
        a[0] = sc.nextInt();
        for (int i = 1; i < n; i++) {
            a[i] = sc.nextInt();
            sum += a[i] - a[i-1];
            max = Math.max(max, a[i] - a[i-1]);
        }
        int se = k - a[n-1] + a[0];
        sum += se;
        max = Math.max(max, se);
        System.out.println(sum-max);
    } main {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        long sum = 0;
        int max = 0;
        a[0] = sc.nextInt();
        for (int i = 1; i < n; i++) {
            a[i] = sc.nextInt();
            sum += a[i] - a[i-1];
            max = Math.max(max, a[i] - a[i-1]);
        }
        int se = k - a[n-1] + a[0];
        sum += se;
        max = Math.max(max, se);
        System.out.println(sum-max);
    } 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 long sum = 0; sum 0 int max = 0; max 0 a[0] = sc.nextInt() a[0] a 0 sc.nextInt() sc.nextInt sc for (int i = 1; i < n; i++) {
            a[i] = sc.nextInt();
            sum += a[i] - a[i-1];
            max = Math.max(max, a[i] - a[i-1]);
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            a[i] = sc.nextInt();
            sum += a[i] - a[i-1];
            max = Math.max(max, a[i] - a[i-1]);
        } {
            a[i] = sc.nextInt();
            sum += a[i] - a[i-1];
            max = Math.max(max, a[i] - a[i-1]);
        } a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc sum += a[i] - a[i-1] sum a[i] - a[i-1] a[i] a i a[i-1] a i-1 i 1 max = Math.max(max, a[i] - a[i-1]) max Math.max(max, a[i] - a[i-1]) Math.max Math max a[i] - a[i-1] a[i] a i a[i-1] a i-1 i 1 int se = k - a[n-1] + a[0]; se k - a[n-1] + a[0] k - a[n-1] k a[n-1] a n-1 n 1 a[0] a 0 sum += se sum se max = Math.max(max, se) max Math.max(max, se) Math.max Math max se System.out.println(sum-max) System.out.println System.out System System.out sum-max sum max 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];
        long sum = 0;
        int max = 0;
        a[0] = sc.nextInt();
        for (int i = 1; i < n; i++) {
            a[i] = sc.nextInt();
            sum += a[i] - a[i-1];
            max = Math.max(max, a[i] - a[i-1]);
        }
        int se = k - a[n-1] + a[0];
        sum += se;
        max = Math.max(max, se);
        System.out.println(sum-max);
    }
} 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];
        long sum = 0;
        int max = 0;
        a[0] = sc.nextInt();
        for (int i = 1; i < n; i++) {
            a[i] = sc.nextInt();
            sum += a[i] - a[i-1];
            max = Math.max(max, a[i] - a[i-1]);
        }
        int se = k - a[n-1] + a[0];
        sum += se;
        max = Math.max(max, se);
        System.out.println(sum-max);
    }
} Main
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];
        long sum = 0;
        int max = 0;
        a[0] = sc.nextInt();
        for (int i = 1; i < n; i++) {
            a[i] = sc.nextInt();
            sum += a[i] - a[i-1];
            max = Math.max(max, a[i] - a[i-1]);
        }
        int se = k - a[n-1] + a[0];
        sum += se;
        max = Math.max(max, se);
        System.out.println(sum-max);
    }
}