import java.util.Scanner;

/**
 * Another way to put the question is to calculate the longest arc between two houses around the circle. the
 * houses will be visited in such a way that the arc won't be passed through.
 * substract the length of the longest arc from the circumference(k) of the circular pond. that is the answer
 */
public class Main {
    public static void main(String[] args) {
        final Scanner input = new Scanner(System.in);
        int k= input.nextInt();
        int n=input.nextInt();

       int maxArc =0,northToFirst=0,lastToNorth=0;



        //for the first case
        int cur=0,prev=0;

        //get the dis

        for (int i = 1; i <= n; i++) {
            prev =cur;
            cur =input.nextInt();

            //get the distance between the zero point(north) and first house
            if(i==1){
               northToFirst = cur;
            }
            //get the distance between the last house and the zero point (north)
            if(i==n){
                lastToNorth = k-cur;
            }

            //compare every other arcs
            if(i>=2){
                int arc = cur-prev;
                if(arc > maxArc){
                    maxArc = arc;
                }
            }


        }
        //check the last arc. i.e. spack between the first and last houses

        if((lastToNorth + northToFirst) > maxArc){
            maxArc = lastToNorth + northToFirst;
        }

        //answer, i.e min distance around the lake is

        System.out.println(k-maxArc);


    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        final Scanner input = new Scanner(System.in);
        int k= input.nextInt();
        int n=input.nextInt();

       int maxArc =0,northToFirst=0,lastToNorth=0;



        //for the first case
        int cur=0,prev=0;

        //get the dis

        for (int i = 1; i <= n; i++) {
            prev =cur;
            cur =input.nextInt();

            //get the distance between the zero point(north) and first house
            if(i==1){
               northToFirst = cur;
            }
            //get the distance between the last house and the zero point (north)
            if(i==n){
                lastToNorth = k-cur;
            }

            //compare every other arcs
            if(i>=2){
                int arc = cur-prev;
                if(arc > maxArc){
                    maxArc = arc;
                }
            }


        }
        //check the last arc. i.e. spack between the first and last houses

        if((lastToNorth + northToFirst) > maxArc){
            maxArc = lastToNorth + northToFirst;
        }

        //answer, i.e min distance around the lake is

        System.out.println(k-maxArc);


    }
} Main public static void main(String[] args) {
        final Scanner input = new Scanner(System.in);
        int k= input.nextInt();
        int n=input.nextInt();

       int maxArc =0,northToFirst=0,lastToNorth=0;



        //for the first case
        int cur=0,prev=0;

        //get the dis

        for (int i = 1; i <= n; i++) {
            prev =cur;
            cur =input.nextInt();

            //get the distance between the zero point(north) and first house
            if(i==1){
               northToFirst = cur;
            }
            //get the distance between the last house and the zero point (north)
            if(i==n){
                lastToNorth = k-cur;
            }

            //compare every other arcs
            if(i>=2){
                int arc = cur-prev;
                if(arc > maxArc){
                    maxArc = arc;
                }
            }


        }
        //check the last arc. i.e. spack between the first and last houses

        if((lastToNorth + northToFirst) > maxArc){
            maxArc = lastToNorth + northToFirst;
        }

        //answer, i.e min distance around the lake is

        System.out.println(k-maxArc);


    } main {
        final Scanner input = new Scanner(System.in);
        int k= input.nextInt();
        int n=input.nextInt();

       int maxArc =0,northToFirst=0,lastToNorth=0;



        //for the first case
        int cur=0,prev=0;

        //get the dis

        for (int i = 1; i <= n; i++) {
            prev =cur;
            cur =input.nextInt();

            //get the distance between the zero point(north) and first house
            if(i==1){
               northToFirst = cur;
            }
            //get the distance between the last house and the zero point (north)
            if(i==n){
                lastToNorth = k-cur;
            }

            //compare every other arcs
            if(i>=2){
                int arc = cur-prev;
                if(arc > maxArc){
                    maxArc = arc;
                }
            }


        }
        //check the last arc. i.e. spack between the first and last houses

        if((lastToNorth + northToFirst) > maxArc){
            maxArc = lastToNorth + northToFirst;
        }

        //answer, i.e min distance around the lake is

        System.out.println(k-maxArc);


    } final Scanner input = new Scanner(System.in); input new Scanner(System.in) int k= input.nextInt(); k input.nextInt() input.nextInt input int n=input.nextInt(); n input.nextInt() input.nextInt input int maxArc =0 maxArc 0 northToFirst=0 northToFirst 0 lastToNorth=0; lastToNorth 0 int cur=0 cur 0 prev=0; prev 0 for (int i = 1; i <= n; i++) {
            prev =cur;
            cur =input.nextInt();

            //get the distance between the zero point(north) and first house
            if(i==1){
               northToFirst = cur;
            }
            //get the distance between the last house and the zero point (north)
            if(i==n){
                lastToNorth = k-cur;
            }

            //compare every other arcs
            if(i>=2){
                int arc = cur-prev;
                if(arc > maxArc){
                    maxArc = arc;
                }
            }


        } int i = 1; int i = 1; i 1 i <= n i n i++ i++ i {
            prev =cur;
            cur =input.nextInt();

            //get the distance between the zero point(north) and first house
            if(i==1){
               northToFirst = cur;
            }
            //get the distance between the last house and the zero point (north)
            if(i==n){
                lastToNorth = k-cur;
            }

            //compare every other arcs
            if(i>=2){
                int arc = cur-prev;
                if(arc > maxArc){
                    maxArc = arc;
                }
            }


        } {
            prev =cur;
            cur =input.nextInt();

            //get the distance between the zero point(north) and first house
            if(i==1){
               northToFirst = cur;
            }
            //get the distance between the last house and the zero point (north)
            if(i==n){
                lastToNorth = k-cur;
            }

            //compare every other arcs
            if(i>=2){
                int arc = cur-prev;
                if(arc > maxArc){
                    maxArc = arc;
                }
            }


        } prev =cur prev cur cur =input.nextInt() cur input.nextInt() input.nextInt input if(i==1){
               northToFirst = cur;
            } i==1 i 1 {
               northToFirst = cur;
            } northToFirst = cur northToFirst cur if(i==n){
                lastToNorth = k-cur;
            } i==n i n {
                lastToNorth = k-cur;
            } lastToNorth = k-cur lastToNorth k-cur k cur if(i>=2){
                int arc = cur-prev;
                if(arc > maxArc){
                    maxArc = arc;
                }
            } i>=2 i 2 {
                int arc = cur-prev;
                if(arc > maxArc){
                    maxArc = arc;
                }
            } int arc = cur-prev; arc cur-prev cur prev if(arc > maxArc){
                    maxArc = arc;
                } arc > maxArc arc maxArc {
                    maxArc = arc;
                } maxArc = arc maxArc arc if((lastToNorth + northToFirst) > maxArc){
            maxArc = lastToNorth + northToFirst;
        } (lastToNorth + northToFirst) > maxArc (lastToNorth + northToFirst) lastToNorth northToFirst maxArc {
            maxArc = lastToNorth + northToFirst;
        } maxArc = lastToNorth + northToFirst maxArc lastToNorth + northToFirst lastToNorth northToFirst System.out.println(k-maxArc) System.out.println System.out System System.out k-maxArc k maxArc String[] args args public class Main {
    public static void main(String[] args) {
        final Scanner input = new Scanner(System.in);
        int k= input.nextInt();
        int n=input.nextInt();

       int maxArc =0,northToFirst=0,lastToNorth=0;



        //for the first case
        int cur=0,prev=0;

        //get the dis

        for (int i = 1; i <= n; i++) {
            prev =cur;
            cur =input.nextInt();

            //get the distance between the zero point(north) and first house
            if(i==1){
               northToFirst = cur;
            }
            //get the distance between the last house and the zero point (north)
            if(i==n){
                lastToNorth = k-cur;
            }

            //compare every other arcs
            if(i>=2){
                int arc = cur-prev;
                if(arc > maxArc){
                    maxArc = arc;
                }
            }


        }
        //check the last arc. i.e. spack between the first and last houses

        if((lastToNorth + northToFirst) > maxArc){
            maxArc = lastToNorth + northToFirst;
        }

        //answer, i.e min distance around the lake is

        System.out.println(k-maxArc);


    }
} public class Main {
    public static void main(String[] args) {
        final Scanner input = new Scanner(System.in);
        int k= input.nextInt();
        int n=input.nextInt();

       int maxArc =0,northToFirst=0,lastToNorth=0;



        //for the first case
        int cur=0,prev=0;

        //get the dis

        for (int i = 1; i <= n; i++) {
            prev =cur;
            cur =input.nextInt();

            //get the distance between the zero point(north) and first house
            if(i==1){
               northToFirst = cur;
            }
            //get the distance between the last house and the zero point (north)
            if(i==n){
                lastToNorth = k-cur;
            }

            //compare every other arcs
            if(i>=2){
                int arc = cur-prev;
                if(arc > maxArc){
                    maxArc = arc;
                }
            }


        }
        //check the last arc. i.e. spack between the first and last houses

        if((lastToNorth + northToFirst) > maxArc){
            maxArc = lastToNorth + northToFirst;
        }

        //answer, i.e min distance around the lake is

        System.out.println(k-maxArc);


    }
} Main
import java.util.Scanner;

/**
 * Another way to put the question is to calculate the longest arc between two houses around the circle. the
 * houses will be visited in such a way that the arc won't be passed through.
 * substract the length of the longest arc from the circumference(k) of the circular pond. that is the answer
 */
public class Main {
    public static void main(String[] args) {
        final Scanner input = new Scanner(System.in);
        int k= input.nextInt();
        int n=input.nextInt();

       int maxArc =0,northToFirst=0,lastToNorth=0;



        //for the first case
        int cur=0,prev=0;

        //get the dis

        for (int i = 1; i <= n; i++) {
            prev =cur;
            cur =input.nextInt();

            //get the distance between the zero point(north) and first house
            if(i==1){
               northToFirst = cur;
            }
            //get the distance between the last house and the zero point (north)
            if(i==n){
                lastToNorth = k-cur;
            }

            //compare every other arcs
            if(i>=2){
                int arc = cur-prev;
                if(arc > maxArc){
                    maxArc = arc;
                }
            }


        }
        //check the last arc. i.e. spack between the first and last houses

        if((lastToNorth + northToFirst) > maxArc){
            maxArc = lastToNorth + northToFirst;
        }

        //answer, i.e min distance around the lake is

        System.out.println(k-maxArc);


    }
}
