
import java.util.Scanner;

class Main {

    public int foo(int K, int N, int[] dist) {
        int last = K;
        int max = 0;
        for (int i = 1; i < N; i++) {
            int d = dist[i]-dist[i-1];
            max = Math.max(max, d);
            last -= d;
        }
        max = Math.max(max, last);
        return K - max;
    }

	public static void main(String[] args){
        Main o = new Main();

		Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] arr = new int[N];
        for (int i = 0; i < N; i++) {
            arr[i] = sc.nextInt();
        }
        // sc.close();

        System.out.println(o.foo(K, N, arr));
	}
} import java.util.Scanner; Scanner java.util class Main {

    public int foo(int K, int N, int[] dist) {
        int last = K;
        int max = 0;
        for (int i = 1; i < N; i++) {
            int d = dist[i]-dist[i-1];
            max = Math.max(max, d);
            last -= d;
        }
        max = Math.max(max, last);
        return K - max;
    }

	public static void main(String[] args){
        Main o = new Main();

		Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] arr = new int[N];
        for (int i = 0; i < N; i++) {
            arr[i] = sc.nextInt();
        }
        // sc.close();

        System.out.println(o.foo(K, N, arr));
	}
} Main public int foo(int K, int N, int[] dist) {
        int last = K;
        int max = 0;
        for (int i = 1; i < N; i++) {
            int d = dist[i]-dist[i-1];
            max = Math.max(max, d);
            last -= d;
        }
        max = Math.max(max, last);
        return K - max;
    } foo {
        int last = K;
        int max = 0;
        for (int i = 1; i < N; i++) {
            int d = dist[i]-dist[i-1];
            max = Math.max(max, d);
            last -= d;
        }
        max = Math.max(max, last);
        return K - max;
    } int last = K; last K int max = 0; max 0 for (int i = 1; i < N; i++) {
            int d = dist[i]-dist[i-1];
            max = Math.max(max, d);
            last -= d;
        } int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
            int d = dist[i]-dist[i-1];
            max = Math.max(max, d);
            last -= d;
        } {
            int d = dist[i]-dist[i-1];
            max = Math.max(max, d);
            last -= d;
        } int d = dist[i]-dist[i-1]; d dist[i]-dist[i-1] dist[i] dist i dist[i-1] dist i-1 i 1 max = Math.max(max, d) max Math.max(max, d) Math.max Math max d last -= d last d max = Math.max(max, last) max Math.max(max, last) Math.max Math max last return K - max; K - max K max int K K int N N int[] dist dist public static void main(String[] args){
        Main o = new Main();

		Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] arr = new int[N];
        for (int i = 0; i < N; i++) {
            arr[i] = sc.nextInt();
        }
        // sc.close();

        System.out.println(o.foo(K, N, arr));
	} main {
        Main o = new Main();

		Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] arr = new int[N];
        for (int i = 0; i < N; i++) {
            arr[i] = sc.nextInt();
        }
        // sc.close();

        System.out.println(o.foo(K, N, arr));
	} Main o = new Main(); o new Main() 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[] arr = new int[N]; arr new int[N] N for (int i = 0; i < N; i++) {
            arr[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            arr[i] = sc.nextInt();
        } {
            arr[i] = sc.nextInt();
        } arr[i] = sc.nextInt() arr[i] arr i sc.nextInt() sc.nextInt sc System.out.println(o.foo(K, N, arr)) System.out.println System.out System System.out o.foo(K, N, arr) o.foo o K N arr String[] args args

import java.util.Scanner;

class Main {

    public int foo(int K, int N, int[] dist) {
        int last = K;
        int max = 0;
        for (int i = 1; i < N; i++) {
            int d = dist[i]-dist[i-1];
            max = Math.max(max, d);
            last -= d;
        }
        max = Math.max(max, last);
        return K - max;
    }

	public static void main(String[] args){
        Main o = new Main();

		Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] arr = new int[N];
        for (int i = 0; i < N; i++) {
            arr[i] = sc.nextInt();
        }
        // sc.close();

        System.out.println(o.foo(K, N, arr));
	}
}
