#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lll = __int128_t;
ll n, a, b, ans;
lll x, md;
vector<pair<lll, lll>> v;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> a >> b;
x = a/__gcd(a, b+1);
md = x * (lll)b / __gcd(x, (lll)b);
cout << (ll) md;
while (n--) {
cin >> a >> b;
if (b - a + 1 >= md) v.push_back({0, md-1});
else {
a %= md;
b %= md;
if (a < b) v.push_back({a, b});
else v.push_back({b, md-1}), v.push_back({0, a});
}
}
sort(v.begin(), v.end(), [](const pair<lll, lll> &l, const pair<lll, lll> &r) { return l.second < r.second; } );
lll last = 0;
for (auto [l, r]: v) {
l = max(l, last+1);
if (r >= l) ans += r-l+1;
last = max(r, last);
}
cout << ans << '\n';
}