import java.util.*;

public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		long K = scan.nextLong();
		int N = scan.nextInt();
		long [] house = new long[N + 1];
		long l = 0;

		for(int i=0; i < N; i++){
			house[i] = scan.nextLong();
		}
		house[N] = K + house[0];

		for(int i=0; i < N; i++){
			if (l < (house[i+1] - house[i]) )l = (house[i+1] - house[i]);
		}

		System.out.println(K - l);
	}
} import java.util.*; util.* java public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		long K = scan.nextLong();
		int N = scan.nextInt();
		long [] house = new long[N + 1];
		long l = 0;

		for(int i=0; i < N; i++){
			house[i] = scan.nextLong();
		}
		house[N] = K + house[0];

		for(int i=0; i < N; i++){
			if (l < (house[i+1] - house[i]) )l = (house[i+1] - house[i]);
		}

		System.out.println(K - l);
	}
} Main public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		long K = scan.nextLong();
		int N = scan.nextInt();
		long [] house = new long[N + 1];
		long l = 0;

		for(int i=0; i < N; i++){
			house[i] = scan.nextLong();
		}
		house[N] = K + house[0];

		for(int i=0; i < N; i++){
			if (l < (house[i+1] - house[i]) )l = (house[i+1] - house[i]);
		}

		System.out.println(K - l);
	} main {
		Scanner scan = new Scanner(System.in);
		long K = scan.nextLong();
		int N = scan.nextInt();
		long [] house = new long[N + 1];
		long l = 0;

		for(int i=0; i < N; i++){
			house[i] = scan.nextLong();
		}
		house[N] = K + house[0];

		for(int i=0; i < N; i++){
			if (l < (house[i+1] - house[i]) )l = (house[i+1] - house[i]);
		}

		System.out.println(K - l);
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) long K = scan.nextLong(); K scan.nextLong() scan.nextLong scan int N = scan.nextInt(); N scan.nextInt() scan.nextInt scan long [] house = new long[N + 1]; house new long[N + 1] N + 1 N 1 long l = 0; l 0 for(int i=0; i < N; i++){
			house[i] = scan.nextLong();
		} int i=0; int i=0; i 0 i < N i N i++ i++ i {
			house[i] = scan.nextLong();
		} {
			house[i] = scan.nextLong();
		} house[i] = scan.nextLong() house[i] house i scan.nextLong() scan.nextLong scan house[N] = K + house[0] house[N] house N K + house[0] K house[0] house 0 for(int i=0; i < N; i++){
			if (l < (house[i+1] - house[i]) )l = (house[i+1] - house[i]);
		} int i=0; int i=0; i 0 i < N i N i++ i++ i {
			if (l < (house[i+1] - house[i]) )l = (house[i+1] - house[i]);
		} {
			if (l < (house[i+1] - house[i]) )l = (house[i+1] - house[i]);
		} if (l < (house[i+1] - house[i]) )l = (house[i+1] - house[i]); l < (house[i+1] - house[i]) l (house[i+1] - house[i]) house[i+1] house i+1 i 1 house[i] house i l = (house[i+1] - house[i]) l (house[i+1] - house[i]) house[i+1] house i+1 i 1 house[i] house i System.out.println(K - l) System.out.println System.out System System.out K - l K l String[] args args public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		long K = scan.nextLong();
		int N = scan.nextInt();
		long [] house = new long[N + 1];
		long l = 0;

		for(int i=0; i < N; i++){
			house[i] = scan.nextLong();
		}
		house[N] = K + house[0];

		for(int i=0; i < N; i++){
			if (l < (house[i+1] - house[i]) )l = (house[i+1] - house[i]);
		}

		System.out.println(K - l);
	}
} public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		long K = scan.nextLong();
		int N = scan.nextInt();
		long [] house = new long[N + 1];
		long l = 0;

		for(int i=0; i < N; i++){
			house[i] = scan.nextLong();
		}
		house[N] = K + house[0];

		for(int i=0; i < N; i++){
			if (l < (house[i+1] - house[i]) )l = (house[i+1] - house[i]);
		}

		System.out.println(K - l);
	}
} Main
import java.util.*;

public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		long K = scan.nextLong();
		int N = scan.nextInt();
		long [] house = new long[N + 1];
		long l = 0;

		for(int i=0; i < N; i++){
			house[i] = scan.nextLong();
		}
		house[N] = K + house[0];

		for(int i=0; i < N; i++){
			if (l < (house[i+1] - house[i]) )l = (house[i+1] - house[i]);
		}

		System.out.println(K - l);
	}
}