import java.util.*;
class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] param = new int[n];
        int[] param1 = new int[n];
        
        for(int i=0 ; i<n ; i++){
            param[i] = sc.nextInt();
        }
        
        for(int i=0 ; i<n-1 ; i++){
            param1[i] = param[i+1]-param[i]; 
        }
        
        param1[n-1] = (k-param[n-1]) + (param[0]);
        
        
       Arrays.sort(param1);
       
       int dis = k-param1[n-1];
       
       System.out.println(dis);
      }
    } import java.util.*; util.* java class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] param = new int[n];
        int[] param1 = new int[n];
        
        for(int i=0 ; i<n ; i++){
            param[i] = sc.nextInt();
        }
        
        for(int i=0 ; i<n-1 ; i++){
            param1[i] = param[i+1]-param[i]; 
        }
        
        param1[n-1] = (k-param[n-1]) + (param[0]);
        
        
       Arrays.sort(param1);
       
       int dis = k-param1[n-1];
       
       System.out.println(dis);
      }
    } Main public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] param = new int[n];
        int[] param1 = new int[n];
        
        for(int i=0 ; i<n ; i++){
            param[i] = sc.nextInt();
        }
        
        for(int i=0 ; i<n-1 ; i++){
            param1[i] = param[i+1]-param[i]; 
        }
        
        param1[n-1] = (k-param[n-1]) + (param[0]);
        
        
       Arrays.sort(param1);
       
       int dis = k-param1[n-1];
       
       System.out.println(dis);
      } main {
        Scanner sc = new Scanner(System.in);
        
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] param = new int[n];
        int[] param1 = new int[n];
        
        for(int i=0 ; i<n ; i++){
            param[i] = sc.nextInt();
        }
        
        for(int i=0 ; i<n-1 ; i++){
            param1[i] = param[i+1]-param[i]; 
        }
        
        param1[n-1] = (k-param[n-1]) + (param[0]);
        
        
       Arrays.sort(param1);
       
       int dis = k-param1[n-1];
       
       System.out.println(dis);
      } 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[] param = new int[n]; param new int[n] n int[] param1 = new int[n]; param1 new int[n] n for(int i=0 ; i<n ; i++){
            param[i] = sc.nextInt();
        } int i=0 ; int i=0 ; i 0 i<n i n i++ i++ i {
            param[i] = sc.nextInt();
        } {
            param[i] = sc.nextInt();
        } param[i] = sc.nextInt() param[i] param i sc.nextInt() sc.nextInt sc for(int i=0 ; i<n-1 ; i++){
            param1[i] = param[i+1]-param[i]; 
        } int i=0 ; int i=0 ; i 0 i<n-1 i n-1 n 1 i++ i++ i {
            param1[i] = param[i+1]-param[i]; 
        } {
            param1[i] = param[i+1]-param[i]; 
        } param1[i] = param[i+1]-param[i] param1[i] param1 i param[i+1]-param[i] param[i+1] param i+1 i 1 param[i] param i param1[n-1] = (k-param[n-1]) + (param[0]) param1[n-1] param1 n-1 n 1 (k-param[n-1]) + (param[0]) (k-param[n-1]) k param[n-1] param n-1 n 1 (param[0]) param 0 Arrays.sort(param1) Arrays.sort Arrays param1 int dis = k-param1[n-1]; dis k-param1[n-1] k param1[n-1] param1 n-1 n 1 System.out.println(dis) System.out.println System.out System System.out dis String[] args args
import java.util.*;
class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] param = new int[n];
        int[] param1 = new int[n];
        
        for(int i=0 ; i<n ; i++){
            param[i] = sc.nextInt();
        }
        
        for(int i=0 ; i<n-1 ; i++){
            param1[i] = param[i+1]-param[i]; 
        }
        
        param1[n-1] = (k-param[n-1]) + (param[0]);
        
        
       Arrays.sort(param1);
       
       int dis = k-param1[n-1];
       
       System.out.println(dis);
      }
    }