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

public class Main {
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();

		int[] a = new int[N];
		int maxDist = 0;
		for(int i = 0; i < N; i++) {
		 a[i] = sc.nextInt();
		 if(i > 0 && (a[i] - a[i-1]) > maxDist)
			 maxDist = a[i] - a[i-1];
		}
		sc.close();

		int tmp = (K-a[N-1]) + a[0];

		if( tmp> maxDist) {
			maxDist = tmp;
		}

		System.out.println(K-maxDist);

	}

} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();

		int[] a = new int[N];
		int maxDist = 0;
		for(int i = 0; i < N; i++) {
		 a[i] = sc.nextInt();
		 if(i > 0 && (a[i] - a[i-1]) > maxDist)
			 maxDist = a[i] - a[i-1];
		}
		sc.close();

		int tmp = (K-a[N-1]) + a[0];

		if( tmp> maxDist) {
			maxDist = tmp;
		}

		System.out.println(K-maxDist);

	}

} Main public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();

		int[] a = new int[N];
		int maxDist = 0;
		for(int i = 0; i < N; i++) {
		 a[i] = sc.nextInt();
		 if(i > 0 && (a[i] - a[i-1]) > maxDist)
			 maxDist = a[i] - a[i-1];
		}
		sc.close();

		int tmp = (K-a[N-1]) + a[0];

		if( tmp> maxDist) {
			maxDist = tmp;
		}

		System.out.println(K-maxDist);

	} main {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();

		int[] a = new int[N];
		int maxDist = 0;
		for(int i = 0; i < N; i++) {
		 a[i] = sc.nextInt();
		 if(i > 0 && (a[i] - a[i-1]) > maxDist)
			 maxDist = a[i] - a[i-1];
		}
		sc.close();

		int tmp = (K-a[N-1]) + a[0];

		if( tmp> maxDist) {
			maxDist = tmp;
		}

		System.out.println(K-maxDist);

	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int[] a = new int[N]; a new int[N] N int maxDist = 0; maxDist 0 for(int i = 0; i < N; i++) {
		 a[i] = sc.nextInt();
		 if(i > 0 && (a[i] - a[i-1]) > maxDist)
			 maxDist = a[i] - a[i-1];
		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
		 a[i] = sc.nextInt();
		 if(i > 0 && (a[i] - a[i-1]) > maxDist)
			 maxDist = a[i] - a[i-1];
		} {
		 a[i] = sc.nextInt();
		 if(i > 0 && (a[i] - a[i-1]) > maxDist)
			 maxDist = a[i] - a[i-1];
		} a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc if(i > 0 && (a[i] - a[i-1]) > maxDist)
			 maxDist = a[i] - a[i-1]; i > 0 && (a[i] - a[i-1]) > maxDist i > 0 i 0 (a[i] - a[i-1]) > maxDist (a[i] - a[i-1]) a[i] a i a[i-1] a i-1 i 1 maxDist maxDist = a[i] - a[i-1] maxDist a[i] - a[i-1] a[i] a i a[i-1] a i-1 i 1 sc.close() sc.close sc int tmp = (K-a[N-1]) + a[0]; tmp (K-a[N-1]) + a[0] (K-a[N-1]) K a[N-1] a N-1 N 1 a[0] a 0 if( tmp> maxDist) {
			maxDist = tmp;
		} tmp> maxDist tmp maxDist {
			maxDist = tmp;
		} maxDist = tmp maxDist tmp System.out.println(K-maxDist) System.out.println System.out System System.out K-maxDist K maxDist String[] args args public class Main {
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();

		int[] a = new int[N];
		int maxDist = 0;
		for(int i = 0; i < N; i++) {
		 a[i] = sc.nextInt();
		 if(i > 0 && (a[i] - a[i-1]) > maxDist)
			 maxDist = a[i] - a[i-1];
		}
		sc.close();

		int tmp = (K-a[N-1]) + a[0];

		if( tmp> maxDist) {
			maxDist = tmp;
		}

		System.out.println(K-maxDist);

	}

} public class Main {
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();

		int[] a = new int[N];
		int maxDist = 0;
		for(int i = 0; i < N; i++) {
		 a[i] = sc.nextInt();
		 if(i > 0 && (a[i] - a[i-1]) > maxDist)
			 maxDist = a[i] - a[i-1];
		}
		sc.close();

		int tmp = (K-a[N-1]) + a[0];

		if( tmp> maxDist) {
			maxDist = tmp;
		}

		System.out.println(K-maxDist);

	}

} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int N = sc.nextInt();

		int[] a = new int[N];
		int maxDist = 0;
		for(int i = 0; i < N; i++) {
		 a[i] = sc.nextInt();
		 if(i > 0 && (a[i] - a[i-1]) > maxDist)
			 maxDist = a[i] - a[i-1];
		}
		sc.close();

		int tmp = (K-a[N-1]) + a[0];

		if( tmp> maxDist) {
			maxDist = tmp;
		}

		System.out.println(K-maxDist);

	}

}