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

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String str[] = br.readLine().split(" ");

		int k = Integer.parseInt(str[0]);
		int n = Integer.parseInt(str[1]);

		int a[] = new int[n];

		str = br.readLine().split(" ");

		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(str[i]);
		}

		int min = k - Math.abs(k - Math.abs(a[0] - a[n - 1]));

		for (int i = 0; i < n - 1; i++) {
			min = Math.min(min, k - Math.abs(a[i] - a[i + 1]));
		}

		System.out.println(min);
	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String str[] = br.readLine().split(" ");

		int k = Integer.parseInt(str[0]);
		int n = Integer.parseInt(str[1]);

		int a[] = new int[n];

		str = br.readLine().split(" ");

		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(str[i]);
		}

		int min = k - Math.abs(k - Math.abs(a[0] - a[n - 1]));

		for (int i = 0; i < n - 1; i++) {
			min = Math.min(min, k - Math.abs(a[i] - a[i + 1]));
		}

		System.out.println(min);
	}
} Main public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String str[] = br.readLine().split(" ");

		int k = Integer.parseInt(str[0]);
		int n = Integer.parseInt(str[1]);

		int a[] = new int[n];

		str = br.readLine().split(" ");

		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(str[i]);
		}

		int min = k - Math.abs(k - Math.abs(a[0] - a[n - 1]));

		for (int i = 0; i < n - 1; i++) {
			min = Math.min(min, k - Math.abs(a[i] - a[i + 1]));
		}

		System.out.println(min);
	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String str[] = br.readLine().split(" ");

		int k = Integer.parseInt(str[0]);
		int n = Integer.parseInt(str[1]);

		int a[] = new int[n];

		str = br.readLine().split(" ");

		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(str[i]);
		}

		int min = k - Math.abs(k - Math.abs(a[0] - a[n - 1]));

		for (int i = 0; i < n - 1; i++) {
			min = Math.min(min, k - Math.abs(a[i] - a[i + 1]));
		}

		System.out.println(min);
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String str[] = br.readLine().split(" "); str br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int k = Integer.parseInt(str[0]); k Integer.parseInt(str[0]) Integer.parseInt Integer str[0] str 0 int n = Integer.parseInt(str[1]); n Integer.parseInt(str[1]) Integer.parseInt Integer str[1] str 1 int a[] = new int[n]; a new int[n] n str = br.readLine().split(" ") str br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(str[i]);
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			a[i] = Integer.parseInt(str[i]);
		} {
			a[i] = Integer.parseInt(str[i]);
		} a[i] = Integer.parseInt(str[i]) a[i] a i Integer.parseInt(str[i]) Integer.parseInt Integer str[i] str i int min = k - Math.abs(k - Math.abs(a[0] - a[n - 1])); min k - Math.abs(k - Math.abs(a[0] - a[n - 1])) k Math.abs(k - Math.abs(a[0] - a[n - 1])) Math.abs Math k - Math.abs(a[0] - a[n - 1]) k Math.abs(a[0] - a[n - 1]) Math.abs Math a[0] - a[n - 1] a[0] a 0 a[n - 1] a n - 1 n 1 for (int i = 0; i < n - 1; i++) {
			min = Math.min(min, k - Math.abs(a[i] - a[i + 1]));
		} int i = 0; int i = 0; i 0 i < n - 1 i n - 1 n 1 i++ i++ i {
			min = Math.min(min, k - Math.abs(a[i] - a[i + 1]));
		} {
			min = Math.min(min, k - Math.abs(a[i] - a[i + 1]));
		} min = Math.min(min, k - Math.abs(a[i] - a[i + 1])) min Math.min(min, k - Math.abs(a[i] - a[i + 1])) Math.min Math min k - Math.abs(a[i] - a[i + 1]) k Math.abs(a[i] - a[i + 1]) Math.abs Math a[i] - a[i + 1] a[i] a i a[i + 1] a i + 1 i 1 System.out.println(min) System.out.println System.out System System.out min String[] args args public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String str[] = br.readLine().split(" ");

		int k = Integer.parseInt(str[0]);
		int n = Integer.parseInt(str[1]);

		int a[] = new int[n];

		str = br.readLine().split(" ");

		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(str[i]);
		}

		int min = k - Math.abs(k - Math.abs(a[0] - a[n - 1]));

		for (int i = 0; i < n - 1; i++) {
			min = Math.min(min, k - Math.abs(a[i] - a[i + 1]));
		}

		System.out.println(min);
	}
} public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String str[] = br.readLine().split(" ");

		int k = Integer.parseInt(str[0]);
		int n = Integer.parseInt(str[1]);

		int a[] = new int[n];

		str = br.readLine().split(" ");

		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(str[i]);
		}

		int min = k - Math.abs(k - Math.abs(a[0] - a[n - 1]));

		for (int i = 0; i < n - 1; i++) {
			min = Math.min(min, k - Math.abs(a[i] - a[i + 1]));
		}

		System.out.println(min);
	}
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String str[] = br.readLine().split(" ");

		int k = Integer.parseInt(str[0]);
		int n = Integer.parseInt(str[1]);

		int a[] = new int[n];

		str = br.readLine().split(" ");

		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(str[i]);
		}

		int min = k - Math.abs(k - Math.abs(a[0] - a[n - 1]));

		for (int i = 0; i < n - 1; i++) {
			min = Math.min(min, k - Math.abs(a[i] - a[i + 1]));
		}

		System.out.println(min);
	}
}