#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;


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

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

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

    sort(a+1,a+n+1);
    a[0]=0;
    int cnt=1,sum=0;
    for (int i=1;i<=n;i++){
        if (i==1){
            ans[i]=(n-1)*a[i];
            sum+=a[i];
            continue;
        }
        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]);
            cnt=a[i];
        }
        sum+=a[i];
    }
    for (int i=1;i<=n;i++)a[i]=m-a[i];
    cnt=1,sum=0;
    for (int i=n;i>=1;i--){
        if (i==n){
            rans[i]=(n-1)*a[i];
            sum+=a[i];
            continue;
        }
        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]);
            cnt=a[i];
        }
        sum+=a[i];
    }
    int res=ans[n];

    for (int i=0;i<=n;i++)res=min(res,ans[i]+rans[i+1]);
    cout<<res<<"\n";
    return 0;
}