import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Main {
	public static void main(String args[]) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		Solver solver = new Solver();
		solver.init();
		solver.readHead(in.readLine());
		solver.readA(in.readLine());
		// for (; solver.hasNext();) {
		// solver.readBody(in.readLine());
		// }
		// solver.solve();
	}
}

class Solver {
	int N;
	int cnt;

	public void init() {
		N = 0;
		cnt = 0;
	}

	public void readHead(String str) {
		String[] strArr = str.split("\\s+");
		K = Integer.parseInt(strArr[0]);
		N = Integer.parseInt(strArr[1]);
		// System.out.println(N);
	}

	int K;

	public void readA(String str) {
		String[] strArr = str.split("\\s+");
		int[] A = new int[N + 1];
		for (int i = 1; i <= N; i++) {
			A[i] = Integer.parseInt(strArr[i - 1]);
		}
		A[0] = A[N] - K;
		int dMax = A[1] - A[0];
		int start = 1;
		for (int i = 1; i <= N; i++) {
			if (A[i] - A[i - 1] > dMax) {
				dMax = A[i] - A[i - 1];
				start = i;
			}
		}
		System.out.println(K - dMax);
	}

	public boolean hasNext() {
		return cnt < N;
	}

	public void readBody(String str) {
		// System.out.println(str);
		cnt++;
	}

	public void solve() {

	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io class Main {
	public static void main(String args[]) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		Solver solver = new Solver();
		solver.init();
		solver.readHead(in.readLine());
		solver.readA(in.readLine());
		// for (; solver.hasNext();) {
		// solver.readBody(in.readLine());
		// }
		// solver.solve();
	}
} Main public static void main(String args[]) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		Solver solver = new Solver();
		solver.init();
		solver.readHead(in.readLine());
		solver.readA(in.readLine());
		// for (; solver.hasNext();) {
		// solver.readBody(in.readLine());
		// }
		// solver.solve();
	} main {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		Solver solver = new Solver();
		solver.init();
		solver.readHead(in.readLine());
		solver.readA(in.readLine());
		// for (; solver.hasNext();) {
		// solver.readBody(in.readLine());
		// }
		// solver.solve();
	} BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); in new BufferedReader(new InputStreamReader(System.in)) Solver solver = new Solver(); solver new Solver() solver.init() solver.init solver solver.readHead(in.readLine()) solver.readHead solver in.readLine() in.readLine in solver.readA(in.readLine()) solver.readA solver in.readLine() in.readLine in String args[] args class Solver {
	int N;
	int cnt;

	public void init() {
		N = 0;
		cnt = 0;
	}

	public void readHead(String str) {
		String[] strArr = str.split("\\s+");
		K = Integer.parseInt(strArr[0]);
		N = Integer.parseInt(strArr[1]);
		// System.out.println(N);
	}

	int K;

	public void readA(String str) {
		String[] strArr = str.split("\\s+");
		int[] A = new int[N + 1];
		for (int i = 1; i <= N; i++) {
			A[i] = Integer.parseInt(strArr[i - 1]);
		}
		A[0] = A[N] - K;
		int dMax = A[1] - A[0];
		int start = 1;
		for (int i = 1; i <= N; i++) {
			if (A[i] - A[i - 1] > dMax) {
				dMax = A[i] - A[i - 1];
				start = i;
			}
		}
		System.out.println(K - dMax);
	}

	public boolean hasNext() {
		return cnt < N;
	}

	public void readBody(String str) {
		// System.out.println(str);
		cnt++;
	}

	public void solve() {

	}
} Solver int N; N int cnt; cnt int K; K public void init() {
		N = 0;
		cnt = 0;
	} init {
		N = 0;
		cnt = 0;
	} N = 0 N 0 cnt = 0 cnt 0 public void readHead(String str) {
		String[] strArr = str.split("\\s+");
		K = Integer.parseInt(strArr[0]);
		N = Integer.parseInt(strArr[1]);
		// System.out.println(N);
	} readHead {
		String[] strArr = str.split("\\s+");
		K = Integer.parseInt(strArr[0]);
		N = Integer.parseInt(strArr[1]);
		// System.out.println(N);
	} String[] strArr = str.split("\\s+"); strArr str.split("\\s+") str.split str "\\s+" K = Integer.parseInt(strArr[0]) K Integer.parseInt(strArr[0]) Integer.parseInt Integer strArr[0] strArr 0 N = Integer.parseInt(strArr[1]) N Integer.parseInt(strArr[1]) Integer.parseInt Integer strArr[1] strArr 1 String str str public void readA(String str) {
		String[] strArr = str.split("\\s+");
		int[] A = new int[N + 1];
		for (int i = 1; i <= N; i++) {
			A[i] = Integer.parseInt(strArr[i - 1]);
		}
		A[0] = A[N] - K;
		int dMax = A[1] - A[0];
		int start = 1;
		for (int i = 1; i <= N; i++) {
			if (A[i] - A[i - 1] > dMax) {
				dMax = A[i] - A[i - 1];
				start = i;
			}
		}
		System.out.println(K - dMax);
	} readA {
		String[] strArr = str.split("\\s+");
		int[] A = new int[N + 1];
		for (int i = 1; i <= N; i++) {
			A[i] = Integer.parseInt(strArr[i - 1]);
		}
		A[0] = A[N] - K;
		int dMax = A[1] - A[0];
		int start = 1;
		for (int i = 1; i <= N; i++) {
			if (A[i] - A[i - 1] > dMax) {
				dMax = A[i] - A[i - 1];
				start = i;
			}
		}
		System.out.println(K - dMax);
	} String[] strArr = str.split("\\s+"); strArr str.split("\\s+") str.split str "\\s+" int[] A = new int[N + 1]; A new int[N + 1] N + 1 N 1 for (int i = 1; i <= N; i++) {
			A[i] = Integer.parseInt(strArr[i - 1]);
		} int i = 1; int i = 1; i 1 i <= N i N i++ i++ i {
			A[i] = Integer.parseInt(strArr[i - 1]);
		} {
			A[i] = Integer.parseInt(strArr[i - 1]);
		} A[i] = Integer.parseInt(strArr[i - 1]) A[i] A i Integer.parseInt(strArr[i - 1]) Integer.parseInt Integer strArr[i - 1] strArr i - 1 i 1 A[0] = A[N] - K A[0] A 0 A[N] - K A[N] A N K int dMax = A[1] - A[0]; dMax A[1] - A[0] A[1] A 1 A[0] A 0 int start = 1; start 1 for (int i = 1; i <= N; i++) {
			if (A[i] - A[i - 1] > dMax) {
				dMax = A[i] - A[i - 1];
				start = i;
			}
		} int i = 1; int i = 1; i 1 i <= N i N i++ i++ i {
			if (A[i] - A[i - 1] > dMax) {
				dMax = A[i] - A[i - 1];
				start = i;
			}
		} {
			if (A[i] - A[i - 1] > dMax) {
				dMax = A[i] - A[i - 1];
				start = i;
			}
		} if (A[i] - A[i - 1] > dMax) {
				dMax = A[i] - A[i - 1];
				start = i;
			} A[i] - A[i - 1] > dMax A[i] - A[i - 1] A[i] A i A[i - 1] A i - 1 i 1 dMax {
				dMax = A[i] - A[i - 1];
				start = i;
			} dMax = A[i] - A[i - 1] dMax A[i] - A[i - 1] A[i] A i A[i - 1] A i - 1 i 1 start = i start i System.out.println(K - dMax) System.out.println System.out System System.out K - dMax K dMax String str str public boolean hasNext() {
		return cnt < N;
	} hasNext {
		return cnt < N;
	} return cnt < N; cnt < N cnt N public void readBody(String str) {
		// System.out.println(str);
		cnt++;
	} readBody {
		// System.out.println(str);
		cnt++;
	} cnt++ cnt String str str public void solve() {

	} solve {

	}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Main {
	public static void main(String args[]) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		Solver solver = new Solver();
		solver.init();
		solver.readHead(in.readLine());
		solver.readA(in.readLine());
		// for (; solver.hasNext();) {
		// solver.readBody(in.readLine());
		// }
		// solver.solve();
	}
}

class Solver {
	int N;
	int cnt;

	public void init() {
		N = 0;
		cnt = 0;
	}

	public void readHead(String str) {
		String[] strArr = str.split("\\s+");
		K = Integer.parseInt(strArr[0]);
		N = Integer.parseInt(strArr[1]);
		// System.out.println(N);
	}

	int K;

	public void readA(String str) {
		String[] strArr = str.split("\\s+");
		int[] A = new int[N + 1];
		for (int i = 1; i <= N; i++) {
			A[i] = Integer.parseInt(strArr[i - 1]);
		}
		A[0] = A[N] - K;
		int dMax = A[1] - A[0];
		int start = 1;
		for (int i = 1; i <= N; i++) {
			if (A[i] - A[i - 1] > dMax) {
				dMax = A[i] - A[i - 1];
				start = i;
			}
		}
		System.out.println(K - dMax);
	}

	public boolean hasNext() {
		return cnt < N;
	}

	public void readBody(String str) {
		// System.out.println(str);
		cnt++;
	}

	public void solve() {

	}
}
