#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int to;
  int nex;
} e[1000005 << 1];
int head[1000005], tot;
void add(int u, int v) {
  e[tot].to = v;
  e[tot].nex = head[u];
  head[u] = tot++;
}
int tmp[1000005];
int len[1000005], son[1000005], *f[1000005], ans[1000005], *id = tmp;
void dfs(int x, int fa) {
  for (int i = head[x]; i != -1; i = e[i].nex) {
    int y = e[i].to;
    if (y == fa) continue;
    dfs(y, x);
    if (len[y] > len[son[x]]) son[x] = y;
  }
  len[x] = len[son[x]] + 1;
}
void dp(int x, int fa) {
  f[x][0] = 1;
  if (son[x]) {
    f[son[x]] = f[x] + 1;
    dp(son[x], x);
    ans[x] = ans[son[x]] + 1;
  }
  for (int i = head[x]; i != -1; i = e[i].nex) {
    int y = e[i].to;
    if (y == son[x] || y == fa) continue;
    f[y] = id;
    id += len[y];
    dp(y, x);
    for (int j = 1; j <= len[y]; j++) {
      f[x][j] += f[y][j - 1];
      if (f[x][j] > f[x][ans[x]] || (j < ans[x] && f[x][j] == f[x][ans[x]]))
        ans[x] = j;
    }
  }
  if (f[x][ans[x]] == 1) ans[x] = 0;
}
int main() {
  memset(head, -1, sizeof head);
  int n, x, y;
  cin >> n;
  for (int i = 1; i <= n - 1; i++) {
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  dfs(1, 0);
  f[1] = id;
  id += len[1];
  dp(1, 0);
  for (int i = 1; i <= n; i++) {
    printf("%d\n", ans[i]);
  }
}
