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[] array = new int[200000];

		for(int i=0;i<n;i++) {
			array[i] = scan.nextInt();

		}

		int distance = 0;
		int max = array[0] + (k-array[n-1]);
		for(int i=0;i<n-1;i++) {

			distance = array[i+1] - array[i];
			if(distance >= max) {
				max = distance;
			}
	}
		System.out.println(k-max);

		scan.close();

	}

} 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[] array = new int[200000];

		for(int i=0;i<n;i++) {
			array[i] = scan.nextInt();

		}

		int distance = 0;
		int max = array[0] + (k-array[n-1]);
		for(int i=0;i<n-1;i++) {

			distance = array[i+1] - array[i];
			if(distance >= max) {
				max = distance;
			}
	}
		System.out.println(k-max);

		scan.close();

	}

} Main public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int k = scan.nextInt();
		int n = scan.nextInt();
		int[] array = new int[200000];

		for(int i=0;i<n;i++) {
			array[i] = scan.nextInt();

		}

		int distance = 0;
		int max = array[0] + (k-array[n-1]);
		for(int i=0;i<n-1;i++) {

			distance = array[i+1] - array[i];
			if(distance >= max) {
				max = distance;
			}
	}
		System.out.println(k-max);

		scan.close();

	} main {

		Scanner scan = new Scanner(System.in);

		int k = scan.nextInt();
		int n = scan.nextInt();
		int[] array = new int[200000];

		for(int i=0;i<n;i++) {
			array[i] = scan.nextInt();

		}

		int distance = 0;
		int max = array[0] + (k-array[n-1]);
		for(int i=0;i<n-1;i++) {

			distance = array[i+1] - array[i];
			if(distance >= max) {
				max = distance;
			}
	}
		System.out.println(k-max);

		scan.close();

	} 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[] array = new int[200000]; array new int[200000] 200000 for(int i=0;i<n;i++) {
			array[i] = scan.nextInt();

		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			array[i] = scan.nextInt();

		} {
			array[i] = scan.nextInt();

		} array[i] = scan.nextInt() array[i] array i scan.nextInt() scan.nextInt scan int distance = 0; distance 0 int max = array[0] + (k-array[n-1]); max array[0] + (k-array[n-1]) array[0] array 0 (k-array[n-1]) k array[n-1] array n-1 n 1 for(int i=0;i<n-1;i++) {

			distance = array[i+1] - array[i];
			if(distance >= max) {
				max = distance;
			}
	} int i=0; int i=0; i 0 i<n-1 i n-1 n 1 i++ i++ i {

			distance = array[i+1] - array[i];
			if(distance >= max) {
				max = distance;
			}
	} {

			distance = array[i+1] - array[i];
			if(distance >= max) {
				max = distance;
			}
	} distance = array[i+1] - array[i] distance array[i+1] - array[i] array[i+1] array i+1 i 1 array[i] array i if(distance >= max) {
				max = distance;
			} distance >= max distance max {
				max = distance;
			} max = distance max distance System.out.println(k-max) System.out.println System.out System System.out k-max k max scan.close() scan.close scan 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[] array = new int[200000];

		for(int i=0;i<n;i++) {
			array[i] = scan.nextInt();

		}

		int distance = 0;
		int max = array[0] + (k-array[n-1]);
		for(int i=0;i<n-1;i++) {

			distance = array[i+1] - array[i];
			if(distance >= max) {
				max = distance;
			}
	}
		System.out.println(k-max);

		scan.close();

	}

} public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int k = scan.nextInt();
		int n = scan.nextInt();
		int[] array = new int[200000];

		for(int i=0;i<n;i++) {
			array[i] = scan.nextInt();

		}

		int distance = 0;
		int max = array[0] + (k-array[n-1]);
		for(int i=0;i<n-1;i++) {

			distance = array[i+1] - array[i];
			if(distance >= max) {
				max = distance;
			}
	}
		System.out.println(k-max);

		scan.close();

	}

} Main
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[] array = new int[200000];

		for(int i=0;i<n;i++) {
			array[i] = scan.nextInt();

		}

		int distance = 0;
		int max = array[0] + (k-array[n-1]);
		for(int i=0;i<n-1;i++) {

			distance = array[i+1] - array[i];
			if(distance >= max) {
				max = distance;
			}
	}
		System.out.println(k-max);

		scan.close();

	}

}
