#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
int n, u, v, cnt, head[maxn], to[maxn << 1], nex[maxn << 1], deep[maxn],
    len[maxn], son[maxn], *f[maxn], tmp[maxn], *id = tmp, ans[maxn];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return x * f;
}
inline void add(int u, int v) {
  to[++cnt] = v;
  nex[cnt] = head[u];
  head[u] = cnt;
}
void dfs1(int x, int pre, int deep) {
  int maxson = 0;
  ::deep[x] = deep;
  for (int i = head[x]; i; i = nex[i]) {
    if (to[i] == pre) continue;
    dfs1(to[i], x, deep + 1);
    if (len[to[i]] > maxson) son[x] = to[i], maxson = len[to[i]];
  }
  len[x] = maxson + 1;
}
void dfs2(int x, int pre) {
  f[x][0] = 1;
  if (son[x]) {
    f[son[x]] = f[x] + 1;
    dfs2(son[x], x);
    ans[x] = ans[son[x]] + 1;
  }
  for (int i = head[x]; i; i = nex[i]) {
    int v = to[i];
    if (v == pre || v == son[x]) continue;
    f[v] = id, id += len[v], dfs2(v, x);
    for (int j = 1; j <= len[v]; ++j) {
      f[x][j] += f[v][j - 1];
      if (f[x][j] > f[x][ans[x]] || (f[x][j] == f[x][ans[x]] && j < ans[x]))
        ans[x] = j;
    }
  }
  if (f[x][ans[x]] == 1) ans[x] = 0;
}
int main() {
  n = read();
  for (int i = 1; i < n; ++i) {
    u = read(), v = read();
    add(u, v);
    add(v, u);
  }
  dfs1(1, 0, 1);
  f[1] = id, id += len[1], dfs2(1, 0);
  for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]);
}
