import java.util.*;

class Main{
	static public void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int k=sc.nextInt();
		int n=sc.nextInt();
		int ans=0;
		int[] an=new int[n+1];
		
		for(int i=0;i<n;i++){
			an[i]=sc.nextInt();
		}
		an[n]=an[0]+k;
		for(int i=0;i<n;i++){
			ans=Math.max(ans,an[i+1]-an[i]);
		}
		int aa=k-ans;
		System.out.println(aa);
	}
} import java.util.*; util.* java class Main{
	static public void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int k=sc.nextInt();
		int n=sc.nextInt();
		int ans=0;
		int[] an=new int[n+1];
		
		for(int i=0;i<n;i++){
			an[i]=sc.nextInt();
		}
		an[n]=an[0]+k;
		for(int i=0;i<n;i++){
			ans=Math.max(ans,an[i+1]-an[i]);
		}
		int aa=k-ans;
		System.out.println(aa);
	}
} Main static public void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int k=sc.nextInt();
		int n=sc.nextInt();
		int ans=0;
		int[] an=new int[n+1];
		
		for(int i=0;i<n;i++){
			an[i]=sc.nextInt();
		}
		an[n]=an[0]+k;
		for(int i=0;i<n;i++){
			ans=Math.max(ans,an[i+1]-an[i]);
		}
		int aa=k-ans;
		System.out.println(aa);
	} main {
		Scanner sc=new Scanner(System.in);
		int k=sc.nextInt();
		int n=sc.nextInt();
		int ans=0;
		int[] an=new int[n+1];
		
		for(int i=0;i<n;i++){
			an[i]=sc.nextInt();
		}
		an[n]=an[0]+k;
		for(int i=0;i<n;i++){
			ans=Math.max(ans,an[i+1]-an[i]);
		}
		int aa=k-ans;
		System.out.println(aa);
	} Scanner sc=new Scanner(System.in); sc new Scanner(System.in) int k=sc.nextInt(); k sc.nextInt() sc.nextInt sc int n=sc.nextInt(); n sc.nextInt() sc.nextInt sc int ans=0; ans 0 int[] an=new int[n+1]; an new int[n+1] n+1 n 1 for(int i=0;i<n;i++){
			an[i]=sc.nextInt();
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			an[i]=sc.nextInt();
		} {
			an[i]=sc.nextInt();
		} an[i]=sc.nextInt() an[i] an i sc.nextInt() sc.nextInt sc an[n]=an[0]+k an[n] an n an[0]+k an[0] an 0 k for(int i=0;i<n;i++){
			ans=Math.max(ans,an[i+1]-an[i]);
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			ans=Math.max(ans,an[i+1]-an[i]);
		} {
			ans=Math.max(ans,an[i+1]-an[i]);
		} ans=Math.max(ans,an[i+1]-an[i]) ans Math.max(ans,an[i+1]-an[i]) Math.max Math ans an[i+1]-an[i] an[i+1] an i+1 i 1 an[i] an i int aa=k-ans; aa k-ans k ans System.out.println(aa) System.out.println System.out System System.out aa String[] args args
import java.util.*;

class Main{
	static public void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int k=sc.nextInt();
		int n=sc.nextInt();
		int ans=0;
		int[] an=new int[n+1];
		
		for(int i=0;i<n;i++){
			an[i]=sc.nextInt();
		}
		an[n]=an[0]+k;
		for(int i=0;i<n;i++){
			ans=Math.max(ans,an[i+1]-an[i]);
		}
		int aa=k-ans;
		System.out.println(aa);
	}
}