#include <bits/stdc++.h>
using namespace std;
using ld = long double;
using ll = long long;
const ll INF = 3e18, MOD = 1e9 + 7;
void solve();
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int q = 1;
while (q--) {
solve();
}
}
void solve() {
ll n; cin >> n;
vector<pair<ll, ll>> a(2 * n + 1);
ll ans = 0;
for (int i = 1; i <= 2 * n; i++) {
cin >> a[i].first >> a[i].second;
if (abs(1 - a[i].second) < abs(2 - a[i].second)) {
ans += abs(1 - a[i].second);
a[i].second = 1;
} else {
ans += abs(2 - a[i].second);
a[i].second = 2;
}
}
sort(a.begin() + 1, a.end());
vector<ll> dp(n + 1, INF);
dp[0] = 0;
for (int i = 1; i <= 2 * n; i++) {
for (int j = min(ll(i), n); j >= 0; j--) {
dp[j] = dp[j] + abs(2 - a[i].second) + abs(i - j - a[i].first);
if (j != 0) {
dp[j] = min(dp[j], dp[j - 1] + abs(1 - a[i].second) + abs(j - a[i].first));
}
}
}
cout << dp[n] + ans;
}