import java.util.*;
import java.util.stream.IntStream;

public class Main {

    public static void main(String[] args) {
        // write your code here
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] dist = new int[n];
        int a0 = sc.nextInt();
        int prev = a0;

        for (int i = 0; i < n - 1; i++) {
            int now = sc.nextInt();
            dist[i] = now - prev;
            prev = now;
        }
        dist[n - 1] = k + a0 - prev;

        System.out.println(k - IntStream.of(dist).max().orElse(0));
    }
} import java.util.*; util.* java import java.util.stream.IntStream; IntStream java.util.stream public class Main {

    public static void main(String[] args) {
        // write your code here
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] dist = new int[n];
        int a0 = sc.nextInt();
        int prev = a0;

        for (int i = 0; i < n - 1; i++) {
            int now = sc.nextInt();
            dist[i] = now - prev;
            prev = now;
        }
        dist[n - 1] = k + a0 - prev;

        System.out.println(k - IntStream.of(dist).max().orElse(0));
    }
} Main public static void main(String[] args) {
        // write your code here
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] dist = new int[n];
        int a0 = sc.nextInt();
        int prev = a0;

        for (int i = 0; i < n - 1; i++) {
            int now = sc.nextInt();
            dist[i] = now - prev;
            prev = now;
        }
        dist[n - 1] = k + a0 - prev;

        System.out.println(k - IntStream.of(dist).max().orElse(0));
    } main {
        // write your code here
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] dist = new int[n];
        int a0 = sc.nextInt();
        int prev = a0;

        for (int i = 0; i < n - 1; i++) {
            int now = sc.nextInt();
            dist[i] = now - prev;
            prev = now;
        }
        dist[n - 1] = k + a0 - prev;

        System.out.println(k - IntStream.of(dist).max().orElse(0));
    } 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[] dist = new int[n]; dist new int[n] n int a0 = sc.nextInt(); a0 sc.nextInt() sc.nextInt sc int prev = a0; prev a0 for (int i = 0; i < n - 1; i++) {
            int now = sc.nextInt();
            dist[i] = now - prev;
            prev = now;
        } int i = 0; int i = 0; i 0 i < n - 1 i n - 1 n 1 i++ i++ i {
            int now = sc.nextInt();
            dist[i] = now - prev;
            prev = now;
        } {
            int now = sc.nextInt();
            dist[i] = now - prev;
            prev = now;
        } int now = sc.nextInt(); now sc.nextInt() sc.nextInt sc dist[i] = now - prev dist[i] dist i now - prev now prev prev = now prev now dist[n - 1] = k + a0 - prev dist[n - 1] dist n - 1 n 1 k + a0 - prev k + a0 k a0 prev System.out.println(k - IntStream.of(dist).max().orElse(0)) System.out.println System.out System System.out k - IntStream.of(dist).max().orElse(0) k IntStream.of(dist).max().orElse(0) IntStream.of(dist).max().orElse IntStream.of(dist).max() IntStream.of(dist).max IntStream.of(dist) IntStream.of IntStream dist 0 String[] args args public class Main {

    public static void main(String[] args) {
        // write your code here
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] dist = new int[n];
        int a0 = sc.nextInt();
        int prev = a0;

        for (int i = 0; i < n - 1; i++) {
            int now = sc.nextInt();
            dist[i] = now - prev;
            prev = now;
        }
        dist[n - 1] = k + a0 - prev;

        System.out.println(k - IntStream.of(dist).max().orElse(0));
    }
} public class Main {

    public static void main(String[] args) {
        // write your code here
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] dist = new int[n];
        int a0 = sc.nextInt();
        int prev = a0;

        for (int i = 0; i < n - 1; i++) {
            int now = sc.nextInt();
            dist[i] = now - prev;
            prev = now;
        }
        dist[n - 1] = k + a0 - prev;

        System.out.println(k - IntStream.of(dist).max().orElse(0));
    }
} Main
import java.util.*;
import java.util.stream.IntStream;

public class Main {

    public static void main(String[] args) {
        // write your code here
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] dist = new int[n];
        int a0 = sc.nextInt();
        int prev = a0;

        for (int i = 0; i < n - 1; i++) {
            int now = sc.nextInt();
            dist[i] = now - prev;
            prev = now;
        }
        dist[n - 1] = k + a0 - prev;

        System.out.println(k - IntStream.of(dist).max().orElse(0));
    }
}
