#include <bits/stdc++.h>
using namespace std;
int n, m;
int f[11450][11450];
int y, y2;
int main() {
cin >> n;
y = y2 = 1;
f[0][0] = f[0][1] = 0;
for (int i = 1; i <= n; i++) {
int l, r;
cin >> l >> r;
f[i][0] = min(f[i - 1][0] + abs(r - y), f[i - 1][1] + abs(r - y2) + r - l);
f[i][1] = min(f[i - 1][0] + abs(l - y), f[i - 1][1] + abs(l - y2) + r - l);
y = l;
y2 = r;
}
f[n][0] += n - y;
f[n][1] += n - y2;
cout << min(f[n][0], f[n][1]) + n - 1;
return 0;
}
14分求调