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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

// بِسمِ اللہِ  الرَّحم?نِ  الرَّ حِیم
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define MOD 1000000007
#define ll long long
#define pb push_back
#define all(v) v.begin(),v.end()
#define INF 1e18
#define N 1001000
typedef pair<ll,ll> pll;
ll power(ll a,ll b)
{
    if(b==1)return a;
    if(b==0)return 1;
    ll temp=power(a,b/2);
    ll ans=temp*temp;
    if(b&1)
        ans*=a;
    return ans;
}
ll power(ll a,ll b,ll p)
{
    a=a%p;
    if(b==1)return a;
    if(b==0)return 1;

    ll temp=power(a,b/2,p);
    ll ans=(temp*temp)%p;
    if(b&1)
        ans=(a*ans)%p;
    return ans%p;
}
ll inv(ll a,ll m)
{
    return power(a,m-2,m);
}
//code begins
ll binary(ll a[],ll n,ll key)
{
    ll l=0,r=n-1,mid;
    while(l<=r)
    {
        mid=(l+r)/2;
        if(a[mid]==key)
            return mid;
        else if(a[mid]>key)
            r=mid-1;
        else
            l=mid+1;
    }
    return -1;
}
ll lowerbound(ll a[],ll n,ll key)
{
    ll l=0,r=n-1,mid,ans=-1;
    while(l<=r)
    {
        mid=(l+r)/2;
        if(a[mid]==key)
        {
            r=mid-1;
            ans=mid;
        }
        else if(a[mid]<key)
            l=mid+1;
        else
            r=mid-1;
    }
    return ans;
}
ll upperbound(ll a[],ll n,ll key)
{
    ll l=0,r=n-1,mid,ans=-1;
    while(l<=r)
    {
        mid=(l+r)/2;
        if(a[mid]==key)
        {
            ans=mid;
            l=mid+1;
        }
        else if(a[mid]>key)
            r=mid-1;
        else
            l=mid+1;
    }
    return (ans+1==n?-1:ans+1);
}
signed main()
{
//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);

    cin.sync_with_stdio(false);
    ll t;
    t=1;
//    cin>>t;
    while(t--)
    {
        ll n,k;cin>>k>>n;
        ll a[n+1];
        for(ll i=1;i<=n;i++)cin>>a[i];
        k-=a[n];
        ll ans=INF;
        for(ll i=1;i<=n;i++)
        {
            if(i==1)
                ans=min(ans,min(a[n]-a[1],k+a[i]+a[n]-a[i+1]));
            else if(i==n)
                ans=min(ans,min(a[n]-a[1],k+a[i-1]));
            else
                ans=min(ans,min(a[n]-a[i]+k+a[i-1],a[i]-a[1]+k+a[n]-a[i+1]));
        }
        cout<<ans<<endl;
    }
    return 0;
}