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