import java.io.*;
import java.util.*;
import java.lang.*;

public class Main {
    static class FastReader
        {
            BufferedReader br;
            StringTokenizer st;

            public FastReader()
            {
                br = new BufferedReader(new
                        InputStreamReader(System.in));
            }

            String next()
            {
                while (st == null || !st.hasMoreElements())
                {
                    try
                    {
                        st = new StringTokenizer(br.readLine());
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
                return st.nextToken();
            }

            int nextInt()
            {
                return Integer.parseInt(next());
            }

            long nextLong()
            {
                return Long.parseLong(next());
            }

            double nextDouble()
            {
                return Double.parseDouble(next());
            }

            String nextLine()
            {
                String str = "";
                try
                {
                    str = br.readLine();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                return str;
            }
        }
    public static void main(String[] args) {
        FastReader ob = new FastReader();
        int k =ob.nextInt(),n=ob.nextInt();
        int arr[] =  new int[n];
        int max_diff=-1,start=0,end=0;
        long sum=0;
        arr[0]=ob.nextInt();
        for (int i = 1; i < n; i++) {
            arr[i]=ob.nextInt();
            sum+=(arr[i]-arr[i-1]);
            max_diff=Math.max(arr[i]-arr[i-1],max_diff);
        }
        max_diff=Math.max(k-arr[n-1]+arr[0],max_diff);
        sum+=k-arr[n-1]+arr[0];
        System.out.println(sum-max_diff);



    }
} import java.io.*; io.* java import java.util.*; util.* java import java.lang.*; lang.* java public class Main {
    static class FastReader
        {
            BufferedReader br;
            StringTokenizer st;

            public FastReader()
            {
                br = new BufferedReader(new
                        InputStreamReader(System.in));
            }

            String next()
            {
                while (st == null || !st.hasMoreElements())
                {
                    try
                    {
                        st = new StringTokenizer(br.readLine());
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
                return st.nextToken();
            }

            int nextInt()
            {
                return Integer.parseInt(next());
            }

            long nextLong()
            {
                return Long.parseLong(next());
            }

            double nextDouble()
            {
                return Double.parseDouble(next());
            }

            String nextLine()
            {
                String str = "";
                try
                {
                    str = br.readLine();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                return str;
            }
        }
    public static void main(String[] args) {
        FastReader ob = new FastReader();
        int k =ob.nextInt(),n=ob.nextInt();
        int arr[] =  new int[n];
        int max_diff=-1,start=0,end=0;
        long sum=0;
        arr[0]=ob.nextInt();
        for (int i = 1; i < n; i++) {
            arr[i]=ob.nextInt();
            sum+=(arr[i]-arr[i-1]);
            max_diff=Math.max(arr[i]-arr[i-1],max_diff);
        }
        max_diff=Math.max(k-arr[n-1]+arr[0],max_diff);
        sum+=k-arr[n-1]+arr[0];
        System.out.println(sum-max_diff);



    }
} Main public static void main(String[] args) {
        FastReader ob = new FastReader();
        int k =ob.nextInt(),n=ob.nextInt();
        int arr[] =  new int[n];
        int max_diff=-1,start=0,end=0;
        long sum=0;
        arr[0]=ob.nextInt();
        for (int i = 1; i < n; i++) {
            arr[i]=ob.nextInt();
            sum+=(arr[i]-arr[i-1]);
            max_diff=Math.max(arr[i]-arr[i-1],max_diff);
        }
        max_diff=Math.max(k-arr[n-1]+arr[0],max_diff);
        sum+=k-arr[n-1]+arr[0];
        System.out.println(sum-max_diff);



    } main {
        FastReader ob = new FastReader();
        int k =ob.nextInt(),n=ob.nextInt();
        int arr[] =  new int[n];
        int max_diff=-1,start=0,end=0;
        long sum=0;
        arr[0]=ob.nextInt();
        for (int i = 1; i < n; i++) {
            arr[i]=ob.nextInt();
            sum+=(arr[i]-arr[i-1]);
            max_diff=Math.max(arr[i]-arr[i-1],max_diff);
        }
        max_diff=Math.max(k-arr[n-1]+arr[0],max_diff);
        sum+=k-arr[n-1]+arr[0];
        System.out.println(sum-max_diff);



    } FastReader ob = new FastReader(); ob new FastReader() int k =ob.nextInt() k ob.nextInt() ob.nextInt ob n=ob.nextInt(); n ob.nextInt() ob.nextInt ob int arr[] =  new int[n]; arr new int[n] n int max_diff=-1 max_diff -1 1 start=0 start 0 end=0; end 0 long sum=0; sum 0 arr[0]=ob.nextInt() arr[0] arr 0 ob.nextInt() ob.nextInt ob for (int i = 1; i < n; i++) {
            arr[i]=ob.nextInt();
            sum+=(arr[i]-arr[i-1]);
            max_diff=Math.max(arr[i]-arr[i-1],max_diff);
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            arr[i]=ob.nextInt();
            sum+=(arr[i]-arr[i-1]);
            max_diff=Math.max(arr[i]-arr[i-1],max_diff);
        } {
            arr[i]=ob.nextInt();
            sum+=(arr[i]-arr[i-1]);
            max_diff=Math.max(arr[i]-arr[i-1],max_diff);
        } arr[i]=ob.nextInt() arr[i] arr i ob.nextInt() ob.nextInt ob sum+=(arr[i]-arr[i-1]) sum (arr[i]-arr[i-1]) arr[i] arr i arr[i-1] arr i-1 i 1 max_diff=Math.max(arr[i]-arr[i-1],max_diff) max_diff Math.max(arr[i]-arr[i-1],max_diff) Math.max Math arr[i]-arr[i-1] arr[i] arr i arr[i-1] arr i-1 i 1 max_diff max_diff=Math.max(k-arr[n-1]+arr[0],max_diff) max_diff Math.max(k-arr[n-1]+arr[0],max_diff) Math.max Math k-arr[n-1]+arr[0] k-arr[n-1] k arr[n-1] arr n-1 n 1 arr[0] arr 0 max_diff sum+=k-arr[n-1]+arr[0] sum k-arr[n-1]+arr[0] k-arr[n-1] k arr[n-1] arr n-1 n 1 arr[0] arr 0 System.out.println(sum-max_diff) System.out.println System.out System System.out sum-max_diff sum max_diff String[] args args static class FastReader
        {
            BufferedReader br;
            StringTokenizer st;

            public FastReader()
            {
                br = new BufferedReader(new
                        InputStreamReader(System.in));
            }

            String next()
            {
                while (st == null || !st.hasMoreElements())
                {
                    try
                    {
                        st = new StringTokenizer(br.readLine());
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
                return st.nextToken();
            }

            int nextInt()
            {
                return Integer.parseInt(next());
            }

            long nextLong()
            {
                return Long.parseLong(next());
            }

            double nextDouble()
            {
                return Double.parseDouble(next());
            }

            String nextLine()
            {
                String str = "";
                try
                {
                    str = br.readLine();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                return str;
            }
        } FastReader BufferedReader br; br StringTokenizer st; st public FastReader()
            {
                br = new BufferedReader(new
                        InputStreamReader(System.in));
            } FastReader {
                br = new BufferedReader(new
                        InputStreamReader(System.in));
            } br = new BufferedReader(new
                        InputStreamReader(System.in)) br new BufferedReader(new
                        InputStreamReader(System.in)) String next()
            {
                while (st == null || !st.hasMoreElements())
                {
                    try
                    {
                        st = new StringTokenizer(br.readLine());
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
                return st.nextToken();
            } next {
                while (st == null || !st.hasMoreElements())
                {
                    try
                    {
                        st = new StringTokenizer(br.readLine());
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
                return st.nextToken();
            } while (st == null || !st.hasMoreElements())
                {
                    try
                    {
                        st = new StringTokenizer(br.readLine());
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                } st == null || !st.hasMoreElements() st == null st null !st.hasMoreElements() st.hasMoreElements() st.hasMoreElements st {
                    try
                    {
                        st = new StringTokenizer(br.readLine());
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                } try
                    {
                        st = new StringTokenizer(br.readLine());
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    } IOException e {
                        e.printStackTrace();
                    } e.printStackTrace() e.printStackTrace e {
                        st = new StringTokenizer(br.readLine());
                    } st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st int nextInt()
            {
                return Integer.parseInt(next());
            } nextInt {
                return Integer.parseInt(next());
            } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next long nextLong()
            {
                return Long.parseLong(next());
            } nextLong {
                return Long.parseLong(next());
            } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next double nextDouble()
            {
                return Double.parseDouble(next());
            } nextDouble {
                return Double.parseDouble(next());
            } return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next String nextLine()
            {
                String str = "";
                try
                {
                    str = br.readLine();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                return str;
            } nextLine {
                String str = "";
                try
                {
                    str = br.readLine();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                return str;
            } String str = ""; str "" try
                {
                    str = br.readLine();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                } catch (IOException e)
                {
                    e.printStackTrace();
                } IOException e {
                    e.printStackTrace();
                } e.printStackTrace() e.printStackTrace e {
                    str = br.readLine();
                } str = br.readLine() str br.readLine() br.readLine br return str; str public class Main {
    static class FastReader
        {
            BufferedReader br;
            StringTokenizer st;

            public FastReader()
            {
                br = new BufferedReader(new
                        InputStreamReader(System.in));
            }

            String next()
            {
                while (st == null || !st.hasMoreElements())
                {
                    try
                    {
                        st = new StringTokenizer(br.readLine());
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
                return st.nextToken();
            }

            int nextInt()
            {
                return Integer.parseInt(next());
            }

            long nextLong()
            {
                return Long.parseLong(next());
            }

            double nextDouble()
            {
                return Double.parseDouble(next());
            }

            String nextLine()
            {
                String str = "";
                try
                {
                    str = br.readLine();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                return str;
            }
        }
    public static void main(String[] args) {
        FastReader ob = new FastReader();
        int k =ob.nextInt(),n=ob.nextInt();
        int arr[] =  new int[n];
        int max_diff=-1,start=0,end=0;
        long sum=0;
        arr[0]=ob.nextInt();
        for (int i = 1; i < n; i++) {
            arr[i]=ob.nextInt();
            sum+=(arr[i]-arr[i-1]);
            max_diff=Math.max(arr[i]-arr[i-1],max_diff);
        }
        max_diff=Math.max(k-arr[n-1]+arr[0],max_diff);
        sum+=k-arr[n-1]+arr[0];
        System.out.println(sum-max_diff);



    }
} public class Main {
    static class FastReader
        {
            BufferedReader br;
            StringTokenizer st;

            public FastReader()
            {
                br = new BufferedReader(new
                        InputStreamReader(System.in));
            }

            String next()
            {
                while (st == null || !st.hasMoreElements())
                {
                    try
                    {
                        st = new StringTokenizer(br.readLine());
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
                return st.nextToken();
            }

            int nextInt()
            {
                return Integer.parseInt(next());
            }

            long nextLong()
            {
                return Long.parseLong(next());
            }

            double nextDouble()
            {
                return Double.parseDouble(next());
            }

            String nextLine()
            {
                String str = "";
                try
                {
                    str = br.readLine();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                return str;
            }
        }
    public static void main(String[] args) {
        FastReader ob = new FastReader();
        int k =ob.nextInt(),n=ob.nextInt();
        int arr[] =  new int[n];
        int max_diff=-1,start=0,end=0;
        long sum=0;
        arr[0]=ob.nextInt();
        for (int i = 1; i < n; i++) {
            arr[i]=ob.nextInt();
            sum+=(arr[i]-arr[i-1]);
            max_diff=Math.max(arr[i]-arr[i-1],max_diff);
        }
        max_diff=Math.max(k-arr[n-1]+arr[0],max_diff);
        sum+=k-arr[n-1]+arr[0];
        System.out.println(sum-max_diff);



    }
} Main
import java.io.*;
import java.util.*;
import java.lang.*;

public class Main {
    static class FastReader
        {
            BufferedReader br;
            StringTokenizer st;

            public FastReader()
            {
                br = new BufferedReader(new
                        InputStreamReader(System.in));
            }

            String next()
            {
                while (st == null || !st.hasMoreElements())
                {
                    try
                    {
                        st = new StringTokenizer(br.readLine());
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
                return st.nextToken();
            }

            int nextInt()
            {
                return Integer.parseInt(next());
            }

            long nextLong()
            {
                return Long.parseLong(next());
            }

            double nextDouble()
            {
                return Double.parseDouble(next());
            }

            String nextLine()
            {
                String str = "";
                try
                {
                    str = br.readLine();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                return str;
            }
        }
    public static void main(String[] args) {
        FastReader ob = new FastReader();
        int k =ob.nextInt(),n=ob.nextInt();
        int arr[] =  new int[n];
        int max_diff=-1,start=0,end=0;
        long sum=0;
        arr[0]=ob.nextInt();
        for (int i = 1; i < n; i++) {
            arr[i]=ob.nextInt();
            sum+=(arr[i]-arr[i-1]);
            max_diff=Math.max(arr[i]-arr[i-1],max_diff);
        }
        max_diff=Math.max(k-arr[n-1]+arr[0],max_diff);
        sum+=k-arr[n-1]+arr[0];
        System.out.println(sum-max_diff);



    }
}