/* package whatever; // don't place package name! */

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();
        sc.nextLine();

        int[] len = new int[N];

        int sum = 0;
        int max = 0;

        for (int i = 0; i < N; i++) {
            len[i] = sc.nextInt();
        }

        for (int i = 1; i < N; i++) {
            int sa = Math.abs(len[i]  - len[i-1]);
            sum += sa;
            
            if (sa > max) {
                max = sa;
            }
        }
        
        if (max > len[0] + K - len[N-1]) {
            sum = sum - max + len[0] + K - len[N-1];
        } 

        System.out.println(sum);
        sc.close();
    }
} 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();
        sc.nextLine();

        int[] len = new int[N];

        int sum = 0;
        int max = 0;

        for (int i = 0; i < N; i++) {
            len[i] = sc.nextInt();
        }

        for (int i = 1; i < N; i++) {
            int sa = Math.abs(len[i]  - len[i-1]);
            sum += sa;
            
            if (sa > max) {
                max = sa;
            }
        }
        
        if (max > len[0] + K - len[N-1]) {
            sum = sum - max + len[0] + K - len[N-1];
        } 

        System.out.println(sum);
        sc.close();
    }
} Main public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);

        int K = sc.nextInt();
        int N = sc.nextInt();
        sc.nextLine();

        int[] len = new int[N];

        int sum = 0;
        int max = 0;

        for (int i = 0; i < N; i++) {
            len[i] = sc.nextInt();
        }

        for (int i = 1; i < N; i++) {
            int sa = Math.abs(len[i]  - len[i-1]);
            sum += sa;
            
            if (sa > max) {
                max = sa;
            }
        }
        
        if (max > len[0] + K - len[N-1]) {
            sum = sum - max + len[0] + K - len[N-1];
        } 

        System.out.println(sum);
        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);

        int K = sc.nextInt();
        int N = sc.nextInt();
        sc.nextLine();

        int[] len = new int[N];

        int sum = 0;
        int max = 0;

        for (int i = 0; i < N; i++) {
            len[i] = sc.nextInt();
        }

        for (int i = 1; i < N; i++) {
            int sa = Math.abs(len[i]  - len[i-1]);
            sum += sa;
            
            if (sa > max) {
                max = sa;
            }
        }
        
        if (max > len[0] + K - len[N-1]) {
            sum = sum - max + len[0] + K - len[N-1];
        } 

        System.out.println(sum);
        sc.close();
    } 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 sc.nextLine() sc.nextLine sc int[] len = new int[N]; len new int[N] N int sum = 0; sum 0 int max = 0; max 0 for (int i = 0; i < N; i++) {
            len[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            len[i] = sc.nextInt();
        } {
            len[i] = sc.nextInt();
        } len[i] = sc.nextInt() len[i] len i sc.nextInt() sc.nextInt sc for (int i = 1; i < N; i++) {
            int sa = Math.abs(len[i]  - len[i-1]);
            sum += sa;
            
            if (sa > max) {
                max = sa;
            }
        } int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
            int sa = Math.abs(len[i]  - len[i-1]);
            sum += sa;
            
            if (sa > max) {
                max = sa;
            }
        } {
            int sa = Math.abs(len[i]  - len[i-1]);
            sum += sa;
            
            if (sa > max) {
                max = sa;
            }
        } int sa = Math.abs(len[i]  - len[i-1]); sa Math.abs(len[i]  - len[i-1]) Math.abs Math len[i]  - len[i-1] len[i] len i len[i-1] len i-1 i 1 sum += sa sum sa if (sa > max) {
                max = sa;
            } sa > max sa max {
                max = sa;
            } max = sa max sa if (max > len[0] + K - len[N-1]) {
            sum = sum - max + len[0] + K - len[N-1];
        } max > len[0] + K - len[N-1] max len[0] + K - len[N-1] len[0] + K len[0] len 0 K len[N-1] len N-1 N 1 {
            sum = sum - max + len[0] + K - len[N-1];
        } sum = sum - max + len[0] + K - len[N-1] sum sum - max + len[0] + K - len[N-1] sum - max + len[0] + K sum - max + len[0] + K sum - max sum max len[0] len 0 K len[N-1] len N-1 N 1 System.out.println(sum) System.out.println System.out System System.out sum sc.close() sc.close sc String[] args args
/* package whatever; // don't place package name! */

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();
        sc.nextLine();

        int[] len = new int[N];

        int sum = 0;
        int max = 0;

        for (int i = 0; i < N; i++) {
            len[i] = sc.nextInt();
        }

        for (int i = 1; i < N; i++) {
            int sa = Math.abs(len[i]  - len[i-1]);
            sum += sa;
            
            if (sa > max) {
                max = sa;
            }
        }
        
        if (max > len[0] + K - len[N-1]) {
            sum = sum - max + len[0] + K - len[N-1];
        } 

        System.out.println(sum);
        sc.close();
    }
}