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