import java.util.Scanner;

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

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

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

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

    
} Main
import java.util.Scanner;

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

    
}