#include <bits/stdc++.h>
using namespace std;
int read() {
  register int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = 10 * x + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void write(int x) {
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
const int Size = 1000005;
const int INF = 0x3f3f3f3f;
int n, cnt, head[Size];
struct Edge {
  int v, next;
} w[Size << 1];
void AddEdge(int u, int v) {
  w[++cnt].v = v;
  w[cnt].next = head[u];
  head[u] = cnt;
}
int len[Size], son[Size];
void dfs(int x, int fa) {
  for (int i = head[x]; i; i = w[i].next) {
    int nxt = w[i].v;
    if (nxt != fa) {
      dfs(nxt, x);
      if (len[nxt] > len[son[x]]) {
        son[x] = nxt;
      }
    }
  }
  len[x] = len[son[x]] + 1;
}
int k[Size];
int tmp[Size * 10], *id = tmp, *f[Size];
void dp(int x, int fa) {
  f[x][0] = 1;
  if (son[x]) {
    f[son[x]] = f[x] + 1;
    dp(son[x], x);
    k[x] = k[son[x]] + 1;
  }
  for (int i = head[x]; i; i = w[i].next) {
    int nxt = w[i].v;
    if (nxt != fa && nxt != son[x]) {
      f[nxt] = id;
      id += len[nxt];
      dp(nxt, x);
      for (register int j = 1; j <= len[nxt]; j++) {
        f[x][j] += f[nxt][j - 1];
        if (f[x][j] > f[x][k[x]] || (f[x][j] == f[x][k[x]] && j < k[x])) {
          k[x] = j;
        }
      }
    }
  }
  if (f[x][k[x]] == 1) k[x] = 0;
}
int main() {
  n = read();
  for (register int i = 1; i < n; i++) {
    int u = read();
    int v = read();
    AddEdge(u, v);
    AddEdge(v, u);
  }
  dfs(1, 0);
  f[1] = id;
  id += len[1];
  dp(1, 0);
  for (register int i = 1; i <= n; i++) {
    write(k[i]);
    putchar(10);
  }
  return 0;
}
