//Referred Professors code
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[] aa = new int[n];
		st = new StringTokenizer(br.readLine());
		for (int i=0; i<n; i++)
			aa[i] = Integer.parseInt(st.nextToken());
		int gap = aa[0] + k - aa[n-1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i]-aa[i-1]);
		System.out.println(k-gap);
	}
} 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[] aa = new int[n];
		st = new StringTokenizer(br.readLine());
		for (int i=0; i<n; i++)
			aa[i] = Integer.parseInt(st.nextToken());
		int gap = aa[0] + k - aa[n-1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i]-aa[i-1]);
		System.out.println(k-gap);
	}
} 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[] aa = new int[n];
		st = new StringTokenizer(br.readLine());
		for (int i=0; i<n; i++)
			aa[i] = Integer.parseInt(st.nextToken());
		int gap = aa[0] + k - aa[n-1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i]-aa[i-1]);
		System.out.println(k-gap);
	} 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[] aa = new int[n];
		st = new StringTokenizer(br.readLine());
		for (int i=0; i<n; i++)
			aa[i] = Integer.parseInt(st.nextToken());
		int gap = aa[0] + k - aa[n-1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i]-aa[i-1]);
		System.out.println(k-gap);
	} 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[] aa = new int[n]; aa new int[n] n st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) for (int i=0; i<n; i++)
			aa[i] = Integer.parseInt(st.nextToken()); int i=0; int i=0; i 0 i<n i n i++ i++ i aa[i] = Integer.parseInt(st.nextToken()); aa[i] = Integer.parseInt(st.nextToken()) aa[i] aa i Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int gap = aa[0] + k - aa[n-1]; gap aa[0] + k - aa[n-1] aa[0] + k aa[0] aa 0 k aa[n-1] aa n-1 n 1 for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i]-aa[i-1]); int i = 1; int i = 1; i 1 i < n i n i++ i++ i gap = Math.max(gap, aa[i]-aa[i-1]); gap = Math.max(gap, aa[i]-aa[i-1]) gap Math.max(gap, aa[i]-aa[i-1]) Math.max Math gap aa[i]-aa[i-1] aa[i] aa i aa[i-1] aa i-1 i 1 System.out.println(k-gap) System.out.println System.out System System.out k-gap k gap 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[] aa = new int[n];
		st = new StringTokenizer(br.readLine());
		for (int i=0; i<n; i++)
			aa[i] = Integer.parseInt(st.nextToken());
		int gap = aa[0] + k - aa[n-1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i]-aa[i-1]);
		System.out.println(k-gap);
	}
} 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[] aa = new int[n];
		st = new StringTokenizer(br.readLine());
		for (int i=0; i<n; i++)
			aa[i] = Integer.parseInt(st.nextToken());
		int gap = aa[0] + k - aa[n-1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i]-aa[i-1]);
		System.out.println(k-gap);
	}
} Main
//Referred Professors code
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[] aa = new int[n];
		st = new StringTokenizer(br.readLine());
		for (int i=0; i<n; i++)
			aa[i] = Integer.parseInt(st.nextToken());
		int gap = aa[0] + k - aa[n-1];
		for (int i = 1; i < n; i++)
			gap = Math.max(gap, aa[i]-aa[i-1]);
		System.out.println(k-gap);
	}
}