import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;

public class Main {
    public static void main (String[] args) throws FileNotFoundException {

        Scanner sc =  new Scanner(System.in);

        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] res = new int[n];
        for (int i =0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        for (int i = 1; i < n; i++) {
            res[i] = a[i] - a[i - 1];
        }
        res[0] = (k - a[n - 1]) + a[0];

        int in = 0;
        for (int i = 1; i < n; i++) {
            if (Math.max(res[in], res[i]) == res[i]) {
                in = i;
            }
        }
        System.out.println(k - res[in]);
    }
} import java.io.File; File java.io import java.io.FileNotFoundException; FileNotFoundException java.io import java.io.PrintStream; PrintStream java.io import java.util.Scanner; Scanner java.util public class Main {
    public static void main (String[] args) throws FileNotFoundException {

        Scanner sc =  new Scanner(System.in);

        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] res = new int[n];
        for (int i =0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        for (int i = 1; i < n; i++) {
            res[i] = a[i] - a[i - 1];
        }
        res[0] = (k - a[n - 1]) + a[0];

        int in = 0;
        for (int i = 1; i < n; i++) {
            if (Math.max(res[in], res[i]) == res[i]) {
                in = i;
            }
        }
        System.out.println(k - res[in]);
    }
} Main public static void main (String[] args) throws FileNotFoundException {

        Scanner sc =  new Scanner(System.in);

        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] res = new int[n];
        for (int i =0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        for (int i = 1; i < n; i++) {
            res[i] = a[i] - a[i - 1];
        }
        res[0] = (k - a[n - 1]) + a[0];

        int in = 0;
        for (int i = 1; i < n; i++) {
            if (Math.max(res[in], res[i]) == res[i]) {
                in = i;
            }
        }
        System.out.println(k - res[in]);
    } main {

        Scanner sc =  new Scanner(System.in);

        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] res = new int[n];
        for (int i =0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        for (int i = 1; i < n; i++) {
            res[i] = a[i] - a[i - 1];
        }
        res[0] = (k - a[n - 1]) + a[0];

        int in = 0;
        for (int i = 1; i < n; i++) {
            if (Math.max(res[in], res[i]) == res[i]) {
                in = i;
            }
        }
        System.out.println(k - res[in]);
    } 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[] res = new int[n]; res 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 i = 1; i < n; i++) {
            res[i] = a[i] - a[i - 1];
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            res[i] = a[i] - a[i - 1];
        } {
            res[i] = a[i] - a[i - 1];
        } res[i] = a[i] - a[i - 1] res[i] res i a[i] - a[i - 1] a[i] a i a[i - 1] a i - 1 i 1 res[0] = (k - a[n - 1]) + a[0] res[0] res 0 (k - a[n - 1]) + a[0] (k - a[n - 1]) k a[n - 1] a n - 1 n 1 a[0] a 0 int in = 0; in 0 for (int i = 1; i < n; i++) {
            if (Math.max(res[in], res[i]) == res[i]) {
                in = i;
            }
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            if (Math.max(res[in], res[i]) == res[i]) {
                in = i;
            }
        } {
            if (Math.max(res[in], res[i]) == res[i]) {
                in = i;
            }
        } if (Math.max(res[in], res[i]) == res[i]) {
                in = i;
            } Math.max(res[in], res[i]) == res[i] Math.max(res[in], res[i]) Math.max Math res[in] res in res[i] res i res[i] res i {
                in = i;
            } in = i in i System.out.println(k - res[in]) System.out.println System.out System System.out k - res[in] k res[in] res in String[] args args public class Main {
    public static void main (String[] args) throws FileNotFoundException {

        Scanner sc =  new Scanner(System.in);

        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] res = new int[n];
        for (int i =0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        for (int i = 1; i < n; i++) {
            res[i] = a[i] - a[i - 1];
        }
        res[0] = (k - a[n - 1]) + a[0];

        int in = 0;
        for (int i = 1; i < n; i++) {
            if (Math.max(res[in], res[i]) == res[i]) {
                in = i;
            }
        }
        System.out.println(k - res[in]);
    }
} public class Main {
    public static void main (String[] args) throws FileNotFoundException {

        Scanner sc =  new Scanner(System.in);

        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] res = new int[n];
        for (int i =0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        for (int i = 1; i < n; i++) {
            res[i] = a[i] - a[i - 1];
        }
        res[0] = (k - a[n - 1]) + a[0];

        int in = 0;
        for (int i = 1; i < n; i++) {
            if (Math.max(res[in], res[i]) == res[i]) {
                in = i;
            }
        }
        System.out.println(k - res[in]);
    }
} Main
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;

public class Main {
    public static void main (String[] args) throws FileNotFoundException {

        Scanner sc =  new Scanner(System.in);

        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] res = new int[n];
        for (int i =0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        for (int i = 1; i < n; i++) {
            res[i] = a[i] - a[i - 1];
        }
        res[0] = (k - a[n - 1]) + a[0];

        int in = 0;
        for (int i = 1; i < n; i++) {
            if (Math.max(res[in], res[i]) == res[i]) {
                in = i;
            }
        }
        System.out.println(k - res[in]);
    }
}
