import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

immutable long MOD = 10^^9 + 7;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1].to!long;
    auto A = N.iota.map!(_ => readln.split.map!(to!int).array).array;

    int[] B = [0, M.to!int];
    foreach (a; A) B ~= a;
    B = B.sort().uniq.array;
    auto X = B.length.to!int;
    int[int] comp;
    foreach (i; 0..X) comp[B[i]] = i;

    auto imos = new int[](X + 1);
    foreach (a; A) {
        imos[comp[a[0]]] += 1;
        imos[comp[a[1]]+1] -= 1;
    }
    foreach (i; 0..X) imos[i+1] += imos[i];

    auto st = new SegmentTree!(int, max, 0)(X+1);
    foreach (i; 0..X+1) st.assign(i, imos[i]);

    auto maxL = new int[](X+1);
    auto maxR = new int[](X+1);
    maxL[0] = imos[0];
    foreach (i; 0..X) maxL[i+1] = max(maxL[i], imos[i+1]);
    maxR[X] = imos[X];
    foreach_reverse (i; 0..X) maxR[i] = max(maxR[i+1], imos[i]);

    long ans = 1L << 59;

    foreach (i; 0..N) {
        auto l = comp[A[i][0]];
        auto r = comp[A[i][1]];
        auto left = maxL[l-1];
        auto mid = st.query(l, r) - 1;
        auto right = maxR[r+1];
        auto v = max(left, mid, right);
        long tmp = v * M;
        if (v % 2 == 0) {
            tmp += A[i][1];
        } else {
            tmp += M - A[i][0];
        }
        //writeln([i, A[i][0], A[i][1], left, mid, right, tmp]);
        ans = min(ans, tmp);
    }

    ans.writeln;
}

class SegmentTree(T, alias op, T e) {
    T[] table;
    int size;
    int offset;

    this(int n) {
        size = 1;
        while (size <= n) size <<= 1;
        size <<= 1;
        table = new T[](size);
        fill(table, e);
        offset = size / 2;
    }

    void assign(int pos, T val) {
        pos += offset;
        table[pos] = val;
        while (pos > 1) {
            pos /= 2;
            table[pos] = op(table[pos*2], table[pos*2+1]);
        }
    }

    void add(int pos, T val) {
        pos += offset;
        table[pos] += val;
        while (pos > 1) {
            pos /= 2;
            table[pos] = op(table[pos*2], table[pos*2+1]);
        }
    }

    T query(int l, int r) {
        return query(l, r, 1, 0, offset-1);
    }

    T query(int l, int r, int i, int a, int b) {
        if (b < l || r < a) {
            return e;
        } else if (l <= a && b <= r) {
            return table[i];
        } else {
            return op(query(l, r, i*2, a, (a+b)/2), query(l, r, i*2+1, (a+b)/2+1, b));
        }
    }
}

