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[] A = new int[N];
        for(int i = 0; i < N; i++){
            A[i] = sc.nextInt();
        }
        int[] distance = new int[N];
        for(int i = 0; i < N-1; i++){
            distance[i] = A[i+1] - A[i];
        }
        distance[N-1] = K-A[N-1] + A[0];
        int all = 0;
        int min = Integer.MAX_VALUE;
        int count = 0;
        for(int i = 0; i < N; i++){
            all = all + distance[i];
        }
        for(int i = 0; i < N; i++){
            count = all - distance[i];
            if(min > count) min = count;
        }
        System.out.println(min);
    }
} 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[] A = new int[N];
        for(int i = 0; i < N; i++){
            A[i] = sc.nextInt();
        }
        int[] distance = new int[N];
        for(int i = 0; i < N-1; i++){
            distance[i] = A[i+1] - A[i];
        }
        distance[N-1] = K-A[N-1] + A[0];
        int all = 0;
        int min = Integer.MAX_VALUE;
        int count = 0;
        for(int i = 0; i < N; i++){
            all = all + distance[i];
        }
        for(int i = 0; i < N; i++){
            count = all - distance[i];
            if(min > count) min = count;
        }
        System.out.println(min);
    }
} 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];
        for(int i = 0; i < N; i++){
            A[i] = sc.nextInt();
        }
        int[] distance = new int[N];
        for(int i = 0; i < N-1; i++){
            distance[i] = A[i+1] - A[i];
        }
        distance[N-1] = K-A[N-1] + A[0];
        int all = 0;
        int min = Integer.MAX_VALUE;
        int count = 0;
        for(int i = 0; i < N; i++){
            all = all + distance[i];
        }
        for(int i = 0; i < N; i++){
            count = all - distance[i];
            if(min > count) min = count;
        }
        System.out.println(min);
    } main {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] A = new int[N];
        for(int i = 0; i < N; i++){
            A[i] = sc.nextInt();
        }
        int[] distance = new int[N];
        for(int i = 0; i < N-1; i++){
            distance[i] = A[i+1] - A[i];
        }
        distance[N-1] = K-A[N-1] + A[0];
        int all = 0;
        int min = Integer.MAX_VALUE;
        int count = 0;
        for(int i = 0; i < N; i++){
            all = all + distance[i];
        }
        for(int i = 0; i < N; i++){
            count = all - distance[i];
            if(min > count) min = count;
        }
        System.out.println(min);
    } 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 for(int i = 0; i < N; i++){
            A[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            A[i] = sc.nextInt();
        } {
            A[i] = sc.nextInt();
        } A[i] = sc.nextInt() A[i] A i sc.nextInt() sc.nextInt sc int[] distance = new int[N]; distance new int[N] N for(int i = 0; i < N-1; i++){
            distance[i] = A[i+1] - A[i];
        } int i = 0; int i = 0; i 0 i < N-1 i N-1 N 1 i++ i++ i {
            distance[i] = A[i+1] - A[i];
        } {
            distance[i] = A[i+1] - A[i];
        } distance[i] = A[i+1] - A[i] distance[i] distance i A[i+1] - A[i] A[i+1] A i+1 i 1 A[i] A i distance[N-1] = K-A[N-1] + A[0] distance[N-1] distance N-1 N 1 K-A[N-1] + A[0] K-A[N-1] K A[N-1] A N-1 N 1 A[0] A 0 int all = 0; all 0 int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE int count = 0; count 0 for(int i = 0; i < N; i++){
            all = all + distance[i];
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            all = all + distance[i];
        } {
            all = all + distance[i];
        } all = all + distance[i] all all + distance[i] all distance[i] distance i for(int i = 0; i < N; i++){
            count = all - distance[i];
            if(min > count) min = count;
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            count = all - distance[i];
            if(min > count) min = count;
        } {
            count = all - distance[i];
            if(min > count) min = count;
        } count = all - distance[i] count all - distance[i] all distance[i] distance i if(min > count) min = count; min > count min count min = count min count System.out.println(min) System.out.println System.out System System.out min 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[] A = new int[N];
        for(int i = 0; i < N; i++){
            A[i] = sc.nextInt();
        }
        int[] distance = new int[N];
        for(int i = 0; i < N-1; i++){
            distance[i] = A[i+1] - A[i];
        }
        distance[N-1] = K-A[N-1] + A[0];
        int all = 0;
        int min = Integer.MAX_VALUE;
        int count = 0;
        for(int i = 0; i < N; i++){
            all = all + distance[i];
        }
        for(int i = 0; i < N; i++){
            count = all - distance[i];
            if(min > count) min = count;
        }
        System.out.println(min);
    }
} public 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];
        for(int i = 0; i < N; i++){
            A[i] = sc.nextInt();
        }
        int[] distance = new int[N];
        for(int i = 0; i < N-1; i++){
            distance[i] = A[i+1] - A[i];
        }
        distance[N-1] = K-A[N-1] + A[0];
        int all = 0;
        int min = Integer.MAX_VALUE;
        int count = 0;
        for(int i = 0; i < N; i++){
            all = all + distance[i];
        }
        for(int i = 0; i < N; i++){
            count = all - distance[i];
            if(min > count) min = count;
        }
        System.out.println(min);
    }
} 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[] A = new int[N];
        for(int i = 0; i < N; i++){
            A[i] = sc.nextInt();
        }
        int[] distance = new int[N];
        for(int i = 0; i < N-1; i++){
            distance[i] = A[i+1] - A[i];
        }
        distance[N-1] = K-A[N-1] + A[0];
        int all = 0;
        int min = Integer.MAX_VALUE;
        int count = 0;
        for(int i = 0; i < N; i++){
            all = all + distance[i];
        }
        for(int i = 0; i < N; i++){
            count = all - distance[i];
            if(min > count) min = count;
        }
        System.out.println(min);
    }
}