import java.util.*;

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];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        
        int min = 2 * k;
        for (int i = 0; i < n; i++) {
            int tmp = 0;

            // cw
            int l = i - 1;
            if (l < 0) {
                l = n - 1;
                tmp = a[l] - a[i];
            } else {
                tmp = k - a[i] + a[l];
            }
            min = Math.min(min, tmp);

            // ccw
            int r = i + 1;
            if (r >= n) {
                r = 0;
                tmp = a[i] - a[r];
            } else {
                tmp = a[i] + k - a[r];
            }
            min = Math.min(min, tmp);
        }
        
        System.out.println(min);
    }
} import java.util.*; util.* java 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];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        
        int min = 2 * k;
        for (int i = 0; i < n; i++) {
            int tmp = 0;

            // cw
            int l = i - 1;
            if (l < 0) {
                l = n - 1;
                tmp = a[l] - a[i];
            } else {
                tmp = k - a[i] + a[l];
            }
            min = Math.min(min, tmp);

            // ccw
            int r = i + 1;
            if (r >= n) {
                r = 0;
                tmp = a[i] - a[r];
            } else {
                tmp = a[i] + k - a[r];
            }
            min = Math.min(min, tmp);
        }
        
        System.out.println(min);
    }
} 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];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        
        int min = 2 * k;
        for (int i = 0; i < n; i++) {
            int tmp = 0;

            // cw
            int l = i - 1;
            if (l < 0) {
                l = n - 1;
                tmp = a[l] - a[i];
            } else {
                tmp = k - a[i] + a[l];
            }
            min = Math.min(min, tmp);

            // ccw
            int r = i + 1;
            if (r >= n) {
                r = 0;
                tmp = a[i] - a[r];
            } else {
                tmp = a[i] + k - a[r];
            }
            min = Math.min(min, tmp);
        }
        
        System.out.println(min);
    } main {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        
        int min = 2 * k;
        for (int i = 0; i < n; i++) {
            int tmp = 0;

            // cw
            int l = i - 1;
            if (l < 0) {
                l = n - 1;
                tmp = a[l] - a[i];
            } else {
                tmp = k - a[i] + a[l];
            }
            min = Math.min(min, tmp);

            // ccw
            int r = i + 1;
            if (r >= n) {
                r = 0;
                tmp = a[i] - a[r];
            } else {
                tmp = a[i] + k - a[r];
            }
            min = Math.min(min, tmp);
        }
        
        System.out.println(min);
    } 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 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 int min = 2 * k; min 2 * k 2 k for (int i = 0; i < n; i++) {
            int tmp = 0;

            // cw
            int l = i - 1;
            if (l < 0) {
                l = n - 1;
                tmp = a[l] - a[i];
            } else {
                tmp = k - a[i] + a[l];
            }
            min = Math.min(min, tmp);

            // ccw
            int r = i + 1;
            if (r >= n) {
                r = 0;
                tmp = a[i] - a[r];
            } else {
                tmp = a[i] + k - a[r];
            }
            min = Math.min(min, tmp);
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            int tmp = 0;

            // cw
            int l = i - 1;
            if (l < 0) {
                l = n - 1;
                tmp = a[l] - a[i];
            } else {
                tmp = k - a[i] + a[l];
            }
            min = Math.min(min, tmp);

            // ccw
            int r = i + 1;
            if (r >= n) {
                r = 0;
                tmp = a[i] - a[r];
            } else {
                tmp = a[i] + k - a[r];
            }
            min = Math.min(min, tmp);
        } {
            int tmp = 0;

            // cw
            int l = i - 1;
            if (l < 0) {
                l = n - 1;
                tmp = a[l] - a[i];
            } else {
                tmp = k - a[i] + a[l];
            }
            min = Math.min(min, tmp);

            // ccw
            int r = i + 1;
            if (r >= n) {
                r = 0;
                tmp = a[i] - a[r];
            } else {
                tmp = a[i] + k - a[r];
            }
            min = Math.min(min, tmp);
        } int tmp = 0; tmp 0 int l = i - 1; l i - 1 i 1 if (l < 0) {
                l = n - 1;
                tmp = a[l] - a[i];
            } else {
                tmp = k - a[i] + a[l];
            } l < 0 l 0 {
                l = n - 1;
                tmp = a[l] - a[i];
            } l = n - 1 l n - 1 n 1 tmp = a[l] - a[i] tmp a[l] - a[i] a[l] a l a[i] a i {
                tmp = k - a[i] + a[l];
            } tmp = k - a[i] + a[l] tmp k - a[i] + a[l] k - a[i] k a[i] a i a[l] a l min = Math.min(min, tmp) min Math.min(min, tmp) Math.min Math min tmp int r = i + 1; r i + 1 i 1 if (r >= n) {
                r = 0;
                tmp = a[i] - a[r];
            } else {
                tmp = a[i] + k - a[r];
            } r >= n r n {
                r = 0;
                tmp = a[i] - a[r];
            } r = 0 r 0 tmp = a[i] - a[r] tmp a[i] - a[r] a[i] a i a[r] a r {
                tmp = a[i] + k - a[r];
            } tmp = a[i] + k - a[r] tmp a[i] + k - a[r] a[i] + k a[i] a i k a[r] a r min = Math.min(min, tmp) min Math.min(min, tmp) Math.min Math min tmp System.out.println(min) System.out.println System.out System System.out min String[] args args
import java.util.*;

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];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        
        int min = 2 * k;
        for (int i = 0; i < n; i++) {
            int tmp = 0;

            // cw
            int l = i - 1;
            if (l < 0) {
                l = n - 1;
                tmp = a[l] - a[i];
            } else {
                tmp = k - a[i] + a[l];
            }
            min = Math.min(min, tmp);

            // ccw
            int r = i + 1;
            if (r >= n) {
                r = 0;
                tmp = a[i] - a[r];
            } else {
                tmp = a[i] + k - a[r];
            }
            min = Math.min(min, tmp);
        }
        
        System.out.println(min);
    }
}