import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        
        int array[] = new int[N];
        int gap = 0;
        
        for(int i=0;i<N;i++){
            array[i] = sc.nextInt();
        }
        
        int tmp;
        
        for(int i=0;i<N-1;i++){
            tmp = array[i+1] - array[i];
            gap = Math.max(gap, tmp);
        }
        
        tmp = (array[0] + K) - array[N-1];
        gap = Math.max(gap, tmp);
        
        System.out.println(K-gap);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        
        int array[] = new int[N];
        int gap = 0;
        
        for(int i=0;i<N;i++){
            array[i] = sc.nextInt();
        }
        
        int tmp;
        
        for(int i=0;i<N-1;i++){
            tmp = array[i+1] - array[i];
            gap = Math.max(gap, tmp);
        }
        
        tmp = (array[0] + K) - array[N-1];
        gap = Math.max(gap, tmp);
        
        System.out.println(K-gap);
    }
} Main public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        
        int array[] = new int[N];
        int gap = 0;
        
        for(int i=0;i<N;i++){
            array[i] = sc.nextInt();
        }
        
        int tmp;
        
        for(int i=0;i<N-1;i++){
            tmp = array[i+1] - array[i];
            gap = Math.max(gap, tmp);
        }
        
        tmp = (array[0] + K) - array[N-1];
        gap = Math.max(gap, tmp);
        
        System.out.println(K-gap);
    } main {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        
        int array[] = new int[N];
        int gap = 0;
        
        for(int i=0;i<N;i++){
            array[i] = sc.nextInt();
        }
        
        int tmp;
        
        for(int i=0;i<N-1;i++){
            tmp = array[i+1] - array[i];
            gap = Math.max(gap, tmp);
        }
        
        tmp = (array[0] + K) - array[N-1];
        gap = Math.max(gap, tmp);
        
        System.out.println(K-gap);
    } 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 array[] = new int[N]; array new int[N] N int gap = 0; gap 0 for(int i=0;i<N;i++){
            array[i] = sc.nextInt();
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            array[i] = sc.nextInt();
        } {
            array[i] = sc.nextInt();
        } array[i] = sc.nextInt() array[i] array i sc.nextInt() sc.nextInt sc int tmp; tmp for(int i=0;i<N-1;i++){
            tmp = array[i+1] - array[i];
            gap = Math.max(gap, tmp);
        } int i=0; int i=0; i 0 i<N-1 i N-1 N 1 i++ i++ i {
            tmp = array[i+1] - array[i];
            gap = Math.max(gap, tmp);
        } {
            tmp = array[i+1] - array[i];
            gap = Math.max(gap, tmp);
        } tmp = array[i+1] - array[i] tmp array[i+1] - array[i] array[i+1] array i+1 i 1 array[i] array i gap = Math.max(gap, tmp) gap Math.max(gap, tmp) Math.max Math gap tmp tmp = (array[0] + K) - array[N-1] tmp (array[0] + K) - array[N-1] (array[0] + K) array[0] array 0 K array[N-1] array N-1 N 1 gap = Math.max(gap, tmp) gap Math.max(gap, tmp) Math.max Math gap tmp System.out.println(K-gap) System.out.println System.out System System.out K-gap K gap String[] args args public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        
        int array[] = new int[N];
        int gap = 0;
        
        for(int i=0;i<N;i++){
            array[i] = sc.nextInt();
        }
        
        int tmp;
        
        for(int i=0;i<N-1;i++){
            tmp = array[i+1] - array[i];
            gap = Math.max(gap, tmp);
        }
        
        tmp = (array[0] + K) - array[N-1];
        gap = Math.max(gap, tmp);
        
        System.out.println(K-gap);
    }
} public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        
        int array[] = new int[N];
        int gap = 0;
        
        for(int i=0;i<N;i++){
            array[i] = sc.nextInt();
        }
        
        int tmp;
        
        for(int i=0;i<N-1;i++){
            tmp = array[i+1] - array[i];
            gap = Math.max(gap, tmp);
        }
        
        tmp = (array[0] + K) - array[N-1];
        gap = Math.max(gap, tmp);
        
        System.out.println(K-gap);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        
        int array[] = new int[N];
        int gap = 0;
        
        for(int i=0;i<N;i++){
            array[i] = sc.nextInt();
        }
        
        int tmp;
        
        for(int i=0;i<N-1;i++){
            tmp = array[i+1] - array[i];
            gap = Math.max(gap, tmp);
        }
        
        tmp = (array[0] + K) - array[N-1];
        gap = Math.max(gap, tmp);
        
        System.out.println(K-gap);
    }
}
