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();
        int[] a = new int[n];
        for(int i =0;i<a.length;i++){
            a[i]=sc.nextInt();
        }
        int t = 0;
        int sum = 0;
        for(int i=0;i<a.length;i++){
            if(i+1-n==0){
                t = Math.max(k+a[0]-a[n-1],t);
                sum += k+a[0]-a[n-1];
            } else {
                t = Math.max(t,a[i+1]-a[i]);
                sum += a[i+1]-a[i];
            }
        }
        System.out.println(sum-t);
    }
} 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();
        int[] a = new int[n];
        for(int i =0;i<a.length;i++){
            a[i]=sc.nextInt();
        }
        int t = 0;
        int sum = 0;
        for(int i=0;i<a.length;i++){
            if(i+1-n==0){
                t = Math.max(k+a[0]-a[n-1],t);
                sum += k+a[0]-a[n-1];
            } else {
                t = Math.max(t,a[i+1]-a[i]);
                sum += a[i+1]-a[i];
            }
        }
        System.out.println(sum-t);
    }
} 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<a.length;i++){
            a[i]=sc.nextInt();
        }
        int t = 0;
        int sum = 0;
        for(int i=0;i<a.length;i++){
            if(i+1-n==0){
                t = Math.max(k+a[0]-a[n-1],t);
                sum += k+a[0]-a[n-1];
            } else {
                t = Math.max(t,a[i+1]-a[i]);
                sum += a[i+1]-a[i];
            }
        }
        System.out.println(sum-t);
    } 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<a.length;i++){
            a[i]=sc.nextInt();
        }
        int t = 0;
        int sum = 0;
        for(int i=0;i<a.length;i++){
            if(i+1-n==0){
                t = Math.max(k+a[0]-a[n-1],t);
                sum += k+a[0]-a[n-1];
            } else {
                t = Math.max(t,a[i+1]-a[i]);
                sum += a[i+1]-a[i];
            }
        }
        System.out.println(sum-t);
    } 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<a.length;i++){
            a[i]=sc.nextInt();
        } int i =0; int i =0; i 0 i<a.length i a.length a a.length 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 t = 0; t 0 int sum = 0; sum 0 for(int i=0;i<a.length;i++){
            if(i+1-n==0){
                t = Math.max(k+a[0]-a[n-1],t);
                sum += k+a[0]-a[n-1];
            } else {
                t = Math.max(t,a[i+1]-a[i]);
                sum += a[i+1]-a[i];
            }
        } int i=0; int i=0; i 0 i<a.length i a.length a a.length i++ i++ i {
            if(i+1-n==0){
                t = Math.max(k+a[0]-a[n-1],t);
                sum += k+a[0]-a[n-1];
            } else {
                t = Math.max(t,a[i+1]-a[i]);
                sum += a[i+1]-a[i];
            }
        } {
            if(i+1-n==0){
                t = Math.max(k+a[0]-a[n-1],t);
                sum += k+a[0]-a[n-1];
            } else {
                t = Math.max(t,a[i+1]-a[i]);
                sum += a[i+1]-a[i];
            }
        } if(i+1-n==0){
                t = Math.max(k+a[0]-a[n-1],t);
                sum += k+a[0]-a[n-1];
            } else {
                t = Math.max(t,a[i+1]-a[i]);
                sum += a[i+1]-a[i];
            } i+1-n==0 i+1-n i+1 i 1 n 0 {
                t = Math.max(k+a[0]-a[n-1],t);
                sum += k+a[0]-a[n-1];
            } t = Math.max(k+a[0]-a[n-1],t) t Math.max(k+a[0]-a[n-1],t) Math.max Math k+a[0]-a[n-1] k+a[0] k a[0] a 0 a[n-1] a n-1 n 1 t sum += k+a[0]-a[n-1] sum k+a[0]-a[n-1] k+a[0] k a[0] a 0 a[n-1] a n-1 n 1 {
                t = Math.max(t,a[i+1]-a[i]);
                sum += a[i+1]-a[i];
            } t = Math.max(t,a[i+1]-a[i]) t Math.max(t,a[i+1]-a[i]) Math.max Math t a[i+1]-a[i] a[i+1] a i+1 i 1 a[i] a i sum += a[i+1]-a[i] sum a[i+1]-a[i] a[i+1] a i+1 i 1 a[i] a i System.out.println(sum-t) System.out.println System.out System System.out sum-t sum t String[] args args
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();
        int[] a = new int[n];
        for(int i =0;i<a.length;i++){
            a[i]=sc.nextInt();
        }
        int t = 0;
        int sum = 0;
        for(int i=0;i<a.length;i++){
            if(i+1-n==0){
                t = Math.max(k+a[0]-a[n-1],t);
                sum += k+a[0]-a[n-1];
            } else {
                t = Math.max(t,a[i+1]-a[i]);
                sum += a[i+1]-a[i];
            }
        }
        System.out.println(sum-t);
    }
}