#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef unsigned long long ui64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;

#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for(i=(s); i < (e); ++i)
inline void RI(i64 &i) {scanf("%lld", &(i));}
inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } }
inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } }
inline void WI(const i64 &i) {printf("%lld\n", i);}
inline void WVI(const vi &v, char sep=' ') { for(i64 i=0;i<sz(v);++i) { if(i != 0){ printf("%c", sep); } printf("%lld", v[i]);} printf("\n"); }
inline void WS(const string &s) { printf("%s\n", s.c_str()); }
inline void WB(bool b, const string &yes, const string &no) { if(b){ WS(yes);} else { WS(no);} }
inline void YESNO(bool b) { WB(b, "YES", "NO"); }
inline void YesNo(bool b) { WB(b, "Yes", "No"); }
#define BUF_LENGTH 1000000
inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;}
template<typename T> inline bool IN(T &S, const typename T::key_type &key) {
  return S.find(key) != S.end();
}
template<typename T> inline bool ON(const T &b, i64 idx) {
  return ((T(1) << idx) & b) != 0;
}

i64 dfs(const vvi &edges, i64 cur, i64 prev, i64 n, const vi &to, vi &ans, bool f) {
  // start from v
  //cerr << "dfs " << cur << " " << prev << " " << n << endl;
  ans[cur] = n;
  for(auto &e: edges[cur]) {
    if(e != prev && e != to[cur]) {
      n += 1;
      n = dfs(edges, e, cur, n, to, ans, false);
      n += 1;
    }
  }
  if(f && to[cur] != -1) {
    n += 1;
    n = dfs(edges, to[cur], cur, n, to, ans, true);
  }
  return n;
}

int main(int argc, char *argv[]) {
  i64 i, j, k;  
  i64 t, T;
  i64 N; cin >> N;
  vvi edges(N);
  REP(0, N-1, i) {
    i64 a, b; RI(a); RI(b);
    --a; --b;
    edges[a].pb(b);
    edges[b].pb(a);
  }

  const i64 INF = N * 2;

  auto bfs = [&](i64 s) {
    vi dists(N, INF);
    vi from(N, -1);
    dists[s] = 0;
    queue<i64> q;
    q.push(s);
    while(!q.empty()) {
      i64 cur = q.front(); q.pop();
      for(auto &e: edges[cur]) {
        if(dists[e] == INF) {
          dists[e] = dists[cur] + 1;
          from[e] = cur;
          q.push(e);
        }
      }
    }
    /*
    for(auto &d: dists) {
      cerr << d << " ";
    }
    cerr << endl;
    for(auto &d: from) {
      cerr << d << " ";
    }
    cerr << endl;
    */
    return pair<vi, vi> {dists, from};
  };

  auto res_0 = bfs(0);
  vi &d0 = res_0.first;
  i64 u = -1;
  REP(0, N, i) {
    if(u == -1 || d0[u] < d0[i]) {
      u = i;
    }
  }
  assert(u != -1);
  auto res_u = bfs(u);
  vi &du = res_u.first;
  vi &fu = res_u.second;
  i64 v = -1;
  REP(0, N, i) {
    if(v == -1 || du[v] < du[i]) {
      v = i;
    }
  }
  assert(v != -1);
  //cerr << "diameter " << du[v] << " " << u << " -> " << v << endl;

  vi ans(N, -1);
  dfs(edges, v, -1, 1, fu, ans, true);
  WVI(ans);

  return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

class ETraveler {
public:
    void solve(std::istream& cin, std::ostream& cout) {
        int N;
        cin >> N;
        vector<pair<int, int>> v(N);
        REP(i, N) {
            int x, c;
            cin >> x >> c;
            v[i] = {c, x};
        }
        sort(v.begin(), v.end());
        vector<pair<int, int>> LR;
        int prev = v[0].first;
        int l = v[0].second;
        int r = v[0].second;
        REP(i, v.size()) {
            if (v[i].first != prev) {
                LR.emplace_back(l, r);
                l = r = v[i].second;
            } else {
                l = min(l, v[i].second);
                r = max(r, v[i].second);
            }
            prev = v[i].first;
        }
        LR.emplace_back(l, r);
        LR.emplace_back(0, 0);
        vector<vector<int>> dp(LR.size() + 1, vector<int>(2, INF));
        dp[0][0] = dp[0][1] = 0;
        REP(i, LR.size()) {
            int L = LR[i].first;
            int R = LR[i].second;
            int PL = i > 0 ? LR[i - 1].first : 0;
            int PR = i > 0 ? LR[i - 1].second : 0;
            dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + abs(PL - R) + R - L);
            dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + abs(PR - R) + R - L);
            dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + abs(PL - L) + R - L);
            dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + abs(PR - L) + R - L);
        }
        cout << min(dp[LR.size()][0], dp[LR.size()][1]) << endl;
    }
};


signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    std::istream& in(std::cin);
    std::ostream& out(std::cout);

    ETraveler solver;
    solver.solve(in, out);
    return 0;
}
