#include <bits/stdc++.h>
using namespace std;
int n, child[1000005], ans[1000005], cnt[1000005], T[1000005];
vector<int> g[1000005], vec[1000005];
void DFS(int u, int pre) {
  child[u] = 1;
  for (int i = 0; i < g[u].size(); ++i) {
    int v = g[u][i];
    if (v != pre) {
      T[v] = T[u] + 1;
      DFS(v, u);
      child[u] += child[v];
    }
  }
}
void dfs(int u, int pre, bool df) {
  int mx = 0, bigchild = -1;
  for (int i = 0; i < g[u].size(); ++i) {
    int v = g[u][i];
    if (v != pre && mx < child[v]) {
      mx = child[v];
      bigchild = v;
    }
  }
  for (int i = 0; i < g[u].size(); ++i) {
    int v = g[u][i];
    if (v != pre && v != bigchild) {
      dfs(v, u, 0);
    }
  }
  if (bigchild != -1) {
    dfs(bigchild, u, 1);
    vec[u].swap(vec[bigchild]);
    ans[u] = ans[bigchild] + 1;
  }
  vec[u].push_back(u);
  cnt[T[u]]++;
  if (cnt[T[u]] == cnt[T[u] + ans[u]]) ans[u] = 0;
  for (int i = 0; i < g[u].size(); ++i) {
    int v = g[u][i];
    if (v != pre && v != bigchild) {
      for (int j = 0; j < vec[v].size(); ++j) {
        int t = vec[v][j];
        cnt[T[t]]++;
        if (cnt[T[t]] > cnt[T[u] + ans[u]])
          ans[u] = T[t] - T[u];
        else if (cnt[T[t]] == cnt[T[u] + ans[u]]) {
          if (T[t] < T[u] + ans[u]) ans[u] = T[t] - T[u];
        }
        vec[u].push_back(t);
      }
    }
  }
  if (df == 0) {
    for (int i = 0; i < vec[u].size(); ++i) {
      int v = vec[u][i];
      cnt[T[v]]--;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  DFS(1, 0);
  dfs(1, 0, 1);
  for (int i = 1; i <= n; ++i) cout << ans[i] << '\n';
  return 0;
}
