import java.util.*;

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

        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int firsta = sc.nextInt();
        int prea = firsta, maxdis = 0, nowa = 0;

        for (int i = 0; i < n - 1; i++) {
            nowa = sc.nextInt();
            maxdis = Math.max(maxdis, nowa - prea);
            prea = nowa;
        }

        maxdis = Math.max(maxdis, firsta + k - nowa);

        System.out.println(k - maxdis);

    }
} import java.util.*; util.* java public class Main {
    static public void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int firsta = sc.nextInt();
        int prea = firsta, maxdis = 0, nowa = 0;

        for (int i = 0; i < n - 1; i++) {
            nowa = sc.nextInt();
            maxdis = Math.max(maxdis, nowa - prea);
            prea = nowa;
        }

        maxdis = Math.max(maxdis, firsta + k - nowa);

        System.out.println(k - maxdis);

    }
} Main static public void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int firsta = sc.nextInt();
        int prea = firsta, maxdis = 0, nowa = 0;

        for (int i = 0; i < n - 1; i++) {
            nowa = sc.nextInt();
            maxdis = Math.max(maxdis, nowa - prea);
            prea = nowa;
        }

        maxdis = Math.max(maxdis, firsta + k - nowa);

        System.out.println(k - maxdis);

    } main {

        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int firsta = sc.nextInt();
        int prea = firsta, maxdis = 0, nowa = 0;

        for (int i = 0; i < n - 1; i++) {
            nowa = sc.nextInt();
            maxdis = Math.max(maxdis, nowa - prea);
            prea = nowa;
        }

        maxdis = Math.max(maxdis, firsta + k - nowa);

        System.out.println(k - maxdis);

    } 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 firsta = sc.nextInt(); firsta sc.nextInt() sc.nextInt sc int prea = firsta prea firsta maxdis = 0 maxdis 0 nowa = 0; nowa 0 for (int i = 0; i < n - 1; i++) {
            nowa = sc.nextInt();
            maxdis = Math.max(maxdis, nowa - prea);
            prea = nowa;
        } int i = 0; int i = 0; i 0 i < n - 1 i n - 1 n 1 i++ i++ i {
            nowa = sc.nextInt();
            maxdis = Math.max(maxdis, nowa - prea);
            prea = nowa;
        } {
            nowa = sc.nextInt();
            maxdis = Math.max(maxdis, nowa - prea);
            prea = nowa;
        } nowa = sc.nextInt() nowa sc.nextInt() sc.nextInt sc maxdis = Math.max(maxdis, nowa - prea) maxdis Math.max(maxdis, nowa - prea) Math.max Math maxdis nowa - prea nowa prea prea = nowa prea nowa maxdis = Math.max(maxdis, firsta + k - nowa) maxdis Math.max(maxdis, firsta + k - nowa) Math.max Math maxdis firsta + k - nowa firsta + k firsta k nowa System.out.println(k - maxdis) System.out.println System.out System System.out k - maxdis k maxdis String args[] args public class Main {
    static public void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int firsta = sc.nextInt();
        int prea = firsta, maxdis = 0, nowa = 0;

        for (int i = 0; i < n - 1; i++) {
            nowa = sc.nextInt();
            maxdis = Math.max(maxdis, nowa - prea);
            prea = nowa;
        }

        maxdis = Math.max(maxdis, firsta + k - nowa);

        System.out.println(k - maxdis);

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

        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int firsta = sc.nextInt();
        int prea = firsta, maxdis = 0, nowa = 0;

        for (int i = 0; i < n - 1; i++) {
            nowa = sc.nextInt();
            maxdis = Math.max(maxdis, nowa - prea);
            prea = nowa;
        }

        maxdis = Math.max(maxdis, firsta + k - nowa);

        System.out.println(k - maxdis);

    }
} Main
import java.util.*;

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

        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int firsta = sc.nextInt();
        int prea = firsta, maxdis = 0, nowa = 0;

        for (int i = 0; i < n - 1; i++) {
            nowa = sc.nextInt();
            maxdis = Math.max(maxdis, nowa - prea);
            prea = nowa;
        }

        maxdis = Math.max(maxdis, firsta + k - nowa);

        System.out.println(k - maxdis);

    }
}