#include<bits/stdc++.h>
#pragma optimize ("g",on)
#pragma GCC optimize ("inline")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("03")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,avx2,mmx,fma,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
//01001101 01100001 01101011 01101000 01100001 01100111 01100001 01111001
using namespace std;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ll long long
#define pb push_back
#define sz(a) a.size()
#define nl '\n'
#define popb pop_back()
#define ld double
#define ull unsigned long long
#define ff first
#define ss second
#define fix fixed << setprecision
#define pii pair<int, int>
#define E exit (0)
#define int long long
const int inf = 1e15, N = 1e6 + 1, mod = 998244353;
vector<pii> dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int dp[1001][2001];
main() {
//freopen("cowrect.in", "r", stdin);
//freopen("cowrect.out", "w", stdout);
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
int n;
cin >> n;
vector<pii> a, b;
for (int i = 1, x, y; i <= n * 2; i++) {
cin >> x >> y;
if (y <= 1) {
a.pb({x, y});
}else b.pb({x, y});
}
if (sz(a) > n) {
sort(all(a));
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= sz(a); j++) dp[i][j] = inf;
}
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sz(a); j++) {
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j - 1] + abs(a[j - 1].ff - i) + 1 - a[j - 1].ss);
}
}
set<int> pos;
for (int i = 0; i < sz(a); i++) pos.insert(i);
int x = n, y = sz(a);
while (x && y) {
if (dp[x][y] == dp[x][y - 1]) {
y--;
continue;
}
x--, y--;
pos.erase(y);
}
for (auto e: pos) {
b.pb(a[e]);
}
int cur = dp[n][sz(a)];
sort(all(b));
for (int i = 0; i < sz(b); i++) {
cur += abs(i + 1 - b[i].ff) + abs(2 - b[i].ss);
}
cout << cur;
}else {
sort(all(b));
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= sz(b); j++) dp[i][j] = inf;
}
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sz(b); j++) {
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j - 1] + abs(b[j - 1].ff - i) + b[j - 1].ss - 2);
}
}
set<int> pos;
for (int i = 0; i < sz(b); i++) pos.insert(i);
int x = n, y = sz(b);
while (x && y) {
if (dp[x][y] == dp[x][y - 1]) {
y--;
continue;
}
x--, y--;
pos.erase(y);
}
for (auto e: pos) {
a.pb(b[e]);
}
int cur = dp[n][sz(b)];
sort(all(a));
for (int i = 0; i < sz(a); i++) {
cur += abs(i + 1 - a[i].ff) + abs(1 - a[i].ss);
}
cout << cur;
}
}