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[] A = new int[N];
        int largest = 0;
        A[0] = sc.nextInt();
        largest = A[0];
        for (int i = 1; i < A.length; i++) {
            A[i] = sc.nextInt();
        }
        largest = A[0] + K - A[N-1];
        for (int i = 1; i < A.length; i++) {
            largest = Math.max(largest,A[i] - A[i-1]);
        }
        
        System.out.println(K - largest);
        
    }
} 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[] A = new int[N];
        int largest = 0;
        A[0] = sc.nextInt();
        largest = A[0];
        for (int i = 1; i < A.length; i++) {
            A[i] = sc.nextInt();
        }
        largest = A[0] + K - A[N-1];
        for (int i = 1; i < A.length; i++) {
            largest = Math.max(largest,A[i] - A[i-1]);
        }
        
        System.out.println(K - largest);
        
    }
} Main public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] A = new int[N];
        int largest = 0;
        A[0] = sc.nextInt();
        largest = A[0];
        for (int i = 1; i < A.length; i++) {
            A[i] = sc.nextInt();
        }
        largest = A[0] + K - A[N-1];
        for (int i = 1; i < A.length; i++) {
            largest = Math.max(largest,A[i] - A[i-1]);
        }
        
        System.out.println(K - largest);
        
    } main {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] A = new int[N];
        int largest = 0;
        A[0] = sc.nextInt();
        largest = A[0];
        for (int i = 1; i < A.length; i++) {
            A[i] = sc.nextInt();
        }
        largest = A[0] + K - A[N-1];
        for (int i = 1; i < A.length; i++) {
            largest = Math.max(largest,A[i] - A[i-1]);
        }
        
        System.out.println(K - largest);
        
    } 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[] A = new int[N]; A new int[N] N int largest = 0; largest 0 A[0] = sc.nextInt() A[0] A 0 sc.nextInt() sc.nextInt sc largest = A[0] largest A[0] A 0 for (int i = 1; i < A.length; i++) {
            A[i] = sc.nextInt();
        } int i = 1; int i = 1; i 1 i < A.length i A.length A A.length i++ i++ i {
            A[i] = sc.nextInt();
        } {
            A[i] = sc.nextInt();
        } A[i] = sc.nextInt() A[i] A i sc.nextInt() sc.nextInt sc largest = A[0] + K - A[N-1] largest A[0] + K - A[N-1] A[0] + K A[0] A 0 K A[N-1] A N-1 N 1 for (int i = 1; i < A.length; i++) {
            largest = Math.max(largest,A[i] - A[i-1]);
        } int i = 1; int i = 1; i 1 i < A.length i A.length A A.length i++ i++ i {
            largest = Math.max(largest,A[i] - A[i-1]);
        } {
            largest = Math.max(largest,A[i] - A[i-1]);
        } largest = Math.max(largest,A[i] - A[i-1]) largest Math.max(largest,A[i] - A[i-1]) Math.max Math largest A[i] - A[i-1] A[i] A i A[i-1] A i-1 i 1 System.out.println(K - largest) System.out.println System.out System System.out K - largest K largest 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[] A = new int[N];
        int largest = 0;
        A[0] = sc.nextInt();
        largest = A[0];
        for (int i = 1; i < A.length; i++) {
            A[i] = sc.nextInt();
        }
        largest = A[0] + K - A[N-1];
        for (int i = 1; i < A.length; i++) {
            largest = Math.max(largest,A[i] - A[i-1]);
        }
        
        System.out.println(K - largest);
        
    }
}