import java.util.ArrayList;
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();
		int count = 0;

		ArrayList<Integer> list = new ArrayList<Integer>(n);

		for(int i = 0;i < n;i++) {
			list.add(scan.nextInt());
		}

		for(int i = 1;i <= n;i++) {
			if(i == n) {
				if((k - list.get(n - 1)) + list.get(0) > count) {
					count = (k - list.get(n - 1)) + list.get(0);
				}
			}else {
				if((list.get(i) - list.get(i - 1)) > count) {
					count = list.get(i) - list.get(i - 1);
				}
			}
		}

		System.out.println(k - count);
	}
} import java.util.ArrayList; ArrayList 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();
		int count = 0;

		ArrayList<Integer> list = new ArrayList<Integer>(n);

		for(int i = 0;i < n;i++) {
			list.add(scan.nextInt());
		}

		for(int i = 1;i <= n;i++) {
			if(i == n) {
				if((k - list.get(n - 1)) + list.get(0) > count) {
					count = (k - list.get(n - 1)) + list.get(0);
				}
			}else {
				if((list.get(i) - list.get(i - 1)) > count) {
					count = list.get(i) - list.get(i - 1);
				}
			}
		}

		System.out.println(k - count);
	}
} Main public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

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

		ArrayList<Integer> list = new ArrayList<Integer>(n);

		for(int i = 0;i < n;i++) {
			list.add(scan.nextInt());
		}

		for(int i = 1;i <= n;i++) {
			if(i == n) {
				if((k - list.get(n - 1)) + list.get(0) > count) {
					count = (k - list.get(n - 1)) + list.get(0);
				}
			}else {
				if((list.get(i) - list.get(i - 1)) > count) {
					count = list.get(i) - list.get(i - 1);
				}
			}
		}

		System.out.println(k - count);
	} main {
		Scanner scan = new Scanner(System.in);

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

		ArrayList<Integer> list = new ArrayList<Integer>(n);

		for(int i = 0;i < n;i++) {
			list.add(scan.nextInt());
		}

		for(int i = 1;i <= n;i++) {
			if(i == n) {
				if((k - list.get(n - 1)) + list.get(0) > count) {
					count = (k - list.get(n - 1)) + list.get(0);
				}
			}else {
				if((list.get(i) - list.get(i - 1)) > count) {
					count = list.get(i) - list.get(i - 1);
				}
			}
		}

		System.out.println(k - count);
	} 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 int count = 0; count 0 ArrayList<Integer> list = new ArrayList<Integer>(n); list new ArrayList<Integer>(n) for(int i = 0;i < n;i++) {
			list.add(scan.nextInt());
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			list.add(scan.nextInt());
		} {
			list.add(scan.nextInt());
		} list.add(scan.nextInt()) list.add list scan.nextInt() scan.nextInt scan for(int i = 1;i <= n;i++) {
			if(i == n) {
				if((k - list.get(n - 1)) + list.get(0) > count) {
					count = (k - list.get(n - 1)) + list.get(0);
				}
			}else {
				if((list.get(i) - list.get(i - 1)) > count) {
					count = list.get(i) - list.get(i - 1);
				}
			}
		} int i = 1; int i = 1; i 1 i <= n i n i++ i++ i {
			if(i == n) {
				if((k - list.get(n - 1)) + list.get(0) > count) {
					count = (k - list.get(n - 1)) + list.get(0);
				}
			}else {
				if((list.get(i) - list.get(i - 1)) > count) {
					count = list.get(i) - list.get(i - 1);
				}
			}
		} {
			if(i == n) {
				if((k - list.get(n - 1)) + list.get(0) > count) {
					count = (k - list.get(n - 1)) + list.get(0);
				}
			}else {
				if((list.get(i) - list.get(i - 1)) > count) {
					count = list.get(i) - list.get(i - 1);
				}
			}
		} if(i == n) {
				if((k - list.get(n - 1)) + list.get(0) > count) {
					count = (k - list.get(n - 1)) + list.get(0);
				}
			}else {
				if((list.get(i) - list.get(i - 1)) > count) {
					count = list.get(i) - list.get(i - 1);
				}
			} i == n i n {
				if((k - list.get(n - 1)) + list.get(0) > count) {
					count = (k - list.get(n - 1)) + list.get(0);
				}
			} if((k - list.get(n - 1)) + list.get(0) > count) {
					count = (k - list.get(n - 1)) + list.get(0);
				} (k - list.get(n - 1)) + list.get(0) > count (k - list.get(n - 1)) + list.get(0) (k - list.get(n - 1)) k list.get(n - 1) list.get list n - 1 n 1 list.get(0) list.get list 0 count {
					count = (k - list.get(n - 1)) + list.get(0);
				} count = (k - list.get(n - 1)) + list.get(0) count (k - list.get(n - 1)) + list.get(0) (k - list.get(n - 1)) k list.get(n - 1) list.get list n - 1 n 1 list.get(0) list.get list 0 {
				if((list.get(i) - list.get(i - 1)) > count) {
					count = list.get(i) - list.get(i - 1);
				}
			} if((list.get(i) - list.get(i - 1)) > count) {
					count = list.get(i) - list.get(i - 1);
				} (list.get(i) - list.get(i - 1)) > count (list.get(i) - list.get(i - 1)) list.get(i) list.get list i list.get(i - 1) list.get list i - 1 i 1 count {
					count = list.get(i) - list.get(i - 1);
				} count = list.get(i) - list.get(i - 1) count list.get(i) - list.get(i - 1) list.get(i) list.get list i list.get(i - 1) list.get list i - 1 i 1 System.out.println(k - count) System.out.println System.out System System.out k - count k count 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();
		int count = 0;

		ArrayList<Integer> list = new ArrayList<Integer>(n);

		for(int i = 0;i < n;i++) {
			list.add(scan.nextInt());
		}

		for(int i = 1;i <= n;i++) {
			if(i == n) {
				if((k - list.get(n - 1)) + list.get(0) > count) {
					count = (k - list.get(n - 1)) + list.get(0);
				}
			}else {
				if((list.get(i) - list.get(i - 1)) > count) {
					count = list.get(i) - list.get(i - 1);
				}
			}
		}

		System.out.println(k - count);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

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

		ArrayList<Integer> list = new ArrayList<Integer>(n);

		for(int i = 0;i < n;i++) {
			list.add(scan.nextInt());
		}

		for(int i = 1;i <= n;i++) {
			if(i == n) {
				if((k - list.get(n - 1)) + list.get(0) > count) {
					count = (k - list.get(n - 1)) + list.get(0);
				}
			}else {
				if((list.get(i) - list.get(i - 1)) > count) {
					count = list.get(i) - list.get(i - 1);
				}
			}
		}

		System.out.println(k - count);
	}
} Main
import java.util.ArrayList;
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();
		int count = 0;

		ArrayList<Integer> list = new ArrayList<Integer>(n);

		for(int i = 0;i < n;i++) {
			list.add(scan.nextInt());
		}

		for(int i = 1;i <= n;i++) {
			if(i == n) {
				if((k - list.get(n - 1)) + list.get(0) > count) {
					count = (k - list.get(n - 1)) + list.get(0);
				}
			}else {
				if((list.get(i) - list.get(i - 1)) > count) {
					count = list.get(i) - list.get(i - 1);
				}
			}
		}

		System.out.println(k - count);
	}
}
