import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int N = Integer.parseInt(sc.next());
		ArrayList<Integer> list = new ArrayList<>();
		for(int i = 0; i < N; i++) {
			list.add(Integer.parseInt(sc.next()));
		}
		sc.close();

		Collections.sort(list);
		int distance = Integer.MIN_VALUE;
		for(int i = 1; i < list.size(); i++) {
			int start = list.get(i - 1);
			int end = list.get(i);
			int tmpDistance = end - start;
			distance = Math.max(distance, tmpDistance);
		}
		//始点と終点の差チェック
		distance = Math.max(distance, list.get(0) + (K - list.get(N - 1)));

		int ans = K - distance;
		System.out.println(ans);

	}

} import java.util.ArrayList; ArrayList java.util import java.util.Collections; Collections java.util import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int N = Integer.parseInt(sc.next());
		ArrayList<Integer> list = new ArrayList<>();
		for(int i = 0; i < N; i++) {
			list.add(Integer.parseInt(sc.next()));
		}
		sc.close();

		Collections.sort(list);
		int distance = Integer.MIN_VALUE;
		for(int i = 1; i < list.size(); i++) {
			int start = list.get(i - 1);
			int end = list.get(i);
			int tmpDistance = end - start;
			distance = Math.max(distance, tmpDistance);
		}
		//始点と終点の差チェック
		distance = Math.max(distance, list.get(0) + (K - list.get(N - 1)));

		int ans = K - distance;
		System.out.println(ans);

	}

} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int N = Integer.parseInt(sc.next());
		ArrayList<Integer> list = new ArrayList<>();
		for(int i = 0; i < N; i++) {
			list.add(Integer.parseInt(sc.next()));
		}
		sc.close();

		Collections.sort(list);
		int distance = Integer.MIN_VALUE;
		for(int i = 1; i < list.size(); i++) {
			int start = list.get(i - 1);
			int end = list.get(i);
			int tmpDistance = end - start;
			distance = Math.max(distance, tmpDistance);
		}
		//始点と終点の差チェック
		distance = Math.max(distance, list.get(0) + (K - list.get(N - 1)));

		int ans = K - distance;
		System.out.println(ans);

	} main {
		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int N = Integer.parseInt(sc.next());
		ArrayList<Integer> list = new ArrayList<>();
		for(int i = 0; i < N; i++) {
			list.add(Integer.parseInt(sc.next()));
		}
		sc.close();

		Collections.sort(list);
		int distance = Integer.MIN_VALUE;
		for(int i = 1; i < list.size(); i++) {
			int start = list.get(i - 1);
			int end = list.get(i);
			int tmpDistance = end - start;
			distance = Math.max(distance, tmpDistance);
		}
		//始点と終点の差チェック
		distance = Math.max(distance, list.get(0) + (K - list.get(N - 1)));

		int ans = K - distance;
		System.out.println(ans);

	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = Integer.parseInt(sc.next()); K Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int N = Integer.parseInt(sc.next()); N Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc ArrayList<Integer> list = new ArrayList<>(); list new ArrayList<>() for(int i = 0; i < N; i++) {
			list.add(Integer.parseInt(sc.next()));
		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
			list.add(Integer.parseInt(sc.next()));
		} {
			list.add(Integer.parseInt(sc.next()));
		} list.add(Integer.parseInt(sc.next())) list.add list Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc sc.close() sc.close sc Collections.sort(list) Collections.sort Collections list int distance = Integer.MIN_VALUE; distance Integer.MIN_VALUE Integer Integer.MIN_VALUE for(int i = 1; i < list.size(); i++) {
			int start = list.get(i - 1);
			int end = list.get(i);
			int tmpDistance = end - start;
			distance = Math.max(distance, tmpDistance);
		} int i = 1; int i = 1; i 1 i < list.size() i list.size() list.size list i++ i++ i {
			int start = list.get(i - 1);
			int end = list.get(i);
			int tmpDistance = end - start;
			distance = Math.max(distance, tmpDistance);
		} {
			int start = list.get(i - 1);
			int end = list.get(i);
			int tmpDistance = end - start;
			distance = Math.max(distance, tmpDistance);
		} int start = list.get(i - 1); start list.get(i - 1) list.get list i - 1 i 1 int end = list.get(i); end list.get(i) list.get list i int tmpDistance = end - start; tmpDistance end - start end start distance = Math.max(distance, tmpDistance) distance Math.max(distance, tmpDistance) Math.max Math distance tmpDistance distance = Math.max(distance, list.get(0) + (K - list.get(N - 1))) distance Math.max(distance, list.get(0) + (K - list.get(N - 1))) Math.max Math distance list.get(0) + (K - list.get(N - 1)) list.get(0) list.get list 0 (K - list.get(N - 1)) K list.get(N - 1) list.get list N - 1 N 1 int ans = K - distance; ans K - distance K distance System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int N = Integer.parseInt(sc.next());
		ArrayList<Integer> list = new ArrayList<>();
		for(int i = 0; i < N; i++) {
			list.add(Integer.parseInt(sc.next()));
		}
		sc.close();

		Collections.sort(list);
		int distance = Integer.MIN_VALUE;
		for(int i = 1; i < list.size(); i++) {
			int start = list.get(i - 1);
			int end = list.get(i);
			int tmpDistance = end - start;
			distance = Math.max(distance, tmpDistance);
		}
		//始点と終点の差チェック
		distance = Math.max(distance, list.get(0) + (K - list.get(N - 1)));

		int ans = K - distance;
		System.out.println(ans);

	}

} public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int N = Integer.parseInt(sc.next());
		ArrayList<Integer> list = new ArrayList<>();
		for(int i = 0; i < N; i++) {
			list.add(Integer.parseInt(sc.next()));
		}
		sc.close();

		Collections.sort(list);
		int distance = Integer.MIN_VALUE;
		for(int i = 1; i < list.size(); i++) {
			int start = list.get(i - 1);
			int end = list.get(i);
			int tmpDistance = end - start;
			distance = Math.max(distance, tmpDistance);
		}
		//始点と終点の差チェック
		distance = Math.max(distance, list.get(0) + (K - list.get(N - 1)));

		int ans = K - distance;
		System.out.println(ans);

	}

} Main
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int N = Integer.parseInt(sc.next());
		ArrayList<Integer> list = new ArrayList<>();
		for(int i = 0; i < N; i++) {
			list.add(Integer.parseInt(sc.next()));
		}
		sc.close();

		Collections.sort(list);
		int distance = Integer.MIN_VALUE;
		for(int i = 1; i < list.size(); i++) {
			int start = list.get(i - 1);
			int end = list.get(i);
			int tmpDistance = end - start;
			distance = Math.max(distance, tmpDistance);
		}
		//始点と終点の差チェック
		distance = Math.max(distance, list.get(0) + (K - list.get(N - 1)));

		int ans = K - distance;
		System.out.println(ans);

	}

}