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

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		int K = Integer.parseInt(st.nextToken());
		int N = Integer.parseInt(st.nextToken());
		
		int[] A = new int[N];
		int ans = K;
		
		st = new StringTokenizer(br.readLine());
		for(int i=0; i<N; i++)
			A[i] = Integer.parseInt(st.nextToken());
		
		ans = A[N-1] - A[0];
		
		for(int i=1; i<N; i++)
			ans = Math.min(ans, K - (A[i] - A[i-1]));
		
		System.out.println(ans);
	}

} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.StringTokenizer; StringTokenizer java.util public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		int K = Integer.parseInt(st.nextToken());
		int N = Integer.parseInt(st.nextToken());
		
		int[] A = new int[N];
		int ans = K;
		
		st = new StringTokenizer(br.readLine());
		for(int i=0; i<N; i++)
			A[i] = Integer.parseInt(st.nextToken());
		
		ans = A[N-1] - A[0];
		
		for(int i=1; i<N; i++)
			ans = Math.min(ans, K - (A[i] - A[i-1]));
		
		System.out.println(ans);
	}

} Main public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		int K = Integer.parseInt(st.nextToken());
		int N = Integer.parseInt(st.nextToken());
		
		int[] A = new int[N];
		int ans = K;
		
		st = new StringTokenizer(br.readLine());
		for(int i=0; i<N; i++)
			A[i] = Integer.parseInt(st.nextToken());
		
		ans = A[N-1] - A[0];
		
		for(int i=1; i<N; i++)
			ans = Math.min(ans, K - (A[i] - A[i-1]));
		
		System.out.println(ans);
	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		int K = Integer.parseInt(st.nextToken());
		int N = Integer.parseInt(st.nextToken());
		
		int[] A = new int[N];
		int ans = K;
		
		st = new StringTokenizer(br.readLine());
		for(int i=0; i<N; i++)
			A[i] = Integer.parseInt(st.nextToken());
		
		ans = A[N-1] - A[0];
		
		for(int i=1; i<N; i++)
			ans = Math.min(ans, K - (A[i] - A[i-1]));
		
		System.out.println(ans);
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) StringTokenizer st = new StringTokenizer(br.readLine()); st new StringTokenizer(br.readLine()) int K = Integer.parseInt(st.nextToken()); K Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int N = Integer.parseInt(st.nextToken()); N Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int[] A = new int[N]; A new int[N] N int ans = K; ans K st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) for(int i=0; i<N; i++)
			A[i] = Integer.parseInt(st.nextToken()); int i=0; int i=0; i 0 i<N i N i++ i++ i A[i] = Integer.parseInt(st.nextToken()); A[i] = Integer.parseInt(st.nextToken()) A[i] A i Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st ans = A[N-1] - A[0] ans A[N-1] - A[0] A[N-1] A N-1 N 1 A[0] A 0 for(int i=1; i<N; i++)
			ans = Math.min(ans, K - (A[i] - A[i-1])); int i=1; int i=1; i 1 i<N i N i++ i++ i ans = Math.min(ans, K - (A[i] - A[i-1])); ans = Math.min(ans, K - (A[i] - A[i-1])) ans Math.min(ans, K - (A[i] - A[i-1])) Math.min Math ans K - (A[i] - A[i-1]) K (A[i] - A[i-1]) A[i] A i A[i-1] A i-1 i 1 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) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		int K = Integer.parseInt(st.nextToken());
		int N = Integer.parseInt(st.nextToken());
		
		int[] A = new int[N];
		int ans = K;
		
		st = new StringTokenizer(br.readLine());
		for(int i=0; i<N; i++)
			A[i] = Integer.parseInt(st.nextToken());
		
		ans = A[N-1] - A[0];
		
		for(int i=1; i<N; i++)
			ans = Math.min(ans, K - (A[i] - A[i-1]));
		
		System.out.println(ans);
	}

} public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		int K = Integer.parseInt(st.nextToken());
		int N = Integer.parseInt(st.nextToken());
		
		int[] A = new int[N];
		int ans = K;
		
		st = new StringTokenizer(br.readLine());
		for(int i=0; i<N; i++)
			A[i] = Integer.parseInt(st.nextToken());
		
		ans = A[N-1] - A[0];
		
		for(int i=1; i<N; i++)
			ans = Math.min(ans, K - (A[i] - A[i-1]));
		
		System.out.println(ans);
	}

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

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		int K = Integer.parseInt(st.nextToken());
		int N = Integer.parseInt(st.nextToken());
		
		int[] A = new int[N];
		int ans = K;
		
		st = new StringTokenizer(br.readLine());
		for(int i=0; i<N; i++)
			A[i] = Integer.parseInt(st.nextToken());
		
		ans = A[N-1] - A[0];
		
		for(int i=1; i<N; i++)
			ans = Math.min(ans, K - (A[i] - A[i-1]));
		
		System.out.println(ans);
	}

}