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