#include <bits/stdc++.h>
using namespace std;
int fa[1000010], to[2000010], la[2000010], cnt;
int len[1000010], son[1000010], tmp[1000010], *f[1000010], ans[1000010],
    *id = tmp;
inline void add(const register int x, const register int y) {
  to[++cnt] = y, la[cnt] = fa[x], fa[x] = cnt;
  to[++cnt] = x, la[cnt] = fa[y], fa[y] = cnt;
}
void pre(const register int x, const register int ff) {
  for (int i = fa[x]; i; i = la[i])
    if (to[i] != ff) {
      pre(to[i], x);
      if (len[to[i]] > len[son[x]]) son[x] = to[i];
    }
  len[x] = len[son[x]] + 1;
}
void dfs(const register int x, const register int ff) {
  f[x][0] = 1;
  if (son[x]) f[son[x]] = f[x] + 1, dfs(son[x], x), ans[x] = ans[son[x]] + 1;
  for (int i = fa[x]; i; i = la[i])
    if (to[i] != ff && to[i] != son[x]) {
      f[to[i]] = id;
      id += len[to[i]];
      dfs(to[i], x);
      for (int j = 1; j <= len[to[i]]; j++) {
        f[x][j] += f[to[i]][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;
}
inline int read(register int a = 0, register char ch = getchar()) {
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9')
    a = (a << 3) + (a << 1) + (ch ^ 48), ch = getchar();
  return a;
}
int main() {
  const register int n = read();
  for (int i = 1; i < n; i++) add(read(), read());
  pre(1, 0);
  f[1] = id, id += len[1];
  dfs(1, 0);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
}
