#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int INF = 1000000001;
int n, len[1000005], cnt[1000005], depth[1000005], tin[1000005], tout[1000005],
    big[1000005], order[1000005], dfsTime = 0;
vector<int> e[1000005];
set<pair<int, int> > s;
int result[1000005];
void dfsCalc(int v, int p);
void dfs(int v, int p);
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    e[x].push_back(y);
    e[y].push_back(x);
  }
  dfsCalc(1, -1);
  dfs(1, -1);
  for (int i = 1; i <= n; i++) cout << result[i] << "\n";
  cout << "\n"
       << "\n";
  return 0;
}
void dfsCalc(int v, int p) {
  tin[v] = dfsTime++;
  order[tin[v]] = v;
  len[v] = 1;
  big[v] = -1;
  for (auto it : e[v]) {
    if (it != p) {
      depth[it] = depth[v] + 1;
      dfsCalc(it, v);
      len[v] += len[it];
      if ((big[v] == -1) || (len[big[v]] < len[it])) big[v] = it;
    }
  }
  tout[v] = dfsTime - 1;
}
void dfs(int v, int p) {
  for (auto it : e[v]) {
    if ((it != p) && (it != big[v])) dfs(it, v);
  }
  if (big[v] != -1) dfs(big[v], v);
  for (auto it : e[v]) {
    if ((it != p) && (it != big[v])) {
      for (int t = tin[it]; t <= tout[it]; t++) {
        s.erase({-cnt[depth[order[t]]], depth[order[t]]});
        cnt[depth[order[t]]]++;
        s.insert({-cnt[depth[order[t]]], depth[order[t]]});
      }
    }
  }
  s.erase({-cnt[depth[v]], depth[v]});
  cnt[depth[v]]++;
  s.insert({-cnt[depth[v]], depth[v]});
  result[v] = s.begin()->second - depth[v];
  if ((p != -1) && (v != big[p])) {
    for (int t = tin[v]; t <= tout[v]; t++) cnt[depth[order[t]]] = 0;
    s.clear();
  }
}
