
import java.util.*;
import java.lang.*;

public class Main {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int k = s.nextInt();
		int n = s.nextInt();
		int[] a = new int[n];

		for(int i=0;i<n;i++) {
			a[i]=s.nextInt();
		}
		int[] diff = new int[n];

		diff[0]=a[0]+k-a[n-1];
		for(int i=1;i<n;i++) {
			diff[i] =  a[i]-a[i-1];
		}
		int max = 0;
		for(int i=0;i<n;i++) {
			if(diff[i]>max) 
				max = diff[i];
		}
		System.out.println(k-max);
	}

 }

	 import java.util.*; util.* java import java.lang.*; lang.* java public class Main {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int k = s.nextInt();
		int n = s.nextInt();
		int[] a = new int[n];

		for(int i=0;i<n;i++) {
			a[i]=s.nextInt();
		}
		int[] diff = new int[n];

		diff[0]=a[0]+k-a[n-1];
		for(int i=1;i<n;i++) {
			diff[i] =  a[i]-a[i-1];
		}
		int max = 0;
		for(int i=0;i<n;i++) {
			if(diff[i]>max) 
				max = diff[i];
		}
		System.out.println(k-max);
	}

 } Main public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int k = s.nextInt();
		int n = s.nextInt();
		int[] a = new int[n];

		for(int i=0;i<n;i++) {
			a[i]=s.nextInt();
		}
		int[] diff = new int[n];

		diff[0]=a[0]+k-a[n-1];
		for(int i=1;i<n;i++) {
			diff[i] =  a[i]-a[i-1];
		}
		int max = 0;
		for(int i=0;i<n;i++) {
			if(diff[i]>max) 
				max = diff[i];
		}
		System.out.println(k-max);
	} main {
		Scanner s = new Scanner(System.in);
		int k = s.nextInt();
		int n = s.nextInt();
		int[] a = new int[n];

		for(int i=0;i<n;i++) {
			a[i]=s.nextInt();
		}
		int[] diff = new int[n];

		diff[0]=a[0]+k-a[n-1];
		for(int i=1;i<n;i++) {
			diff[i] =  a[i]-a[i-1];
		}
		int max = 0;
		for(int i=0;i<n;i++) {
			if(diff[i]>max) 
				max = diff[i];
		}
		System.out.println(k-max);
	} Scanner s = new Scanner(System.in); s new Scanner(System.in) int k = s.nextInt(); k s.nextInt() s.nextInt s int n = s.nextInt(); n s.nextInt() s.nextInt s int[] a = new int[n]; a new int[n] n for(int i=0;i<n;i++) {
			a[i]=s.nextInt();
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			a[i]=s.nextInt();
		} {
			a[i]=s.nextInt();
		} a[i]=s.nextInt() a[i] a i s.nextInt() s.nextInt s int[] diff = new int[n]; diff new int[n] n diff[0]=a[0]+k-a[n-1] diff[0] diff 0 a[0]+k-a[n-1] a[0]+k a[0] a 0 k a[n-1] a n-1 n 1 for(int i=1;i<n;i++) {
			diff[i] =  a[i]-a[i-1];
		} int i=1; int i=1; i 1 i<n i n i++ i++ i {
			diff[i] =  a[i]-a[i-1];
		} {
			diff[i] =  a[i]-a[i-1];
		} diff[i] =  a[i]-a[i-1] diff[i] diff i a[i]-a[i-1] a[i] a i a[i-1] a i-1 i 1 int max = 0; max 0 for(int i=0;i<n;i++) {
			if(diff[i]>max) 
				max = diff[i];
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			if(diff[i]>max) 
				max = diff[i];
		} {
			if(diff[i]>max) 
				max = diff[i];
		} if(diff[i]>max) 
				max = diff[i]; diff[i]>max diff[i] diff i max max = diff[i] max diff[i] diff i System.out.println(k-max) System.out.println System.out System System.out k-max k max String[] args args public class Main {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int k = s.nextInt();
		int n = s.nextInt();
		int[] a = new int[n];

		for(int i=0;i<n;i++) {
			a[i]=s.nextInt();
		}
		int[] diff = new int[n];

		diff[0]=a[0]+k-a[n-1];
		for(int i=1;i<n;i++) {
			diff[i] =  a[i]-a[i-1];
		}
		int max = 0;
		for(int i=0;i<n;i++) {
			if(diff[i]>max) 
				max = diff[i];
		}
		System.out.println(k-max);
	}

 } public class Main {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int k = s.nextInt();
		int n = s.nextInt();
		int[] a = new int[n];

		for(int i=0;i<n;i++) {
			a[i]=s.nextInt();
		}
		int[] diff = new int[n];

		diff[0]=a[0]+k-a[n-1];
		for(int i=1;i<n;i++) {
			diff[i] =  a[i]-a[i-1];
		}
		int max = 0;
		for(int i=0;i<n;i++) {
			if(diff[i]>max) 
				max = diff[i];
		}
		System.out.println(k-max);
	}

 } Main

import java.util.*;
import java.lang.*;

public class Main {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int k = s.nextInt();
		int n = s.nextInt();
		int[] a = new int[n];

		for(int i=0;i<n;i++) {
			a[i]=s.nextInt();
		}
		int[] diff = new int[n];

		diff[0]=a[0]+k-a[n-1];
		for(int i=1;i<n;i++) {
			diff[i] =  a[i]-a[i-1];
		}
		int max = 0;
		for(int i=0;i<n;i++) {
			if(diff[i]>max) 
				max = diff[i];
		}
		System.out.println(k-max);
	}

 }

	