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

public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int k = scan.nextInt();
		int n = scan.nextInt();

		List<Integer> a = new ArrayList<>();
		for(int i = 0; i < n; i++) {
			a.add(scan.nextInt());
		}

		List<Integer> b = new ArrayList<>();
		b.add(k - a.get(n -1) + a.get(0));
		for(int i = 1; i < a.size(); i++) {
			b.add(a.get(i) - a.get(i -1));
		}

		System.out.println(k - Collections.max(b));
	}
} import java.util.ArrayList; ArrayList java.util import java.util.Collections; Collections java.util import java.util.List; List java.util import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int k = scan.nextInt();
		int n = scan.nextInt();

		List<Integer> a = new ArrayList<>();
		for(int i = 0; i < n; i++) {
			a.add(scan.nextInt());
		}

		List<Integer> b = new ArrayList<>();
		b.add(k - a.get(n -1) + a.get(0));
		for(int i = 1; i < a.size(); i++) {
			b.add(a.get(i) - a.get(i -1));
		}

		System.out.println(k - Collections.max(b));
	}
} Main public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int k = scan.nextInt();
		int n = scan.nextInt();

		List<Integer> a = new ArrayList<>();
		for(int i = 0; i < n; i++) {
			a.add(scan.nextInt());
		}

		List<Integer> b = new ArrayList<>();
		b.add(k - a.get(n -1) + a.get(0));
		for(int i = 1; i < a.size(); i++) {
			b.add(a.get(i) - a.get(i -1));
		}

		System.out.println(k - Collections.max(b));
	} main {

		Scanner scan = new Scanner(System.in);

		int k = scan.nextInt();
		int n = scan.nextInt();

		List<Integer> a = new ArrayList<>();
		for(int i = 0; i < n; i++) {
			a.add(scan.nextInt());
		}

		List<Integer> b = new ArrayList<>();
		b.add(k - a.get(n -1) + a.get(0));
		for(int i = 1; i < a.size(); i++) {
			b.add(a.get(i) - a.get(i -1));
		}

		System.out.println(k - Collections.max(b));
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan int n = scan.nextInt(); n scan.nextInt() scan.nextInt scan List<Integer> a = new ArrayList<>(); a new ArrayList<>() for(int i = 0; i < n; i++) {
			a.add(scan.nextInt());
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			a.add(scan.nextInt());
		} {
			a.add(scan.nextInt());
		} a.add(scan.nextInt()) a.add a scan.nextInt() scan.nextInt scan List<Integer> b = new ArrayList<>(); b new ArrayList<>() b.add(k - a.get(n -1) + a.get(0)) b.add b k - a.get(n -1) + a.get(0) k - a.get(n -1) k a.get(n -1) a.get a n -1 n 1 a.get(0) a.get a 0 for(int i = 1; i < a.size(); i++) {
			b.add(a.get(i) - a.get(i -1));
		} int i = 1; int i = 1; i 1 i < a.size() i a.size() a.size a i++ i++ i {
			b.add(a.get(i) - a.get(i -1));
		} {
			b.add(a.get(i) - a.get(i -1));
		} b.add(a.get(i) - a.get(i -1)) b.add b a.get(i) - a.get(i -1) a.get(i) a.get a i a.get(i -1) a.get a i -1 i 1 System.out.println(k - Collections.max(b)) System.out.println System.out System System.out k - Collections.max(b) k Collections.max(b) Collections.max Collections b String[] args args public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int k = scan.nextInt();
		int n = scan.nextInt();

		List<Integer> a = new ArrayList<>();
		for(int i = 0; i < n; i++) {
			a.add(scan.nextInt());
		}

		List<Integer> b = new ArrayList<>();
		b.add(k - a.get(n -1) + a.get(0));
		for(int i = 1; i < a.size(); i++) {
			b.add(a.get(i) - a.get(i -1));
		}

		System.out.println(k - Collections.max(b));
	}
} public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int k = scan.nextInt();
		int n = scan.nextInt();

		List<Integer> a = new ArrayList<>();
		for(int i = 0; i < n; i++) {
			a.add(scan.nextInt());
		}

		List<Integer> b = new ArrayList<>();
		b.add(k - a.get(n -1) + a.get(0));
		for(int i = 1; i < a.size(); i++) {
			b.add(a.get(i) - a.get(i -1));
		}

		System.out.println(k - Collections.max(b));
	}
} Main
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int k = scan.nextInt();
		int n = scan.nextInt();

		List<Integer> a = new ArrayList<>();
		for(int i = 0; i < n; i++) {
			a.add(scan.nextInt());
		}

		List<Integer> b = new ArrayList<>();
		b.add(k - a.get(n -1) + a.get(0));
		for(int i = 1; i < a.size(); i++) {
			b.add(a.get(i) - a.get(i -1));
		}

		System.out.println(k - Collections.max(b));
	}
}