
import java.util.*;
import java.util.stream.Collectors;
import java.io.*;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;

public class Main {
	int K, N;
	int[] A;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		K = Integer.parseInt(tokens[0]);
		N = Integer.parseInt(tokens[1]);
		A = new int[N];
		tokens = in.readLine().split(" ");
		for (int i = 0; i < N; ++i) {
			A[i] = Integer.parseInt(tokens[i]);
		}

	}

	void calc() {
		int result = A[N - 1] - A[0];
		for (int i = 0; i + 1 < N; ++i) {
			result = Math.min(result, K - (A[i + 1] - A[i]));
		}
		System.out.println(result);
	}

	void showResult() {
	}

} import java.util.*; util.* java import java.util.stream.Collectors; Collectors java.util.stream import java.io.*; io.* java import java.math.BigDecimal; BigDecimal java.math import java.nio.charset.StandardCharsets; StandardCharsets java.nio.charset public class Main {
	int K, N;
	int[] A;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		K = Integer.parseInt(tokens[0]);
		N = Integer.parseInt(tokens[1]);
		A = new int[N];
		tokens = in.readLine().split(" ");
		for (int i = 0; i < N; ++i) {
			A[i] = Integer.parseInt(tokens[i]);
		}

	}

	void calc() {
		int result = A[N - 1] - A[0];
		for (int i = 0; i + 1 < N; ++i) {
			result = Math.min(result, K - (A[i + 1] - A[i]));
		}
		System.out.println(result);
	}

	void showResult() {
	}

} Main int K K N; N int[] A; A public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	} main {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	} InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8); reader new InputStreamReader(System.in, StandardCharsets.UTF_8) BufferedReader in = new BufferedReader(reader); in new BufferedReader(reader) Main ins = new Main(in); ins new Main(in) ins.calc() ins.calc ins ins.showResult() ins.showResult ins String[] args args Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		K = Integer.parseInt(tokens[0]);
		N = Integer.parseInt(tokens[1]);
		A = new int[N];
		tokens = in.readLine().split(" ");
		for (int i = 0; i < N; ++i) {
			A[i] = Integer.parseInt(tokens[i]);
		}

	} Main {
		String[] tokens = in.readLine().split(" ");
		K = Integer.parseInt(tokens[0]);
		N = Integer.parseInt(tokens[1]);
		A = new int[N];
		tokens = in.readLine().split(" ");
		for (int i = 0; i < N; ++i) {
			A[i] = Integer.parseInt(tokens[i]);
		}

	} String[] tokens = in.readLine().split(" "); tokens in.readLine().split(" ") in.readLine().split in.readLine() in.readLine in " " K = Integer.parseInt(tokens[0]) K Integer.parseInt(tokens[0]) Integer.parseInt Integer tokens[0] tokens 0 N = Integer.parseInt(tokens[1]) N Integer.parseInt(tokens[1]) Integer.parseInt Integer tokens[1] tokens 1 A = new int[N] A new int[N] N tokens = in.readLine().split(" ") tokens in.readLine().split(" ") in.readLine().split in.readLine() in.readLine in " " for (int i = 0; i < N; ++i) {
			A[i] = Integer.parseInt(tokens[i]);
		} int i = 0; int i = 0; i 0 i < N i N ++i ++i i {
			A[i] = Integer.parseInt(tokens[i]);
		} {
			A[i] = Integer.parseInt(tokens[i]);
		} A[i] = Integer.parseInt(tokens[i]) A[i] A i Integer.parseInt(tokens[i]) Integer.parseInt Integer tokens[i] tokens i BufferedReader in in void calc() {
		int result = A[N - 1] - A[0];
		for (int i = 0; i + 1 < N; ++i) {
			result = Math.min(result, K - (A[i + 1] - A[i]));
		}
		System.out.println(result);
	} calc {
		int result = A[N - 1] - A[0];
		for (int i = 0; i + 1 < N; ++i) {
			result = Math.min(result, K - (A[i + 1] - A[i]));
		}
		System.out.println(result);
	} int result = A[N - 1] - A[0]; result A[N - 1] - A[0] A[N - 1] A N - 1 N 1 A[0] A 0 for (int i = 0; i + 1 < N; ++i) {
			result = Math.min(result, K - (A[i + 1] - A[i]));
		} int i = 0; int i = 0; i 0 i + 1 < N i + 1 i 1 N ++i ++i i {
			result = Math.min(result, K - (A[i + 1] - A[i]));
		} {
			result = Math.min(result, K - (A[i + 1] - A[i]));
		} result = Math.min(result, K - (A[i + 1] - A[i])) result Math.min(result, K - (A[i + 1] - A[i])) Math.min Math result K - (A[i + 1] - A[i]) K (A[i + 1] - A[i]) A[i + 1] A i + 1 i 1 A[i] A i System.out.println(result) System.out.println System.out System System.out result void showResult() {
	} showResult {
	} public class Main {
	int K, N;
	int[] A;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		K = Integer.parseInt(tokens[0]);
		N = Integer.parseInt(tokens[1]);
		A = new int[N];
		tokens = in.readLine().split(" ");
		for (int i = 0; i < N; ++i) {
			A[i] = Integer.parseInt(tokens[i]);
		}

	}

	void calc() {
		int result = A[N - 1] - A[0];
		for (int i = 0; i + 1 < N; ++i) {
			result = Math.min(result, K - (A[i + 1] - A[i]));
		}
		System.out.println(result);
	}

	void showResult() {
	}

} public class Main {
	int K, N;
	int[] A;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		K = Integer.parseInt(tokens[0]);
		N = Integer.parseInt(tokens[1]);
		A = new int[N];
		tokens = in.readLine().split(" ");
		for (int i = 0; i < N; ++i) {
			A[i] = Integer.parseInt(tokens[i]);
		}

	}

	void calc() {
		int result = A[N - 1] - A[0];
		for (int i = 0; i + 1 < N; ++i) {
			result = Math.min(result, K - (A[i + 1] - A[i]));
		}
		System.out.println(result);
	}

	void showResult() {
	}

} Main

import java.util.*;
import java.util.stream.Collectors;
import java.io.*;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;

public class Main {
	int K, N;
	int[] A;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		K = Integer.parseInt(tokens[0]);
		N = Integer.parseInt(tokens[1]);
		A = new int[N];
		tokens = in.readLine().split(" ");
		for (int i = 0; i < N; ++i) {
			A[i] = Integer.parseInt(tokens[i]);
		}

	}

	void calc() {
		int result = A[N - 1] - A[0];
		for (int i = 0; i + 1 < N; ++i) {
			result = Math.min(result, K - (A[i + 1] - A[i]));
		}
		System.out.println(result);
	}

	void showResult() {
	}

}
