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

public class Main{
    
 
    public static void main(String[] args)  {
        new Main().solve();
    }

    void solve(){
        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());
        int N = Integer.parseInt(sc.next());

        int[] a = new int[N];
        for(int i=0; i<N; i++){
            a[i] = Integer.parseInt(sc.next());
        }

        sc.close();

        int[] b = new int[N];
        b[0] = a[1] - a[0];
        for(int i=1; i<N-1; i++){
            b[i] = a[i+1] - a[i];
        }
        b[N-1] = K - a[N-1] + a[0];

        // System.out.println(Arrays.toString(b));

        ArrayList<Integer> list = new ArrayList<>();
        for(int i=0; i< N; i++){
            list.add(b[i]);
        }

        Arrays.sort(b);
        int result=0;
        for(int i=0; i<N-1; i++){
            result += b[i];
        }

        System.out.println(result);

    }
} import java.util.ArrayList; ArrayList java.util import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main{
    
 
    public static void main(String[] args)  {
        new Main().solve();
    }

    void solve(){
        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());
        int N = Integer.parseInt(sc.next());

        int[] a = new int[N];
        for(int i=0; i<N; i++){
            a[i] = Integer.parseInt(sc.next());
        }

        sc.close();

        int[] b = new int[N];
        b[0] = a[1] - a[0];
        for(int i=1; i<N-1; i++){
            b[i] = a[i+1] - a[i];
        }
        b[N-1] = K - a[N-1] + a[0];

        // System.out.println(Arrays.toString(b));

        ArrayList<Integer> list = new ArrayList<>();
        for(int i=0; i< N; i++){
            list.add(b[i]);
        }

        Arrays.sort(b);
        int result=0;
        for(int i=0; i<N-1; i++){
            result += b[i];
        }

        System.out.println(result);

    }
} Main public static void main(String[] args)  {
        new Main().solve();
    } main {
        new Main().solve();
    } new Main().solve() new Main().solve new Main() String[] args args void solve(){
        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());
        int N = Integer.parseInt(sc.next());

        int[] a = new int[N];
        for(int i=0; i<N; i++){
            a[i] = Integer.parseInt(sc.next());
        }

        sc.close();

        int[] b = new int[N];
        b[0] = a[1] - a[0];
        for(int i=1; i<N-1; i++){
            b[i] = a[i+1] - a[i];
        }
        b[N-1] = K - a[N-1] + a[0];

        // System.out.println(Arrays.toString(b));

        ArrayList<Integer> list = new ArrayList<>();
        for(int i=0; i< N; i++){
            list.add(b[i]);
        }

        Arrays.sort(b);
        int result=0;
        for(int i=0; i<N-1; i++){
            result += b[i];
        }

        System.out.println(result);

    } solve {
        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());
        int N = Integer.parseInt(sc.next());

        int[] a = new int[N];
        for(int i=0; i<N; i++){
            a[i] = Integer.parseInt(sc.next());
        }

        sc.close();

        int[] b = new int[N];
        b[0] = a[1] - a[0];
        for(int i=1; i<N-1; i++){
            b[i] = a[i+1] - a[i];
        }
        b[N-1] = K - a[N-1] + a[0];

        // System.out.println(Arrays.toString(b));

        ArrayList<Integer> list = new ArrayList<>();
        for(int i=0; i< N; i++){
            list.add(b[i]);
        }

        Arrays.sort(b);
        int result=0;
        for(int i=0; i<N-1; i++){
            result += b[i];
        }

        System.out.println(result);

    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = Integer.parseInt(sc.next()); K Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int N = Integer.parseInt(sc.next()); N Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int[] a = new int[N]; a new int[N] N for(int i=0; i<N; i++){
            a[i] = Integer.parseInt(sc.next());
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            a[i] = Integer.parseInt(sc.next());
        } {
            a[i] = Integer.parseInt(sc.next());
        } a[i] = Integer.parseInt(sc.next()) a[i] a i Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc sc.close() sc.close sc int[] b = new int[N]; b new int[N] N b[0] = a[1] - a[0] b[0] b 0 a[1] - a[0] a[1] a 1 a[0] a 0 for(int i=1; i<N-1; i++){
            b[i] = a[i+1] - a[i];
        } int i=1; int i=1; i 1 i<N-1 i N-1 N 1 i++ i++ i {
            b[i] = a[i+1] - a[i];
        } {
            b[i] = a[i+1] - a[i];
        } b[i] = a[i+1] - a[i] b[i] b i a[i+1] - a[i] a[i+1] a i+1 i 1 a[i] a i b[N-1] = K - a[N-1] + a[0] b[N-1] b 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 ArrayList<Integer> list = new ArrayList<>(); list new ArrayList<>() for(int i=0; i< N; i++){
            list.add(b[i]);
        } int i=0; int i=0; i 0 i< N i N i++ i++ i {
            list.add(b[i]);
        } {
            list.add(b[i]);
        } list.add(b[i]) list.add list b[i] b i Arrays.sort(b) Arrays.sort Arrays b int result=0; result 0 for(int i=0; i<N-1; i++){
            result += b[i];
        } int i=0; int i=0; i 0 i<N-1 i N-1 N 1 i++ i++ i {
            result += b[i];
        } {
            result += b[i];
        } result += b[i] result b[i] b i System.out.println(result) System.out.println System.out System System.out result public class Main{
    
 
    public static void main(String[] args)  {
        new Main().solve();
    }

    void solve(){
        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());
        int N = Integer.parseInt(sc.next());

        int[] a = new int[N];
        for(int i=0; i<N; i++){
            a[i] = Integer.parseInt(sc.next());
        }

        sc.close();

        int[] b = new int[N];
        b[0] = a[1] - a[0];
        for(int i=1; i<N-1; i++){
            b[i] = a[i+1] - a[i];
        }
        b[N-1] = K - a[N-1] + a[0];

        // System.out.println(Arrays.toString(b));

        ArrayList<Integer> list = new ArrayList<>();
        for(int i=0; i< N; i++){
            list.add(b[i]);
        }

        Arrays.sort(b);
        int result=0;
        for(int i=0; i<N-1; i++){
            result += b[i];
        }

        System.out.println(result);

    }
} public class Main{
    
 
    public static void main(String[] args)  {
        new Main().solve();
    }

    void solve(){
        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());
        int N = Integer.parseInt(sc.next());

        int[] a = new int[N];
        for(int i=0; i<N; i++){
            a[i] = Integer.parseInt(sc.next());
        }

        sc.close();

        int[] b = new int[N];
        b[0] = a[1] - a[0];
        for(int i=1; i<N-1; i++){
            b[i] = a[i+1] - a[i];
        }
        b[N-1] = K - a[N-1] + a[0];

        // System.out.println(Arrays.toString(b));

        ArrayList<Integer> list = new ArrayList<>();
        for(int i=0; i< N; i++){
            list.add(b[i]);
        }

        Arrays.sort(b);
        int result=0;
        for(int i=0; i<N-1; i++){
            result += b[i];
        }

        System.out.println(result);

    }
} Main
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main{
    
 
    public static void main(String[] args)  {
        new Main().solve();
    }

    void solve(){
        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());
        int N = Integer.parseInt(sc.next());

        int[] a = new int[N];
        for(int i=0; i<N; i++){
            a[i] = Integer.parseInt(sc.next());
        }

        sc.close();

        int[] b = new int[N];
        b[0] = a[1] - a[0];
        for(int i=1; i<N-1; i++){
            b[i] = a[i+1] - a[i];
        }
        b[N-1] = K - a[N-1] + a[0];

        // System.out.println(Arrays.toString(b));

        ArrayList<Integer> list = new ArrayList<>();
        for(int i=0; i< N; i++){
            list.add(b[i]);
        }

        Arrays.sort(b);
        int result=0;
        for(int i=0; i<N-1; i++){
            result += b[i];
        }

        System.out.println(result);

    }
}