

import java.util.*;

class Main {

    public static void main(String args[]) {
        Scanner s = new Scanner(System.in);

        int k = s.nextInt();
        int n = s.nextInt();
        int[] a=new int[n];
        int[] d=new int[n];
        for (int i = 0; i <n ; i++) {
             a[i] = s.nextInt();
             if(i==0){
             }else{
                 d[i]=a[i]-a[i-1];
             }
        }
        d[0]=k-a[n-1]+a[0];

        int max=-1;
        int sum=0;
        for (int i = 0; i <n ; i++) {
            sum+=d[i];
            if(max<d[i]){
                max=d[i];
            }
        }
        System.out.println(sum-max);
    }

} import java.util.*; util.* java class Main {

    public static void main(String args[]) {
        Scanner s = new Scanner(System.in);

        int k = s.nextInt();
        int n = s.nextInt();
        int[] a=new int[n];
        int[] d=new int[n];
        for (int i = 0; i <n ; i++) {
             a[i] = s.nextInt();
             if(i==0){
             }else{
                 d[i]=a[i]-a[i-1];
             }
        }
        d[0]=k-a[n-1]+a[0];

        int max=-1;
        int sum=0;
        for (int i = 0; i <n ; i++) {
            sum+=d[i];
            if(max<d[i]){
                max=d[i];
            }
        }
        System.out.println(sum-max);
    }

} Main public static void main(String args[]) {
        Scanner s = new Scanner(System.in);

        int k = s.nextInt();
        int n = s.nextInt();
        int[] a=new int[n];
        int[] d=new int[n];
        for (int i = 0; i <n ; i++) {
             a[i] = s.nextInt();
             if(i==0){
             }else{
                 d[i]=a[i]-a[i-1];
             }
        }
        d[0]=k-a[n-1]+a[0];

        int max=-1;
        int sum=0;
        for (int i = 0; i <n ; i++) {
            sum+=d[i];
            if(max<d[i]){
                max=d[i];
            }
        }
        System.out.println(sum-max);
    } main {
        Scanner s = new Scanner(System.in);

        int k = s.nextInt();
        int n = s.nextInt();
        int[] a=new int[n];
        int[] d=new int[n];
        for (int i = 0; i <n ; i++) {
             a[i] = s.nextInt();
             if(i==0){
             }else{
                 d[i]=a[i]-a[i-1];
             }
        }
        d[0]=k-a[n-1]+a[0];

        int max=-1;
        int sum=0;
        for (int i = 0; i <n ; i++) {
            sum+=d[i];
            if(max<d[i]){
                max=d[i];
            }
        }
        System.out.println(sum-max);
    } Scanner s = new Scanner(System.in); s new Scanner(System.in) int k = s.nextInt(); k s.nextInt() s.nextInt s int n = s.nextInt(); n s.nextInt() s.nextInt s int[] a=new int[n]; a new int[n] n int[] d=new int[n]; d new int[n] n for (int i = 0; i <n ; i++) {
             a[i] = s.nextInt();
             if(i==0){
             }else{
                 d[i]=a[i]-a[i-1];
             }
        } int i = 0; int i = 0; i 0 i <n i n i++ i++ i {
             a[i] = s.nextInt();
             if(i==0){
             }else{
                 d[i]=a[i]-a[i-1];
             }
        } {
             a[i] = s.nextInt();
             if(i==0){
             }else{
                 d[i]=a[i]-a[i-1];
             }
        } a[i] = s.nextInt() a[i] a i s.nextInt() s.nextInt s if(i==0){
             }else{
                 d[i]=a[i]-a[i-1];
             } i==0 i 0 {
             } {
                 d[i]=a[i]-a[i-1];
             } d[i]=a[i]-a[i-1] d[i] d i a[i]-a[i-1] a[i] a i a[i-1] a i-1 i 1 d[0]=k-a[n-1]+a[0] d[0] d 0 k-a[n-1]+a[0] k-a[n-1] k a[n-1] a n-1 n 1 a[0] a 0 int max=-1; max -1 1 int sum=0; sum 0 for (int i = 0; i <n ; i++) {
            sum+=d[i];
            if(max<d[i]){
                max=d[i];
            }
        } int i = 0; int i = 0; i 0 i <n i n i++ i++ i {
            sum+=d[i];
            if(max<d[i]){
                max=d[i];
            }
        } {
            sum+=d[i];
            if(max<d[i]){
                max=d[i];
            }
        } sum+=d[i] sum d[i] d i if(max<d[i]){
                max=d[i];
            } max<d[i] max d[i] d i {
                max=d[i];
            } max=d[i] max d[i] d i System.out.println(sum-max) System.out.println System.out System System.out sum-max sum max String args[] args


import java.util.*;

class Main {

    public static void main(String args[]) {
        Scanner s = new Scanner(System.in);

        int k = s.nextInt();
        int n = s.nextInt();
        int[] a=new int[n];
        int[] d=new int[n];
        for (int i = 0; i <n ; i++) {
             a[i] = s.nextInt();
             if(i==0){
             }else{
                 d[i]=a[i]-a[i-1];
             }
        }
        d[0]=k-a[n-1]+a[0];

        int max=-1;
        int sum=0;
        for (int i = 0; i <n ; i++) {
            sum+=d[i];
            if(max<d[i]){
                max=d[i];
            }
        }
        System.out.println(sum-max);
    }

}