import java.util.Scanner;

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];
        int sub = 0;
        int bef = 0;
        int temp = 0;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            temp = Integer.parseInt(sc.next());
            if (i != 0) {
                ans += temp - bef;
                sub = Math.max(sub, temp - bef);
            }
            if (i == n - 1) {
                ans += Math.abs(k - temp + a[0]);
                sub = Math.max(sub, Math.abs(k - temp + a[0]));
            }
            bef = temp;
            a[i] = temp;
        }
        System.out.println(ans - sub);
    }
} import java.util.Scanner; Scanner 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];
        int sub = 0;
        int bef = 0;
        int temp = 0;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            temp = Integer.parseInt(sc.next());
            if (i != 0) {
                ans += temp - bef;
                sub = Math.max(sub, temp - bef);
            }
            if (i == n - 1) {
                ans += Math.abs(k - temp + a[0]);
                sub = Math.max(sub, Math.abs(k - temp + a[0]));
            }
            bef = temp;
            a[i] = temp;
        }
        System.out.println(ans - sub);
    }
} 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];
        int sub = 0;
        int bef = 0;
        int temp = 0;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            temp = Integer.parseInt(sc.next());
            if (i != 0) {
                ans += temp - bef;
                sub = Math.max(sub, temp - bef);
            }
            if (i == n - 1) {
                ans += Math.abs(k - temp + a[0]);
                sub = Math.max(sub, Math.abs(k - temp + a[0]));
            }
            bef = temp;
            a[i] = temp;
        }
        System.out.println(ans - sub);
    } main {
        Scanner sc = new Scanner(System.in);
        int k = Integer.parseInt(sc.next());
        int n = Integer.parseInt(sc.next());
        int[] a = new int[n];
        int sub = 0;
        int bef = 0;
        int temp = 0;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            temp = Integer.parseInt(sc.next());
            if (i != 0) {
                ans += temp - bef;
                sub = Math.max(sub, temp - bef);
            }
            if (i == n - 1) {
                ans += Math.abs(k - temp + a[0]);
                sub = Math.max(sub, Math.abs(k - temp + a[0]));
            }
            bef = temp;
            a[i] = temp;
        }
        System.out.println(ans - sub);
    } 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 int sub = 0; sub 0 int bef = 0; bef 0 int temp = 0; temp 0 int ans = 0; ans 0 for (int i = 0; i < n; i++) {
            temp = Integer.parseInt(sc.next());
            if (i != 0) {
                ans += temp - bef;
                sub = Math.max(sub, temp - bef);
            }
            if (i == n - 1) {
                ans += Math.abs(k - temp + a[0]);
                sub = Math.max(sub, Math.abs(k - temp + a[0]));
            }
            bef = temp;
            a[i] = temp;
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            temp = Integer.parseInt(sc.next());
            if (i != 0) {
                ans += temp - bef;
                sub = Math.max(sub, temp - bef);
            }
            if (i == n - 1) {
                ans += Math.abs(k - temp + a[0]);
                sub = Math.max(sub, Math.abs(k - temp + a[0]));
            }
            bef = temp;
            a[i] = temp;
        } {
            temp = Integer.parseInt(sc.next());
            if (i != 0) {
                ans += temp - bef;
                sub = Math.max(sub, temp - bef);
            }
            if (i == n - 1) {
                ans += Math.abs(k - temp + a[0]);
                sub = Math.max(sub, Math.abs(k - temp + a[0]));
            }
            bef = temp;
            a[i] = temp;
        } temp = Integer.parseInt(sc.next()) temp Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc if (i != 0) {
                ans += temp - bef;
                sub = Math.max(sub, temp - bef);
            } i != 0 i 0 {
                ans += temp - bef;
                sub = Math.max(sub, temp - bef);
            } ans += temp - bef ans temp - bef temp bef sub = Math.max(sub, temp - bef) sub Math.max(sub, temp - bef) Math.max Math sub temp - bef temp bef if (i == n - 1) {
                ans += Math.abs(k - temp + a[0]);
                sub = Math.max(sub, Math.abs(k - temp + a[0]));
            } i == n - 1 i n - 1 n 1 {
                ans += Math.abs(k - temp + a[0]);
                sub = Math.max(sub, Math.abs(k - temp + a[0]));
            } ans += Math.abs(k - temp + a[0]) ans Math.abs(k - temp + a[0]) Math.abs Math k - temp + a[0] k - temp k temp a[0] a 0 sub = Math.max(sub, Math.abs(k - temp + a[0])) sub Math.max(sub, Math.abs(k - temp + a[0])) Math.max Math sub Math.abs(k - temp + a[0]) Math.abs Math k - temp + a[0] k - temp k temp a[0] a 0 bef = temp bef temp a[i] = temp a[i] a i temp System.out.println(ans - sub) System.out.println System.out System System.out ans - sub ans sub 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];
        int sub = 0;
        int bef = 0;
        int temp = 0;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            temp = Integer.parseInt(sc.next());
            if (i != 0) {
                ans += temp - bef;
                sub = Math.max(sub, temp - bef);
            }
            if (i == n - 1) {
                ans += Math.abs(k - temp + a[0]);
                sub = Math.max(sub, Math.abs(k - temp + a[0]));
            }
            bef = temp;
            a[i] = temp;
        }
        System.out.println(ans - sub);
    }
} 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];
        int sub = 0;
        int bef = 0;
        int temp = 0;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            temp = Integer.parseInt(sc.next());
            if (i != 0) {
                ans += temp - bef;
                sub = Math.max(sub, temp - bef);
            }
            if (i == n - 1) {
                ans += Math.abs(k - temp + a[0]);
                sub = Math.max(sub, Math.abs(k - temp + a[0]));
            }
            bef = temp;
            a[i] = temp;
        }
        System.out.println(ans - sub);
    }
} Main
import java.util.Scanner;

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];
        int sub = 0;
        int bef = 0;
        int temp = 0;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            temp = Integer.parseInt(sc.next());
            if (i != 0) {
                ans += temp - bef;
                sub = Math.max(sub, temp - bef);
            }
            if (i == n - 1) {
                ans += Math.abs(k - temp + a[0]);
                sub = Math.max(sub, Math.abs(k - temp + a[0]));
            }
            bef = temp;
            a[i] = temp;
        }
        System.out.println(ans - sub);
    }
}
