import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] arr = new int[N];
        int[] dist = new int[N];
        int result = 0;
        

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

        int first = K - arr[N-1] + arr[0];
        dist[0] = first;

        for(int j = 1; j < N; j++) {
            dist[j] = arr[j] - arr[j-1];
        }
        Arrays.sort(dist);

        for(int k = 0; k < N-1; k++) {
            result += dist[k];
        }
        System.out.println(result);
    }
} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner 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[] arr = new int[N];
        int[] dist = new int[N];
        int result = 0;
        

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

        int first = K - arr[N-1] + arr[0];
        dist[0] = first;

        for(int j = 1; j < N; j++) {
            dist[j] = arr[j] - arr[j-1];
        }
        Arrays.sort(dist);

        for(int k = 0; k < N-1; k++) {
            result += dist[k];
        }
        System.out.println(result);
    }
} Main public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] arr = new int[N];
        int[] dist = new int[N];
        int result = 0;
        

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

        int first = K - arr[N-1] + arr[0];
        dist[0] = first;

        for(int j = 1; j < N; j++) {
            dist[j] = arr[j] - arr[j-1];
        }
        Arrays.sort(dist);

        for(int k = 0; k < N-1; k++) {
            result += dist[k];
        }
        System.out.println(result);
    } main {

        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] arr = new int[N];
        int[] dist = new int[N];
        int result = 0;
        

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

        int first = K - arr[N-1] + arr[0];
        dist[0] = first;

        for(int j = 1; j < N; j++) {
            dist[j] = arr[j] - arr[j-1];
        }
        Arrays.sort(dist);

        for(int k = 0; k < N-1; k++) {
            result += dist[k];
        }
        System.out.println(result);
    } 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[] arr = new int[N]; arr new int[N] N int[] dist = new int[N]; dist new int[N] N int result = 0; result 0 for(int i = 0; i < N; i++) {
            arr[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            arr[i] = sc.nextInt();
        } {
            arr[i] = sc.nextInt();
        } arr[i] = sc.nextInt() arr[i] arr i sc.nextInt() sc.nextInt sc int first = K - arr[N-1] + arr[0]; first K - arr[N-1] + arr[0] K - arr[N-1] K arr[N-1] arr N-1 N 1 arr[0] arr 0 dist[0] = first dist[0] dist 0 first for(int j = 1; j < N; j++) {
            dist[j] = arr[j] - arr[j-1];
        } int j = 1; int j = 1; j 1 j < N j N j++ j++ j {
            dist[j] = arr[j] - arr[j-1];
        } {
            dist[j] = arr[j] - arr[j-1];
        } dist[j] = arr[j] - arr[j-1] dist[j] dist j arr[j] - arr[j-1] arr[j] arr j arr[j-1] arr j-1 j 1 Arrays.sort(dist) Arrays.sort Arrays dist for(int k = 0; k < N-1; k++) {
            result += dist[k];
        } int k = 0; int k = 0; k 0 k < N-1 k N-1 N 1 k++ k++ k {
            result += dist[k];
        } {
            result += dist[k];
        } result += dist[k] result dist[k] dist k System.out.println(result) System.out.println System.out System System.out result 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[] arr = new int[N];
        int[] dist = new int[N];
        int result = 0;
        

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

        int first = K - arr[N-1] + arr[0];
        dist[0] = first;

        for(int j = 1; j < N; j++) {
            dist[j] = arr[j] - arr[j-1];
        }
        Arrays.sort(dist);

        for(int k = 0; k < N-1; k++) {
            result += dist[k];
        }
        System.out.println(result);
    }
} public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] arr = new int[N];
        int[] dist = new int[N];
        int result = 0;
        

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

        int first = K - arr[N-1] + arr[0];
        dist[0] = first;

        for(int j = 1; j < N; j++) {
            dist[j] = arr[j] - arr[j-1];
        }
        Arrays.sort(dist);

        for(int k = 0; k < N-1; k++) {
            result += dist[k];
        }
        System.out.println(result);
    }
} Main
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] arr = new int[N];
        int[] dist = new int[N];
        int result = 0;
        

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

        int first = K - arr[N-1] + arr[0];
        dist[0] = first;

        for(int j = 1; j < N; j++) {
            dist[j] = arr[j] - arr[j-1];
        }
        Arrays.sort(dist);

        for(int k = 0; k < N-1; k++) {
            result += dist[k];
        }
        System.out.println(result);
    }
}
