#include <bits/stdc++.h>
using namespace std;
const int M = 1e6 + 5;
int Head[M], To[M * 2], Nxt[M * 2], TOT, mxdep[M], son[M], fa[M], dep[M];
void ADD(int x, int y) {
  Nxt[++TOT] = Head[x];
  Head[x] = TOT;
  To[TOT] = y;
}
void dfs_begin(int x) {
  dep[x] = dep[fa[x]] + 1;
  for (int i = Head[x]; i; i = Nxt[i]) {
    int y = To[i];
    if (y == fa[x]) continue;
    fa[y] = x;
    dfs_begin(y);
    if (mxdep[son[x]] < mxdep[y]) son[x] = y;
  }
  mxdep[x] = mxdep[son[x]] + 1;
}
int tmp[M], *f[M], *idtmp = tmp, ans[M];
void dfs_main(int x) {
  if (son[x]) {
    f[son[x]] = f[x] + 1;
    dfs_main(son[x]);
    ans[x] = ans[son[x]] + 1;
  }
  f[x][0] = 1;
  if (f[x][ans[x]] == 1) ans[x] = 0;
  for (int i = Head[x]; i; i = Nxt[i]) {
    int y = To[i];
    if (y == fa[x] || y == son[x]) continue;
    f[y] = idtmp, idtmp += mxdep[y];
    dfs_main(y);
    for (int i = 0; i < mxdep[y]; i++) {
      f[x][i + 1] += f[y][i];
      if (f[x][i + 1] > f[x][ans[x]] ||
          (f[x][i + 1] == f[x][ans[x]] && i + 1 < ans[x]))
        ans[x] = i + 1;
    }
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1, nx, ny; i < n; i++)
    scanf("%d", &nx), scanf("%d", &ny), ADD(nx, ny), ADD(ny, nx);
  dfs_begin(1);
  f[1] = idtmp, idtmp += mxdep[1];
  dfs_main(1);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
  return 0;
}
