import java.util.*;
public class Main { 
	final static int maxn=(int)2e5+10;
	static int vis[]=new int [maxn];
	static int a[]=new int [maxn];
	static int b[]=new int [maxn];
	static int f[][]=new int [maxn][3];
 public static void main(String args[])
 {
	 Scanner input=new Scanner(System.in);
     int k,n;
     k=input.nextInt();n=input.nextInt();
     for(int i=1;i<=n;i++)
    	 a[i]=input.nextInt();
     Arrays.sort(a,1,n+1);
     long dis=a[n]-a[1];int alldis=a[n]-a[1];
     int left=a[1];int right=k-a[n];
     for(int i=1;i<=n-1;i++)
     {
    	 int subdis=a[i+1]-a[i];
    	 if(subdis>(left+right))
    		 dis=Math.min(dis, alldis-subdis+(left+right));
     }
     System.out.println(dis);
}
} import java.util.*; util.* java public class Main { 
	final static int maxn=(int)2e5+10;
	static int vis[]=new int [maxn];
	static int a[]=new int [maxn];
	static int b[]=new int [maxn];
	static int f[][]=new int [maxn][3];
 public static void main(String args[])
 {
	 Scanner input=new Scanner(System.in);
     int k,n;
     k=input.nextInt();n=input.nextInt();
     for(int i=1;i<=n;i++)
    	 a[i]=input.nextInt();
     Arrays.sort(a,1,n+1);
     long dis=a[n]-a[1];int alldis=a[n]-a[1];
     int left=a[1];int right=k-a[n];
     for(int i=1;i<=n-1;i++)
     {
    	 int subdis=a[i+1]-a[i];
    	 if(subdis>(left+right))
    		 dis=Math.min(dis, alldis-subdis+(left+right));
     }
     System.out.println(dis);
}
} Main final static int maxn=(int)2e5+10; maxn (int)2e5+10 (int)2e5 10 static int vis[]=new int [maxn]; vis new int [maxn] maxn static int a[]=new int [maxn]; a new int [maxn] maxn static int b[]=new int [maxn]; b new int [maxn] maxn static int f[][]=new int [maxn][3]; f new int [maxn][3] maxn 3 public static void main(String args[])
 {
	 Scanner input=new Scanner(System.in);
     int k,n;
     k=input.nextInt();n=input.nextInt();
     for(int i=1;i<=n;i++)
    	 a[i]=input.nextInt();
     Arrays.sort(a,1,n+1);
     long dis=a[n]-a[1];int alldis=a[n]-a[1];
     int left=a[1];int right=k-a[n];
     for(int i=1;i<=n-1;i++)
     {
    	 int subdis=a[i+1]-a[i];
    	 if(subdis>(left+right))
    		 dis=Math.min(dis, alldis-subdis+(left+right));
     }
     System.out.println(dis);
} main {
	 Scanner input=new Scanner(System.in);
     int k,n;
     k=input.nextInt();n=input.nextInt();
     for(int i=1;i<=n;i++)
    	 a[i]=input.nextInt();
     Arrays.sort(a,1,n+1);
     long dis=a[n]-a[1];int alldis=a[n]-a[1];
     int left=a[1];int right=k-a[n];
     for(int i=1;i<=n-1;i++)
     {
    	 int subdis=a[i+1]-a[i];
    	 if(subdis>(left+right))
    		 dis=Math.min(dis, alldis-subdis+(left+right));
     }
     System.out.println(dis);
} Scanner input=new Scanner(System.in); input new Scanner(System.in) int k k n; n k=input.nextInt() k input.nextInt() input.nextInt input n=input.nextInt() n input.nextInt() input.nextInt input for(int i=1;i<=n;i++)
    	 a[i]=input.nextInt(); int i=1; int i=1; i 1 i<=n i n i++ i++ i a[i]=input.nextInt(); a[i]=input.nextInt() a[i] a i input.nextInt() input.nextInt input Arrays.sort(a,1,n+1) Arrays.sort Arrays a 1 n+1 n 1 long dis=a[n]-a[1]; dis a[n]-a[1] a[n] a n a[1] a 1 int alldis=a[n]-a[1]; alldis a[n]-a[1] a[n] a n a[1] a 1 int left=a[1]; left a[1] a 1 int right=k-a[n]; right k-a[n] k a[n] a n for(int i=1;i<=n-1;i++)
     {
    	 int subdis=a[i+1]-a[i];
    	 if(subdis>(left+right))
    		 dis=Math.min(dis, alldis-subdis+(left+right));
     } int i=1; int i=1; i 1 i<=n-1 i n-1 n 1 i++ i++ i {
    	 int subdis=a[i+1]-a[i];
    	 if(subdis>(left+right))
    		 dis=Math.min(dis, alldis-subdis+(left+right));
     } {
    	 int subdis=a[i+1]-a[i];
    	 if(subdis>(left+right))
    		 dis=Math.min(dis, alldis-subdis+(left+right));
     } int subdis=a[i+1]-a[i]; subdis a[i+1]-a[i] a[i+1] a i+1 i 1 a[i] a i if(subdis>(left+right))
    		 dis=Math.min(dis, alldis-subdis+(left+right)); subdis>(left+right) subdis (left+right) left right dis=Math.min(dis, alldis-subdis+(left+right)) dis Math.min(dis, alldis-subdis+(left+right)) Math.min Math dis alldis-subdis+(left+right) alldis-subdis alldis subdis (left+right) left right System.out.println(dis) System.out.println System.out System System.out dis String args[] args public class Main { 
	final static int maxn=(int)2e5+10;
	static int vis[]=new int [maxn];
	static int a[]=new int [maxn];
	static int b[]=new int [maxn];
	static int f[][]=new int [maxn][3];
 public static void main(String args[])
 {
	 Scanner input=new Scanner(System.in);
     int k,n;
     k=input.nextInt();n=input.nextInt();
     for(int i=1;i<=n;i++)
    	 a[i]=input.nextInt();
     Arrays.sort(a,1,n+1);
     long dis=a[n]-a[1];int alldis=a[n]-a[1];
     int left=a[1];int right=k-a[n];
     for(int i=1;i<=n-1;i++)
     {
    	 int subdis=a[i+1]-a[i];
    	 if(subdis>(left+right))
    		 dis=Math.min(dis, alldis-subdis+(left+right));
     }
     System.out.println(dis);
}
} public class Main { 
	final static int maxn=(int)2e5+10;
	static int vis[]=new int [maxn];
	static int a[]=new int [maxn];
	static int b[]=new int [maxn];
	static int f[][]=new int [maxn][3];
 public static void main(String args[])
 {
	 Scanner input=new Scanner(System.in);
     int k,n;
     k=input.nextInt();n=input.nextInt();
     for(int i=1;i<=n;i++)
    	 a[i]=input.nextInt();
     Arrays.sort(a,1,n+1);
     long dis=a[n]-a[1];int alldis=a[n]-a[1];
     int left=a[1];int right=k-a[n];
     for(int i=1;i<=n-1;i++)
     {
    	 int subdis=a[i+1]-a[i];
    	 if(subdis>(left+right))
    		 dis=Math.min(dis, alldis-subdis+(left+right));
     }
     System.out.println(dis);
}
} Main
import java.util.*;
public class Main { 
	final static int maxn=(int)2e5+10;
	static int vis[]=new int [maxn];
	static int a[]=new int [maxn];
	static int b[]=new int [maxn];
	static int f[][]=new int [maxn][3];
 public static void main(String args[])
 {
	 Scanner input=new Scanner(System.in);
     int k,n;
     k=input.nextInt();n=input.nextInt();
     for(int i=1;i<=n;i++)
    	 a[i]=input.nextInt();
     Arrays.sort(a,1,n+1);
     long dis=a[n]-a[1];int alldis=a[n]-a[1];
     int left=a[1];int right=k-a[n];
     for(int i=1;i<=n-1;i++)
     {
    	 int subdis=a[i+1]-a[i];
    	 if(subdis>(left+right))
    		 dis=Math.min(dis, alldis-subdis+(left+right));
     }
     System.out.println(dis);
}
}