#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
//#define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream& operator<<(ostream& os, pair<T, S> a) {
    os << '(' << a.first << ',' << a.second << ')';
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
    for (auto x : v) os << x << ' ';
    return os;
}
void debug() {
    cerr << '\n';
}
template <typename H, typename... T>
void debug(H a, T... b) {
    cerr << a;
    if (sizeof...(b)) cerr << ", ";
    debug(b...);
}
template<typename T> class segtree {
private:
    int n,sz,h;
    vector<T> node, lazy;
    void eval(int k) {
        if(lazy[k]){
            node[k] += lazy[k];
            if(k < n) {
                lazy[k*2] += lazy[k], lazy[k*2+1] += lazy[k];
            }
            lazy[k] = 0;
        }
    }
    
public:
    segtree(const vector<T>& v) : n(1), sz((int)v.size()), h(0) {
        while(n < sz) n *= 2, h++;
        node.resize(2*n, numeric_limits<T>::min());
        lazy.resize(2*n, 0);
        for(int i = 0; i < sz; i++) node[i+n] = v[i];
        for(int i = n-1; i >= 1; i--) node[i] = max(node[2*i], node[2*i+1]);
    }
    void range(int a, int b, T x, int k=1, int l=0, int r=-1){
        if(r < 0) r = n;
        eval(k);
        if(b <= l || r <= a){
            return;
        }
        if(a <= l && r <= b){
            lazy[k] += x;
            eval(k);
        }else{
            range(a, b, x, 2*k, l, (l+r)/2);
            range(a, b, x, 2*k+1, (l+r)/2, r);
            node[k] = max(node[2*k], node[2*k+1]);
        }
    }
    T query(int a, int b) {
        a += n, b += n - 1;
        for(int i = h; i > 0; i--) eval(a >> i), eval(b >> i);
        b++;
        T res1 = numeric_limits<T>::min(), res2 = numeric_limits<T>::min();
        while(a < b) {
            if(a & 1) eval(a), res1 = max(res1, node[a++]);
            if(b & 1) eval(--b), res2 = max(res2, node[b]);
            a >>= 1, b >>= 1;
        }
        return max(res1, res2);
    }
};
int main(){
    int N, M;
    cin >> N >> M;
    vector<int>za;
    vector<pair<int,int>>a;
    vector<pair<int,int>>b;
    for(int i = 0; i < N; i++){
        int L, R;
        cin >> L >> R;
        za.push_back(L);
        za.push_back(R);
        a.emplace_back(L,R);
        b.emplace_back(R,L);
    }
    sort(a.begin(), a.end());
    sort(b.rbegin(), b.rend());
    sort(za.begin(), za.end());
    za.erase(unique(za.begin(), za.end()),za.end());
    map<int,int>mp;
    for(int i = 0; i < (int)za.size(); i++){
        mp[za[i]] = i;
    }
    vector<int>v(za.size());
    segtree<int>seg(v);
    for(int i = 0; i < N; i++){
        seg.range(mp[a[i].first], mp[a[i].second]+1,1);
    }
    ll num = seg.query(0, (int)za.size());
    ll ans = (num - 1) * M;
    if(num % 2 == 0){
        int ma = -1;
        vector<int> hoge;
        for(auto x : a){
            int l = mp[x.first];
            int r = mp[x.second];
            if(num == seg.query(l,r+1)){
                ma = max(ma, x.first);
                hoge.push_back(x.first);
            }
        }
        sort(hoge.begin(), hoge.end());
        ans += M - hoge[num-1];
    }else{
        int mi = 1<<30;
        vector<int> hoge;

        for(auto x : a){
            int l = mp[x.first];
            int r = mp[x.second];
            if(num == seg.query(l,r+1)){
                mi = min(mi, x.second);
                hoge.push_back(x.second);

            }
        }
        sort(hoge.rbegin(), hoge.rend());

        ans += hoge[num-1];
    }
    cout << ans << endl;
}



Measured memory_bytes: 36912