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

using ll=long long;
int main(){
    ll N,M;
    cin>>N>>M;

    vector<ll> L(N),R(N);

    for(int i=0;i<N;i++) cin>>L[i]>>R[i],R[i]++;
    vector<int> inv;
    map<ll,int> mpX;
    {
        set<ll> s;
        for(auto& x:L) s.insert(x);
        for(auto& x:R) s.insert(x);
        int id=0;
        inv.resize(s.size());
        for(auto& x:s) mpX[x]=id++,inv[id-1]=x;
    }

    vector<int> sum(mpX.size());
    for(int i=0;i<N;i++){
        sum[mpX[R[i]]]--;
        sum[mpX[L[i]]]++;
    }
    for(int i=1;i<mpX.size();i++){
        sum[i]+=sum[i-1];
    }

    int turn=*max_element(sum.begin(),sum.end());

    if(turn%2==1){
        int pos=inv[sum.rend()-max_element(sum.rbegin(),sum.rend())-1];
        ll epos=1e9;
        for(int i=0;i<N;i++){
            if(L[i]<=pos && pos<R[i]){
                epos=min(epos,R[i]);
            }
        }
        ll res=(turn-1)*M+epos-1;
        cout<<res<<endl;
    }
    else{
        int pos=inv[max_element(sum.begin(),sum.end())-sum.begin()];
        ll epos=0;
        for(int i=0;i<N;i++){
            if(L[i]<=pos && pos<R[i]){
                epos=max(epos,L[i]);
            }
        }
        ll res=(turn-1)*M+M-epos;
        cout<<res<<endl;
    }
    
    return 0;
}
