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 = Integer.parseInt(sc.next());
        int n = Integer.parseInt(sc.next());
        int[] a = new int[n];
        int[] b = new int[n];
        int result = 0;
        for (int i = 0; i < n; i++)
            a[i] = Integer.parseInt(sc.next());
        for (int i = n-1; i > 0; i--)
            b[i] = a[i] - a[i-1];
        b[0] = k-a[n-1]+a[0];
        Arrays.sort(b);
        for (int i = 0; i < n-1; i++)
            result += b[i];
        System.out.println(result);
    }
} 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 = Integer.parseInt(sc.next());
        int n = Integer.parseInt(sc.next());
        int[] a = new int[n];
        int[] b = new int[n];
        int result = 0;
        for (int i = 0; i < n; i++)
            a[i] = Integer.parseInt(sc.next());
        for (int i = n-1; i > 0; i--)
            b[i] = a[i] - a[i-1];
        b[0] = k-a[n-1]+a[0];
        Arrays.sort(b);
        for (int i = 0; i < n-1; i++)
            result += b[i];
        System.out.println(result);
    }
} 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[] b = new int[n];
        int result = 0;
        for (int i = 0; i < n; i++)
            a[i] = Integer.parseInt(sc.next());
        for (int i = n-1; i > 0; i--)
            b[i] = a[i] - a[i-1];
        b[0] = k-a[n-1]+a[0];
        Arrays.sort(b);
        for (int i = 0; i < n-1; i++)
            result += b[i];
        System.out.println(result);
    } 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[] b = new int[n];
        int result = 0;
        for (int i = 0; i < n; i++)
            a[i] = Integer.parseInt(sc.next());
        for (int i = n-1; i > 0; i--)
            b[i] = a[i] - a[i-1];
        b[0] = k-a[n-1]+a[0];
        Arrays.sort(b);
        for (int i = 0; i < n-1; i++)
            result += b[i];
        System.out.println(result);
    } 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[] b = new int[n]; b new int[n] n int result = 0; result 0 for (int i = 0; i < n; i++)
            a[i] = Integer.parseInt(sc.next()); int i = 0; int i = 0; i 0 i < n i n i++ i++ i a[i] = Integer.parseInt(sc.next()); a[i] = Integer.parseInt(sc.next()) a[i] a i Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc for (int i = n-1; i > 0; i--)
            b[i] = a[i] - a[i-1]; int i = n-1; int i = n-1; i n-1 n 1 i > 0 i 0 i-- i-- i b[i] = a[i] - a[i-1]; b[i] = a[i] - a[i-1] b[i] b i a[i] - a[i-1] a[i] a i a[i-1] a i-1 i 1 b[0] = k-a[n-1]+a[0] b[0] b 0 k-a[n-1]+a[0] k-a[n-1] k a[n-1] a n-1 n 1 a[0] a 0 Arrays.sort(b) Arrays.sort Arrays b for (int i = 0; i < n-1; i++)
            result += b[i]; int i = 0; int i = 0; i 0 i < n-1 i n-1 n 1 i++ i++ i result += b[i]; result += b[i] result b[i] b i System.out.println(result) System.out.println System.out System System.out result 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[] b = new int[n];
        int result = 0;
        for (int i = 0; i < n; i++)
            a[i] = Integer.parseInt(sc.next());
        for (int i = n-1; i > 0; i--)
            b[i] = a[i] - a[i-1];
        b[0] = k-a[n-1]+a[0];
        Arrays.sort(b);
        for (int i = 0; i < n-1; i++)
            result += b[i];
        System.out.println(result);
    }
} 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[] b = new int[n];
        int result = 0;
        for (int i = 0; i < n; i++)
            a[i] = Integer.parseInt(sc.next());
        for (int i = n-1; i > 0; i--)
            b[i] = a[i] - a[i-1];
        b[0] = k-a[n-1]+a[0];
        Arrays.sort(b);
        for (int i = 0; i < n-1; i++)
            result += b[i];
        System.out.println(result);
    }
} 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 = Integer.parseInt(sc.next());
        int n = Integer.parseInt(sc.next());
        int[] a = new int[n];
        int[] b = new int[n];
        int result = 0;
        for (int i = 0; i < n; i++)
            a[i] = Integer.parseInt(sc.next());
        for (int i = n-1; i > 0; i--)
            b[i] = a[i] - a[i-1];
        b[0] = k-a[n-1]+a[0];
        Arrays.sort(b);
        for (int i = 0; i < n-1; i++)
            result += b[i];
        System.out.println(result);
    }
}