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];
		long way = 0;
		long ans = 0;

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

		long second_way = 0;
		for(int i = 0; i < N; i++){
			way = house[(i + (N-1)) % N] - house[i];
			if(way <= 0){
				way = (K - house[i]) + house[(i + (N-1)) % N];
			}
			second_way = house[i] + (K - house[(i+1) % N]);
			if(second_way >= K){
				second_way = house[i] - house[(i+1) % N];
 			}

			if(i ==0 || ans > Math.min(way, second_way)){
				ans = Math.min(way, second_way);
			}

		}
		System.out.println(ans);


	}
} 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];
		long way = 0;
		long ans = 0;

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

		long second_way = 0;
		for(int i = 0; i < N; i++){
			way = house[(i + (N-1)) % N] - house[i];
			if(way <= 0){
				way = (K - house[i]) + house[(i + (N-1)) % N];
			}
			second_way = house[i] + (K - house[(i+1) % N]);
			if(second_way >= K){
				second_way = house[i] - house[(i+1) % N];
 			}

			if(i ==0 || ans > Math.min(way, second_way)){
				ans = Math.min(way, second_way);
			}

		}
		System.out.println(ans);


	}
} 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];
		long way = 0;
		long ans = 0;

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

		long second_way = 0;
		for(int i = 0; i < N; i++){
			way = house[(i + (N-1)) % N] - house[i];
			if(way <= 0){
				way = (K - house[i]) + house[(i + (N-1)) % N];
			}
			second_way = house[i] + (K - house[(i+1) % N]);
			if(second_way >= K){
				second_way = house[i] - house[(i+1) % N];
 			}

			if(i ==0 || ans > Math.min(way, second_way)){
				ans = Math.min(way, second_way);
			}

		}
		System.out.println(ans);


	} main {
		Scanner scan = new Scanner(System.in);
		long K = scan.nextLong();
		int N = scan.nextInt();
		long [] house = new long[N];
		long way = 0;
		long ans = 0;

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

		long second_way = 0;
		for(int i = 0; i < N; i++){
			way = house[(i + (N-1)) % N] - house[i];
			if(way <= 0){
				way = (K - house[i]) + house[(i + (N-1)) % N];
			}
			second_way = house[i] + (K - house[(i+1) % N]);
			if(second_way >= K){
				second_way = house[i] - house[(i+1) % N];
 			}

			if(i ==0 || ans > Math.min(way, second_way)){
				ans = Math.min(way, second_way);
			}

		}
		System.out.println(ans);


	} 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]; house new long[N] N long way = 0; way 0 long ans = 0; ans 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 long second_way = 0; second_way 0 for(int i = 0; i < N; i++){
			way = house[(i + (N-1)) % N] - house[i];
			if(way <= 0){
				way = (K - house[i]) + house[(i + (N-1)) % N];
			}
			second_way = house[i] + (K - house[(i+1) % N]);
			if(second_way >= K){
				second_way = house[i] - house[(i+1) % N];
 			}

			if(i ==0 || ans > Math.min(way, second_way)){
				ans = Math.min(way, second_way);
			}

		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
			way = house[(i + (N-1)) % N] - house[i];
			if(way <= 0){
				way = (K - house[i]) + house[(i + (N-1)) % N];
			}
			second_way = house[i] + (K - house[(i+1) % N]);
			if(second_way >= K){
				second_way = house[i] - house[(i+1) % N];
 			}

			if(i ==0 || ans > Math.min(way, second_way)){
				ans = Math.min(way, second_way);
			}

		} {
			way = house[(i + (N-1)) % N] - house[i];
			if(way <= 0){
				way = (K - house[i]) + house[(i + (N-1)) % N];
			}
			second_way = house[i] + (K - house[(i+1) % N]);
			if(second_way >= K){
				second_way = house[i] - house[(i+1) % N];
 			}

			if(i ==0 || ans > Math.min(way, second_way)){
				ans = Math.min(way, second_way);
			}

		} way = house[(i + (N-1)) % N] - house[i] way house[(i + (N-1)) % N] - house[i] house[(i + (N-1)) % N] house (i + (N-1)) % N (i + (N-1)) i (N-1) N 1 N house[i] house i if(way <= 0){
				way = (K - house[i]) + house[(i + (N-1)) % N];
			} way <= 0 way 0 {
				way = (K - house[i]) + house[(i + (N-1)) % N];
			} way = (K - house[i]) + house[(i + (N-1)) % N] way (K - house[i]) + house[(i + (N-1)) % N] (K - house[i]) K house[i] house i house[(i + (N-1)) % N] house (i + (N-1)) % N (i + (N-1)) i (N-1) N 1 N second_way = house[i] + (K - house[(i+1) % N]) second_way house[i] + (K - house[(i+1) % N]) house[i] house i (K - house[(i+1) % N]) K house[(i+1) % N] house (i+1) % N (i+1) i 1 N if(second_way >= K){
				second_way = house[i] - house[(i+1) % N];
 			} second_way >= K second_way K {
				second_way = house[i] - house[(i+1) % N];
 			} second_way = house[i] - house[(i+1) % N] second_way house[i] - house[(i+1) % N] house[i] house i house[(i+1) % N] house (i+1) % N (i+1) i 1 N if(i ==0 || ans > Math.min(way, second_way)){
				ans = Math.min(way, second_way);
			} i ==0 || ans > Math.min(way, second_way) i ==0 i 0 ans > Math.min(way, second_way) ans Math.min(way, second_way) Math.min Math way second_way {
				ans = Math.min(way, second_way);
			} ans = Math.min(way, second_way) ans Math.min(way, second_way) Math.min Math way second_way System.out.println(ans) System.out.println System.out System System.out ans 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];
		long way = 0;
		long ans = 0;

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

		long second_way = 0;
		for(int i = 0; i < N; i++){
			way = house[(i + (N-1)) % N] - house[i];
			if(way <= 0){
				way = (K - house[i]) + house[(i + (N-1)) % N];
			}
			second_way = house[i] + (K - house[(i+1) % N]);
			if(second_way >= K){
				second_way = house[i] - house[(i+1) % N];
 			}

			if(i ==0 || ans > Math.min(way, second_way)){
				ans = Math.min(way, second_way);
			}

		}
		System.out.println(ans);


	}
} 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];
		long way = 0;
		long ans = 0;

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

		long second_way = 0;
		for(int i = 0; i < N; i++){
			way = house[(i + (N-1)) % N] - house[i];
			if(way <= 0){
				way = (K - house[i]) + house[(i + (N-1)) % N];
			}
			second_way = house[i] + (K - house[(i+1) % N]);
			if(second_way >= K){
				second_way = house[i] - house[(i+1) % N];
 			}

			if(i ==0 || ans > Math.min(way, second_way)){
				ans = Math.min(way, second_way);
			}

		}
		System.out.println(ans);


	}
} 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];
		long way = 0;
		long ans = 0;

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

		long second_way = 0;
		for(int i = 0; i < N; i++){
			way = house[(i + (N-1)) % N] - house[i];
			if(way <= 0){
				way = (K - house[i]) + house[(i + (N-1)) % N];
			}
			second_way = house[i] + (K - house[(i+1) % N]);
			if(second_way >= K){
				second_way = house[i] - house[(i+1) % N];
 			}

			if(i ==0 || ans > Math.min(way, second_way)){
				ans = Math.min(way, second_way);
			}

		}
		System.out.println(ans);


	}
}