Problem:
Score : 300 points

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.

The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.

When traveling between these houses, you can only go around the pond.

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Constraints

2 \leq K \leq 10^6

2 \leq N \leq 2 \times 10^5

0 \leq A_1 < ... < A_N < K

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K N
A_1 A_2 ... A_N

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Sample Input 1

20 3
5 10 15

Sample Output 1

10

If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.

Sample Input 2

20 3
0 5 15

Sample Output 2

10

If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

public class Solve
{
    public static void Main(string[] args)
    {
        new ABC_C();
    }
}
public class ABC_A
{
    public ABC_A()
    {
        var s = Ex.Read;
        if (s[2] == s[3] && s[4] == s[5])
        {
            Ex.Cw("Yes");
        }
        else Ex.Cw("No");
    }
}
public class ABC_B
{
    public ABC_B()
    {
        var x = Ex.ReadLong;
        long ans = 0;
        ans += (x / 500) * 1000;
        x %= 500;
        ans += (x / 5) * 5;
        Ex.Cw($"{ans}");
    }
}
public class ABC_C
{
    public ABC_C()
    {
        var arr1 = Ex.ReadLongArray();
        //1周Kメートルの円形の湖があり
        var k = arr1[0];
        //その周りにN軒の家
        var n = arr1[1];

        //var r = k / Math.PI / 2;

        var arr = Ex.ReadLongArray();
        long[] a = new long[n];
        long prev = arr[0];
        for (long i = 1; i < n; i++)
        {
            var j = arr[i];
            a[i] = j - prev;
            prev = j;
        }

        a[0] = k - prev + arr[0];
        Array.Sort(a);

        long ans = 0;
        for (long i = 1; i < n; i++)
        {
            ans += a[i - 1];
        }
        Ex.Cw($"{ans}");
    }
}
public class ABC_D
{
    public ABC_D()
    {

    }
}
public class ABC_E
{
    public ABC_E()
    {

    }
}
public class ABC_F
{
    public ABC_F()
    {

    }
}
public static class Ex
{
    public static void Cw(string s)
    {
        Console.WriteLine(s);
    }

    public static string Read { get { return Console.ReadLine().Trim(); } }
    public static int ReadInt { get { return int.Parse(Read); } }
    public static long ReadLong { get { return long.Parse(Read); } }
    public static double ReadDouble { get { return double.Parse(Read); } }
    public static string[] ReadArray() { return Read.Split(' '); }
    public static int[] ReadIntArray() { return Read.Split(' ').Select(s => int.Parse(s)).ToArray(); }
    public static long[] ReadLongArray() { return Read.Split(' ').Select(s => long.Parse(s)).ToArray(); }
    public static string[] ReadArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; }
    public static int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
    public static long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
}