#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define sf scanf
#define pf printf
#define itr iterator
#define SQR(x) ((x) * (x))
#define loop(i, y) for (int i = 0; i < int(y); i++)
#define FOR(i, x, y) for (int i = int(x); i <= int(y); i++)
#define ROF(i, y, x) for (int i = int(y); i >= int(x); i--)
#define ALL(c) c.begin(), c.end()
#define SZ(c) int(c.size())
#define CLR(x, y) memset(x, y, sizeof(x))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define si(x) scanf("%d", &x)
#define sii(x, y) scanf("%d %d", &x, &y)
#define siii(x, y, z) scanf("%d %d %d", &x, &y, &z)
#define sl(x) scanf("%lld", &x)
#define sll(x, y) scanf("%lld %lld", &x, &y)
#define slll(x, y, z) scanf("%lld %lld %lld", &x, &y, &z)
#define sllll(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)
#define FileIO                        \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout);
#define FastIO                   \
    ios::sync_with_stdio(false); \
    cin.tie(0);
#define FixedPrecision(x)        \
    ios::sync_with_stdio(false); \
    cout.setf(ios::fixed);       \
    cout.precision(x);           \
    cin.tie(0);
#define bitCheck(N, in) ((bool)(N & (1LL << (in))))
#define bitOff(N, in) (N & (~(1LL << (in))))
#define bitOn(N, in) (N | (1LL << (in)))
#define bitFlip(a, k) (a ^ (1LL << (k)))
#define start_time clock_t tStart = clock();
#define execution_time printf("\n>>Time taken: %.10fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);
#define popCount(x) __builtin_popcount(x)
#define int2Bin(x, y) \
    bitset<y> z = x;  \
    cout << z.to_string() << endl;
#define tr(it, container) for (auto it = container.begin(); it != container.end(); it++)
#define unq(v) (v).erase(unique(ALL(v)), v.end())
#define common(a, b) sort(ALL(a)), sort(ALL(b)), a.erase(set_intersection(ALL(a), ALL(b), a.begin()), a.end())
#define uncommon(a, b) sort(ALL(a)), sort(ALL(b)), a.erase(set_symmetric_difference(ALL(a), ALL(b), a.begin()), a.end())
#define DB(x) cout << #x << " = " << x << endl
#define DDB(x, y) cout << #x << " = " << x << "   " << #y << " = " << y << endl
#define DDDB(x, y, z) cout << #x << " = " << x << "   " << #y << " = " << y << "   " << #z << " = " << z << endl


template <class T>inline T bigMod(T p, T e, T M){ll ret = 1;for (; e > 0; e >>= 1){if (e & 1)ret = (ret * p) % M;p = (p * p) % M;}return (T)ret;}
template <class T>inline T modInverse(T a, T M) { return bigMod(a, M - 2, M); }
template <class T>inline T gcd(T a, T b){if (b == 0)return a;return gcd(b, a % b);}
template <class T>inline T lcm(T a, T b){a = abs(a);b = abs(b);return (a / gcd(a, b)) * b;}
template <class T>inline T diss(T x1, T x2, T y1, T y2) { return sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2))); }
template <class T>inline T disss(T x1, T x2, T z1, T y1, T y2, T z2) { return sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)) + ((z1 - z2) * (z1 - z2))); }
template <class T>inline string int2String(T a){ostringstream str;str << a;return str.str();}
template <class T>inline T string2int(string a){stringstream str(a);T b;str >> b;return b;}
const int dr[] = {0, 1, 0, -1, -1, 1, 1, -1, -2, -2, 2, 2, -1, -1, 1, 1};
const int dc[] = {1, 0, -1, 0, 1, 1, -1, -1, -1, 1, -1, 1, -2, 2, -2, 2};

/// Constants

#define MAXN 1000005
#define MAX 200005
#define MOD 1000000007
#define BASE 1000000009
#define eps 1e-9
#define INF 1llu << 61 // 2,305,843,009,213,693,952
#define inf 1 << 29    // 536,870,912
#define PI acos(-1.0)  // 3.1415926535897932
#define E 2.71828182845904523536



bool is_prime(long long n)
{

    if (n == 1)
        return false;
    for (long long i = 2; (i * i) <= n; i++)
        if (n % i == 0)
            return false;
    return true;
}

bool cmp(const pair<ll,ll> &a, const pair<ll,ll> &b) 
{ 
    if(a.ff==b.ff)return (a.ss > b.ss);
    return (a.ff < b.ff); 
} 




int dig_cnt (long long n){return ((int)(log10(n))+1 );}


unordered_set<ll> st;

void primeFactors(ll n)  
{  
    // Print the number of 2s that divide n  
    while (n % 2 == 0)  
    {  
        st.insert(2);  
        n = n/2;  
    }  
  
    // n must be odd at this point. So we can skip  
    // one element (Note i = i +2)  
    for (int i = 3; i <= sqrt(n); i = i + 2)  
    {  
        // While i divides n, print i and divide n  
        while (n % i == 0)  
        {  
            st.insert(i); 
            n = n/i;  
        }  
    }  
  
    // This condition is to handle the case when n  
    // is a prime number greater than 2  
    if (n > 2)  
        st.insert(n);  
}


double fact(double n); 
double nCr(double n, double r) ;

double nCr(double n, double r) 
{ 
    return fact(n) / (fact(r) * fact(n - r)); 
} 
  
// Returns factorial of n 
double fact(double n) 
{ 
    double res = 1; 
    for (double i = 2; i <= n; i++) 
        res = res * i; 
    return res; 
} 

double pw(double a , double b){
    double rs = 1;
    for(int i = 0; i < b ; i++){
        rs*=a;
    }
    return rs;
}

// Returns the absolute value of n-mid*mid*mid
double diff(double n, double mid)
{
    if (n > (mid * mid * mid))
        return (n - (mid * mid * mid));
    else
        return ((mid * mid * mid) - n);
}

// Returns cube root of a no n
double cubicRoot(double n)
{
    // Set start and end for binary search
    double start = 0, end = n;

    // Set precision
    double e = 0.0000001;

    while (true)
    {
        double mid = (start + end) / 2;
        double error = diff(n, mid);

        // If error is less than e then mid is
        // our answer so return mid
        if (error <= e)
            return mid;

        // If mid*mid*mid is greater than n set
        // end = mid
        if ((mid * mid * mid) > n)
            end = mid;

        // If mid*mid*mid is less than n set
        // start = mid
        else
            start = mid;
    }
}

int main()
{




ll k , n ; sll(k,n);

vl v(n); for(auto &a: v)sl(a);
sort(ALL(v));

ll mx = (k -v[n-1]) + (v[0]-0);
ll in = 0;

for(int i = 0 ; i < n-1 ; i ++){
    if(v[i+1]-v[i] > mx){
        mx = v[i + 1] - v[i];
        in = i+1;
    }
}

ll ans = 0 ;

for(int i = in , j = 0; j < n-1 ; i=(i+1)%n,j++ ){
    
    if( i+1 == n)ans+= (k-v[i])+v[(i+1)%n];
    else ans += abs(v[i+1]-v[i]);
}


cout << ans << endl;




    
    return 0;
}

Measured memory_bytes: 1792