import 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];
        int[] diff = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        for (int j = 1; j < n; j++) {
            diff[j - 1] = a[j] - a[j - 1];
        }
        diff[n - 1] = k - a[n - 1] + a[0];
        Arrays.sort(diff);
        System.out.println(k - diff[n - 1]);
    }
} import java.util.*; util.* java 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];
        int[] diff = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        for (int j = 1; j < n; j++) {
            diff[j - 1] = a[j] - a[j - 1];
        }
        diff[n - 1] = k - a[n - 1] + a[0];
        Arrays.sort(diff);
        System.out.println(k - diff[n - 1]);
    }
} 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];
        int[] diff = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        for (int j = 1; j < n; j++) {
            diff[j - 1] = a[j] - a[j - 1];
        }
        diff[n - 1] = k - a[n - 1] + a[0];
        Arrays.sort(diff);
        System.out.println(k - diff[n - 1]);
    } main {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] diff = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        for (int j = 1; j < n; j++) {
            diff[j - 1] = a[j] - a[j - 1];
        }
        diff[n - 1] = k - a[n - 1] + a[0];
        Arrays.sort(diff);
        System.out.println(k - diff[n - 1]);
    } 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 int[] diff = new int[n]; diff 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 j = 1; j < n; j++) {
            diff[j - 1] = a[j] - a[j - 1];
        } int j = 1; int j = 1; j 1 j < n j n j++ j++ j {
            diff[j - 1] = a[j] - a[j - 1];
        } {
            diff[j - 1] = a[j] - a[j - 1];
        } diff[j - 1] = a[j] - a[j - 1] diff[j - 1] diff j - 1 j 1 a[j] - a[j - 1] a[j] a j a[j - 1] a j - 1 j 1 diff[n - 1] = k - a[n - 1] + a[0] diff[n - 1] diff n - 1 n 1 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(diff) Arrays.sort Arrays diff System.out.println(k - diff[n - 1]) System.out.println System.out System System.out k - diff[n - 1] k diff[n - 1] diff n - 1 n 1 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];
        int[] diff = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        for (int j = 1; j < n; j++) {
            diff[j - 1] = a[j] - a[j - 1];
        }
        diff[n - 1] = k - a[n - 1] + a[0];
        Arrays.sort(diff);
        System.out.println(k - diff[n - 1]);
    }
} 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];
        int[] diff = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        for (int j = 1; j < n; j++) {
            diff[j - 1] = a[j] - a[j - 1];
        }
        diff[n - 1] = k - a[n - 1] + a[0];
        Arrays.sort(diff);
        System.out.println(k - diff[n - 1]);
    }
} Main
import 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];
        int[] diff = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        for (int j = 1; j < n; j++) {
            diff[j - 1] = a[j] - a[j - 1];
        }
        diff[n - 1] = k - a[n - 1] + a[0];
        Arrays.sort(diff);
        System.out.println(k - diff[n - 1]);
    }
}