#include <bits/stdc++.h>

using namespace std;

using i64 = long;

const i64 MOD = 1e9 + 7;
const i64 INF = 1e18 + 7;

signed main(){
    i64 n, m;
    cin >> n >> m;
    vector<i64> l(n), r(n);
    vector<i64> comp{0, 1, m - 1, m};
    for(i64 i = 0; i < n; ++i){
        cin >> l[i] >> r[i];
        // half open
        ++r[i];
        comp.emplace_back(l[i]);
        comp.emplace_back(l[i] + 1);
        comp.emplace_back(r[i] - 1);
        comp.emplace_back(r[i]);
    }
    sort(comp.begin(), comp.end());
    comp.erase(unique(comp.begin(), comp.end()), comp.end());
    vector<i64> l_comp(n), r_comp(n);
    i64 k = comp.size();
    vector<i64> rui(k + 1, 0);
    for(i64 i = 0; i < n; ++i){
        l_comp[i] = distance(comp.begin(), lower_bound(comp.begin(), comp.end(), l[i]));
        r_comp[i] = distance(comp.begin(), lower_bound(comp.begin(), comp.end(), r[i]));

        ++rui[l_comp[i]];
        --rui[r_comp[i]];
    }
    for(i64 i = 0; i < k; ++i)
        rui[i + 1] += rui[i];
    i64 cnt = *max_element(rui.begin(), rui.end());

    bool is_back = (cnt % 2 == 0);
    if(is_back){
        i64 idx = 0;
        for(i64 i = 0; i <= k; ++i)
            if(rui[i] == cnt){
                idx = i;
                break;
            }
        i64 dif = 0;
        for(i64 i = 0; i < n; ++i){
            if(l_comp[i] <= idx && idx < r_comp[i]){
                dif = max(dif, l_comp[i]);
            }
        }
        cout << (cnt * m) - comp[dif] << endl;
    }else{
        i64 idx = 0;
        for(i64 i = 0; i <= k; ++i)
            if(rui[i] == cnt){
                idx = i;
            }
        i64 dif = INF;
        for(i64 i = 0; i < n; ++i){
            if(l_comp[i] <= idx && idx < r_comp[i]){
                dif = min(dif, r_comp[i]);
            }
        }
        cout << ((cnt - 1) * m) + comp[dif - 1] << endl;
    }
}

