/*package whatever //do not write package name here */

import java.io.*;
import java.util.*;
class Main {
	public static void main (String[] args)   {
	   Scanner s=new Scanner(System.in); 
       int k=s.nextInt();
       int n=s.nextInt();
       int arr[]=new int[n];
       for(int i=0;i<n;i++)
       {
           arr[i]=s.nextInt();
       }
       int maxdiff=0;
       for(int i=1;i<n;i++)
       {
           if(maxdiff<arr[i]-arr[i-1])
           maxdiff=arr[i]-arr[i-1];
       }
       if(maxdiff<k-arr[n-1]+arr[0])
       {
           System.out.println(k-(k-arr[n-1]+arr[0]));
       }
       else
       {
           System.out.println(k-maxdiff);
       }
	   
	    
 
	}
	
} import java.io.*; io.* java import java.util.*; util.* java class Main {
	public static void main (String[] args)   {
	   Scanner s=new Scanner(System.in); 
       int k=s.nextInt();
       int n=s.nextInt();
       int arr[]=new int[n];
       for(int i=0;i<n;i++)
       {
           arr[i]=s.nextInt();
       }
       int maxdiff=0;
       for(int i=1;i<n;i++)
       {
           if(maxdiff<arr[i]-arr[i-1])
           maxdiff=arr[i]-arr[i-1];
       }
       if(maxdiff<k-arr[n-1]+arr[0])
       {
           System.out.println(k-(k-arr[n-1]+arr[0]));
       }
       else
       {
           System.out.println(k-maxdiff);
       }
	   
	    
 
	}
	
} Main public static void main (String[] args)   {
	   Scanner s=new Scanner(System.in); 
       int k=s.nextInt();
       int n=s.nextInt();
       int arr[]=new int[n];
       for(int i=0;i<n;i++)
       {
           arr[i]=s.nextInt();
       }
       int maxdiff=0;
       for(int i=1;i<n;i++)
       {
           if(maxdiff<arr[i]-arr[i-1])
           maxdiff=arr[i]-arr[i-1];
       }
       if(maxdiff<k-arr[n-1]+arr[0])
       {
           System.out.println(k-(k-arr[n-1]+arr[0]));
       }
       else
       {
           System.out.println(k-maxdiff);
       }
	   
	    
 
	} main {
	   Scanner s=new Scanner(System.in); 
       int k=s.nextInt();
       int n=s.nextInt();
       int arr[]=new int[n];
       for(int i=0;i<n;i++)
       {
           arr[i]=s.nextInt();
       }
       int maxdiff=0;
       for(int i=1;i<n;i++)
       {
           if(maxdiff<arr[i]-arr[i-1])
           maxdiff=arr[i]-arr[i-1];
       }
       if(maxdiff<k-arr[n-1]+arr[0])
       {
           System.out.println(k-(k-arr[n-1]+arr[0]));
       }
       else
       {
           System.out.println(k-maxdiff);
       }
	   
	    
 
	} 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 arr[]=new int[n]; arr new int[n] n for(int i=0;i<n;i++)
       {
           arr[i]=s.nextInt();
       } int i=0; int i=0; i 0 i<n i n i++ i++ i {
           arr[i]=s.nextInt();
       } {
           arr[i]=s.nextInt();
       } arr[i]=s.nextInt() arr[i] arr i s.nextInt() s.nextInt s int maxdiff=0; maxdiff 0 for(int i=1;i<n;i++)
       {
           if(maxdiff<arr[i]-arr[i-1])
           maxdiff=arr[i]-arr[i-1];
       } int i=1; int i=1; i 1 i<n i n i++ i++ i {
           if(maxdiff<arr[i]-arr[i-1])
           maxdiff=arr[i]-arr[i-1];
       } {
           if(maxdiff<arr[i]-arr[i-1])
           maxdiff=arr[i]-arr[i-1];
       } if(maxdiff<arr[i]-arr[i-1])
           maxdiff=arr[i]-arr[i-1]; maxdiff<arr[i]-arr[i-1] maxdiff arr[i]-arr[i-1] arr[i] arr i arr[i-1] arr i-1 i 1 maxdiff=arr[i]-arr[i-1] maxdiff arr[i]-arr[i-1] arr[i] arr i arr[i-1] arr i-1 i 1 if(maxdiff<k-arr[n-1]+arr[0])
       {
           System.out.println(k-(k-arr[n-1]+arr[0]));
       }
       else
       {
           System.out.println(k-maxdiff);
       } maxdiff<k-arr[n-1]+arr[0] maxdiff k-arr[n-1]+arr[0] k-arr[n-1] k arr[n-1] arr n-1 n 1 arr[0] arr 0 {
           System.out.println(k-(k-arr[n-1]+arr[0]));
       } System.out.println(k-(k-arr[n-1]+arr[0])) System.out.println System.out System System.out k-(k-arr[n-1]+arr[0]) k (k-arr[n-1]+arr[0]) k-arr[n-1] k arr[n-1] arr n-1 n 1 arr[0] arr 0 {
           System.out.println(k-maxdiff);
       } System.out.println(k-maxdiff) System.out.println System.out System System.out k-maxdiff k maxdiff String[] args args
/*package whatever //do not write package name here */

import java.io.*;
import java.util.*;
class Main {
	public static void main (String[] args)   {
	   Scanner s=new Scanner(System.in); 
       int k=s.nextInt();
       int n=s.nextInt();
       int arr[]=new int[n];
       for(int i=0;i<n;i++)
       {
           arr[i]=s.nextInt();
       }
       int maxdiff=0;
       for(int i=1;i<n;i++)
       {
           if(maxdiff<arr[i]-arr[i-1])
           maxdiff=arr[i]-arr[i-1];
       }
       if(maxdiff<k-arr[n-1]+arr[0])
       {
           System.out.println(k-(k-arr[n-1]+arr[0]));
       }
       else
       {
           System.out.println(k-maxdiff);
       }
	   
	    
 
	}
	
}