#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = (int)(b) - 1; i >= (a); --i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vii = vector<pii>;
using vb = vector<bool>;
using vl = vector<ll>;
int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n;
  cin >> n;
  ll ans = 0;
  vector<vi> a(2, vi(n, -1));
  rep(i, 0, 2 * n) {
    ll x, y;
    cin >> x >> y;
    if (x < 1) ans += 1 - x, x = 1;
    if (x > n) ans += x - n, x = n;
    if (y < 1) ans += 1 - y, y = 1;
    if (y > 2) ans += y - 2, y = 2;
    x--, y--, a[y][x]++;
  }
  int x = 0, y = 0;
  rep(i, 0, n) {
    x += a[0][i], y += a[1][i];
    if (x > 0 && y < 0) {
      int delta = min(x, -y);
      ans += delta, x -= delta, y += delta;
    } else if (x < 0 && y > 0) {
      int delta = min(-x, y);
      ans += delta, x += delta, y -= delta;
    }
    ans += abs(x) + abs(y);
  }
  cout << ans << '\n';
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |