#include <bits/stdc++.h>
using namespace std;
const int max_n = 1000005;
int n;
vector<int> G[max_n];
int len[max_n], son[max_n];
void dfs1(int u, int fa) {
  for (auto v : G[u]) {
    if (v == fa) continue;
    dfs1(v, u);
    if (len[v] > len[son[u]]) son[u] = v;
  }
  len[u] = len[son[u]] + 1;
}
int arr[max_n], *dp[max_n], *now = arr;
int ans[max_n];
void dfs(int u, int fa) {
  dp[u][0] = 1;
  if (son[u]) {
    dp[son[u]] = dp[u] + 1;
    dfs(son[u], u);
    if (dp[u][ans[son[u]] + 1] > 1) ans[u] = ans[son[u]] + 1;
  }
  for (auto v : G[u]) {
    if (v == fa || v == son[u]) continue;
    dp[v] = now, now += len[v];
    dfs(v, u);
    for (int i = 0; i < len[v]; i++) {
      dp[u][i + 1] += dp[v][i];
      if (dp[u][i + 1] > dp[u][ans[u]] ||
          dp[u][i + 1] == dp[u][ans[u]] && i + 1 < ans[u])
        ans[u] = i + 1;
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v), G[v].push_back(u);
  }
  dfs1(1, 0);
  dp[1] = now, now += len[1];
  dfs(1, 0);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
  return 0;
}
