#include <bits/stdc++.h>

#define ll long long
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000009

using namespace std;


ll n,m,a[100100],ans[100100],rans[100100];

void gen(){
    srand(time(NULL));
    n=100000,m=10000;
    for (ll i=1;i<=n;i++){
        a[i]=rand()%m+1;
    }
    cout<<"\n";
}
void input(){
    cin>>n>>m;
    for (ll i=1;i<=n;i++)cin>>a[i];
}

int main(){
    ios;
    memset(a,0,sizeof(a));
    //gen();
    input();
    ans[0]=0,rans[n+1]=0;

    sort(a+1,a+n+1);
    a[0]=0;
    ll cnt=0,sum=0;
    for (ll i=1;i<=n;i++){
        ans[i]=ans[i-1]+cnt*(n-1)-2*sum;
        if (a[i]!=a[i-1]){
            ans[i]+=(n-1)*(a[i]-a[i-1]);
            sum+=a[i]-a[i-1];
            cnt=a[i];
        }
        sum+=a[i-1];
    }
    for (ll i=1;i<=n;i++)a[i]=m-a[i];
    ll res=ans[n];
    cnt=0,sum=0;
    for (ll i=n;i>=1;i--){
        rans[i]=rans[i+1]+cnt*(n-1)-2*sum;
        if (a[i]!=a[i+1]){
            rans[i]+=(n-1)*(a[i]-a[i+1]);
            sum+=a[i]-a[i+1];
            cnt=a[i];
        }
        sum+=a[i+1];
        res=min(res,rans[i]+ans[i-1]);
    }
    cout<<res<<"\n";
    return 0;
}