import java.io.IOException;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        int k = in.nextInt();
        int n = in.nextInt();
        int[] arr = new int[n];
        int[] ans = new int[n];

        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }
        for (int i = 1; i < n; i++) {
            ans[i] = arr[i] - arr[i - 1];
        }
        ans[0] = k - arr[n - 1] + arr[0];

        int temp = 0;
        for (int i = 1; i < n; i++) {
            if (Math.max(ans[temp], ans[i]) == ans[i]) {
                temp = i;
            }
        }
        System.out.println(k - ans[temp]);
    }
} import java.io.IOException; IOException java.io import java.util.*; util.* java public class Main {

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        int k = in.nextInt();
        int n = in.nextInt();
        int[] arr = new int[n];
        int[] ans = new int[n];

        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }
        for (int i = 1; i < n; i++) {
            ans[i] = arr[i] - arr[i - 1];
        }
        ans[0] = k - arr[n - 1] + arr[0];

        int temp = 0;
        for (int i = 1; i < n; i++) {
            if (Math.max(ans[temp], ans[i]) == ans[i]) {
                temp = i;
            }
        }
        System.out.println(k - ans[temp]);
    }
} Main public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        int k = in.nextInt();
        int n = in.nextInt();
        int[] arr = new int[n];
        int[] ans = new int[n];

        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }
        for (int i = 1; i < n; i++) {
            ans[i] = arr[i] - arr[i - 1];
        }
        ans[0] = k - arr[n - 1] + arr[0];

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

        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }
        for (int i = 1; i < n; i++) {
            ans[i] = arr[i] - arr[i - 1];
        }
        ans[0] = k - arr[n - 1] + arr[0];

        int temp = 0;
        for (int i = 1; i < n; i++) {
            if (Math.max(ans[temp], ans[i]) == ans[i]) {
                temp = i;
            }
        }
        System.out.println(k - ans[temp]);
    } Scanner in = new Scanner(System.in); in new Scanner(System.in) int k = in.nextInt(); k in.nextInt() in.nextInt in int n = in.nextInt(); n in.nextInt() in.nextInt in int[] arr = new int[n]; arr new int[n] n int[] ans = new int[n]; ans new int[n] n for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            arr[i] = in.nextInt();
        } {
            arr[i] = in.nextInt();
        } arr[i] = in.nextInt() arr[i] arr i in.nextInt() in.nextInt in for (int i = 1; i < n; i++) {
            ans[i] = arr[i] - arr[i - 1];
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            ans[i] = arr[i] - arr[i - 1];
        } {
            ans[i] = arr[i] - arr[i - 1];
        } ans[i] = arr[i] - arr[i - 1] ans[i] ans i arr[i] - arr[i - 1] arr[i] arr i arr[i - 1] arr i - 1 i 1 ans[0] = k - arr[n - 1] + arr[0] ans[0] ans 0 k - arr[n - 1] + arr[0] k - arr[n - 1] k arr[n - 1] arr n - 1 n 1 arr[0] arr 0 int temp = 0; temp 0 for (int i = 1; i < n; i++) {
            if (Math.max(ans[temp], ans[i]) == ans[i]) {
                temp = i;
            }
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            if (Math.max(ans[temp], ans[i]) == ans[i]) {
                temp = i;
            }
        } {
            if (Math.max(ans[temp], ans[i]) == ans[i]) {
                temp = i;
            }
        } if (Math.max(ans[temp], ans[i]) == ans[i]) {
                temp = i;
            } Math.max(ans[temp], ans[i]) == ans[i] Math.max(ans[temp], ans[i]) Math.max Math ans[temp] ans temp ans[i] ans i ans[i] ans i {
                temp = i;
            } temp = i temp i System.out.println(k - ans[temp]) System.out.println System.out System System.out k - ans[temp] k ans[temp] ans temp String[] args args public class Main {

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        int k = in.nextInt();
        int n = in.nextInt();
        int[] arr = new int[n];
        int[] ans = new int[n];

        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }
        for (int i = 1; i < n; i++) {
            ans[i] = arr[i] - arr[i - 1];
        }
        ans[0] = k - arr[n - 1] + arr[0];

        int temp = 0;
        for (int i = 1; i < n; i++) {
            if (Math.max(ans[temp], ans[i]) == ans[i]) {
                temp = i;
            }
        }
        System.out.println(k - ans[temp]);
    }
} public class Main {

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        int k = in.nextInt();
        int n = in.nextInt();
        int[] arr = new int[n];
        int[] ans = new int[n];

        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }
        for (int i = 1; i < n; i++) {
            ans[i] = arr[i] - arr[i - 1];
        }
        ans[0] = k - arr[n - 1] + arr[0];

        int temp = 0;
        for (int i = 1; i < n; i++) {
            if (Math.max(ans[temp], ans[i]) == ans[i]) {
                temp = i;
            }
        }
        System.out.println(k - ans[temp]);
    }
} Main
import java.io.IOException;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        int k = in.nextInt();
        int n = in.nextInt();
        int[] arr = new int[n];
        int[] ans = new int[n];

        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }
        for (int i = 1; i < n; i++) {
            ans[i] = arr[i] - arr[i - 1];
        }
        ans[0] = k - arr[n - 1] + arr[0];

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