import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int K;
        int N;
        int[] Ai;
        try (Scanner sc = new Scanner(System.in)) {
            K = sc.nextInt();
            N = sc.nextInt();
            Ai = new int[N];
            for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            }
        }
        int dist = Ai[0] + (K-Ai[N-1]);
        for (int i = 0; i < N-1; i++) {
            if(dist < Ai[i+1] -Ai[i]){
                dist = Ai[i+1] -Ai[i];
            };
        }
        System.out.println(K - dist);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        int K;
        int N;
        int[] Ai;
        try (Scanner sc = new Scanner(System.in)) {
            K = sc.nextInt();
            N = sc.nextInt();
            Ai = new int[N];
            for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            }
        }
        int dist = Ai[0] + (K-Ai[N-1]);
        for (int i = 0; i < N-1; i++) {
            if(dist < Ai[i+1] -Ai[i]){
                dist = Ai[i+1] -Ai[i];
            };
        }
        System.out.println(K - dist);
    }
} Main public static void main(String[] args) {
        int K;
        int N;
        int[] Ai;
        try (Scanner sc = new Scanner(System.in)) {
            K = sc.nextInt();
            N = sc.nextInt();
            Ai = new int[N];
            for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            }
        }
        int dist = Ai[0] + (K-Ai[N-1]);
        for (int i = 0; i < N-1; i++) {
            if(dist < Ai[i+1] -Ai[i]){
                dist = Ai[i+1] -Ai[i];
            };
        }
        System.out.println(K - dist);
    } main {
        int K;
        int N;
        int[] Ai;
        try (Scanner sc = new Scanner(System.in)) {
            K = sc.nextInt();
            N = sc.nextInt();
            Ai = new int[N];
            for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            }
        }
        int dist = Ai[0] + (K-Ai[N-1]);
        for (int i = 0; i < N-1; i++) {
            if(dist < Ai[i+1] -Ai[i]){
                dist = Ai[i+1] -Ai[i];
            };
        }
        System.out.println(K - dist);
    } int K; K int N; N int[] Ai; Ai try (Scanner sc = new Scanner(System.in)) {
            K = sc.nextInt();
            N = sc.nextInt();
            Ai = new int[N];
            for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            }
        } {
            K = sc.nextInt();
            N = sc.nextInt();
            Ai = new int[N];
            for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            }
        } Scanner sc = new Scanner(System.in) Scanner sc = new Scanner(System.in) new Scanner(System.in) K = sc.nextInt() K sc.nextInt() sc.nextInt sc N = sc.nextInt() N sc.nextInt() sc.nextInt sc Ai = new int[N] Ai new int[N] N for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
                Ai[i] = sc.nextInt();
            } {
                Ai[i] = sc.nextInt();
            } Ai[i] = sc.nextInt() Ai[i] Ai i sc.nextInt() sc.nextInt sc int dist = Ai[0] + (K-Ai[N-1]); dist Ai[0] + (K-Ai[N-1]) Ai[0] Ai 0 (K-Ai[N-1]) K Ai[N-1] Ai N-1 N 1 for (int i = 0; i < N-1; i++) {
            if(dist < Ai[i+1] -Ai[i]){
                dist = Ai[i+1] -Ai[i];
            };
        } int i = 0; int i = 0; i 0 i < N-1 i N-1 N 1 i++ i++ i {
            if(dist < Ai[i+1] -Ai[i]){
                dist = Ai[i+1] -Ai[i];
            };
        } {
            if(dist < Ai[i+1] -Ai[i]){
                dist = Ai[i+1] -Ai[i];
            };
        } if(dist < Ai[i+1] -Ai[i]){
                dist = Ai[i+1] -Ai[i];
            } dist < Ai[i+1] -Ai[i] dist Ai[i+1] -Ai[i] Ai[i+1] Ai i+1 i 1 Ai[i] Ai i {
                dist = Ai[i+1] -Ai[i];
            } dist = Ai[i+1] -Ai[i] dist Ai[i+1] -Ai[i] Ai[i+1] Ai i+1 i 1 Ai[i] Ai i System.out.println(K - dist) System.out.println System.out System System.out K - dist K dist String[] args args public class Main {
    public static void main(String[] args) {
        int K;
        int N;
        int[] Ai;
        try (Scanner sc = new Scanner(System.in)) {
            K = sc.nextInt();
            N = sc.nextInt();
            Ai = new int[N];
            for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            }
        }
        int dist = Ai[0] + (K-Ai[N-1]);
        for (int i = 0; i < N-1; i++) {
            if(dist < Ai[i+1] -Ai[i]){
                dist = Ai[i+1] -Ai[i];
            };
        }
        System.out.println(K - dist);
    }
} public class Main {
    public static void main(String[] args) {
        int K;
        int N;
        int[] Ai;
        try (Scanner sc = new Scanner(System.in)) {
            K = sc.nextInt();
            N = sc.nextInt();
            Ai = new int[N];
            for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            }
        }
        int dist = Ai[0] + (K-Ai[N-1]);
        for (int i = 0; i < N-1; i++) {
            if(dist < Ai[i+1] -Ai[i]){
                dist = Ai[i+1] -Ai[i];
            };
        }
        System.out.println(K - dist);
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int K;
        int N;
        int[] Ai;
        try (Scanner sc = new Scanner(System.in)) {
            K = sc.nextInt();
            N = sc.nextInt();
            Ai = new int[N];
            for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            }
        }
        int dist = Ai[0] + (K-Ai[N-1]);
        for (int i = 0; i < N-1; i++) {
            if(dist < Ai[i+1] -Ai[i]){
                dist = Ai[i+1] -Ai[i];
            };
        }
        System.out.println(K - dist);
    }
}
