#include <bits/stdc++.h>
const int M = 1e6 + 10;
int n;
template <typename T>
inline void read(T &x) {
  x = 0;
  char ch = getchar();
  int f = 1;
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
  x *= f;
}
int head[M], tot = 0;
struct edge {
  int to, next;
} e[M << 1];
inline void add(int u, int v) {
  e[++tot] = (edge){v, head[u]};
  head[u] = tot;
  e[++tot] = (edge){u, head[v]};
  head[v] = tot;
}
int dep[M], son[M];
void dfs1(int pos, int fa) {
  for (int i = head[pos], v; i; i = e[i].next)
    if ((v = e[i].to) != fa) {
      dfs1(v, pos);
      if (dep[son[pos]] < dep[v]) son[pos] = v;
    }
  dep[pos] = dep[son[pos]] + 1;
}
int spac[M], *now = spac;
int *dp[M];
int ans[M];
void dfs2(int pos, int fa) {
  dp[pos][0] = 1;
  if (son[pos]) {
    dp[son[pos]] = dp[pos] + 1;
    dfs2(son[pos], pos);
    ans[pos] = ans[son[pos]] + 1;
  }
  for (int i = head[pos], v; i; i = e[i].next)
    if ((v = e[i].to) != fa && v != son[pos]) {
      dp[v] = now;
      now += dep[v];
      dfs2(v, pos);
      for (int i = 1; i <= dep[v]; i++) {
        dp[pos][i] += dp[v][i - 1];
        if (dp[pos][i] > dp[pos][ans[pos]] ||
            (dp[pos][i] == dp[pos][ans[pos]] && i < ans[pos]))
          ans[pos] = i;
      }
    }
  if (dp[pos][ans[pos]] == 1) ans[pos] = 0;
}
int main() {
  read(n);
  for (int i = 1, u, v; i < n; i++) read(u), read(v), add(u, v);
  dfs1(1, 0);
  dp[1] = now;
  now += dep[1];
  dfs2(1, 0);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
  return 0;
}
