#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000005;
int n, dep[MAXN], h[MAXN], son[MAXN], ans[MAXN];
vector<int> G[MAXN], f[MAXN];
inline void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    (x *= 10) += ch - '0';
    ch = getchar();
  }
  return x * f;
}
void prep(int u, int c_fa) {
  dep[u] = dep[c_fa] + 1;
  for (int v : G[u])
    if (v != c_fa) {
      prep(v, u);
      if (h[son[u]] <= h[v]) son[u] = v, h[u] = h[v] + 1;
    }
}
void dfs(int u, int c_fa) {
  if (son[u]) {
    dfs(son[u], u);
    swap(f[u], f[son[u]]);
    f[u].push_back(1);
    ans[u] = ans[son[u]] + 1;
    if (f[u][h[u] - ans[u]] == 1) ans[u] = 0;
    for (int v : G[u])
      if (v != c_fa && v != son[u]) {
        dfs(v, u);
        for (int i = 0; i <= h[v]; i++) {
          f[u][h[u] - i - 1] += f[v][h[v] - i];
          if (f[u][h[u] - i - 1] > f[u][h[u] - ans[u]] ||
              (f[u][h[u] - i - 1] == f[u][h[u] - ans[u]] && i + 1 < ans[u]))
            ans[u] = i + 1;
        }
      }
  } else
    f[u].push_back(1);
}
int main() {
  n = read();
  for (int i = 1; i < n; i++) add_edge(read(), read());
  prep(1, 0);
  dfs(1, 0);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
  return 0;
}
