#include <bits/stdc++.h>
std::vector<int> v[1000010];
int n, pl[1000010], *pt = pl, dep[1000010], son[1000010], *f[1000010],
                    ans[1000010];
void dfs(int x, int fa) {
  for (int u : v[x])
    if (u != fa) {
      dfs(u, x);
      if (dep[u] > dep[son[x]]) son[x] = u;
    }
  dep[x] = dep[son[x]] + 1;
}
void dfs2(int x, int fa) {
  f[x][0] = 1;
  if (son[x]) {
    f[son[x]] = f[x] + 1;
    dfs2(son[x], x);
    if (f[son[x]][ans[son[x]]] > 1) ans[x] = ans[son[x]] + 1;
  } else
    return;
  for (int u : v[x])
    if (u != fa && u != son[x]) {
      f[u] = pt, pt += dep[u];
      dfs2(u, x);
      for (int i = 0; i < dep[u]; i++) {
        f[x][i + 1] += f[u][i];
        if (std::make_pair(f[x][i + 1], -i - 1) >
            std::make_pair(f[x][ans[x]], -ans[x]))
          ans[x] = i + 1;
      }
    }
}
int main() {
  scanf("%d", &n);
  for (int i = 1, x, y; i < n; i++)
    scanf("%d%d", &x, &y), v[x].push_back(y), v[y].push_back(x);
  dfs(1, 0);
  f[1] = pt, pt += dep[1];
  dfs2(1, 0);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
  return 0;
}
