#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int a = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = -1;
  for (; isdigit(ch); ch = getchar()) a = (a << 3) + (a << 1) + ch - '0';
  return a * f;
}
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
struct edge {
  int to, next;
} e[N << 2];
int head[N], tot, n;
inline void add(int x, int y) {
  e[++tot].to = y;
  e[tot].next = head[x];
  head[x] = tot;
}
struct node {
  int col, son, siz;
} p[N];
int ans[N], cnt[N], maxn, sum;
void prework(int u, int fa) {
  p[u].siz = 1;
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (v == fa) continue;
    p[v].col = p[u].col + 1;
    prework(v, u);
    p[u].siz += p[v].siz;
    if (p[v].siz > p[p[u].son].siz) p[u].son = v;
  }
  return;
}
void calc(int u, int fa, int val, int flag) {
  cnt[p[u].col] += val;
  if (cnt[p[u].col] > maxn || (cnt[p[u].col] == maxn && p[u].col - sum < 0)) {
    sum = p[u].col;
    maxn = cnt[p[u].col];
  }
  for (int i = head[u]; i; i = e[i].next)
    if (e[i].to != fa && e[i].to != flag) calc(e[i].to, u, val, flag);
}
void dfs(int u, int fa, int opt) {
  for (int i = head[u]; i; i = e[i].next)
    if (e[i].to != fa && e[i].to != p[u].son) dfs(e[i].to, u, 0);
  if (p[u].son) dfs(p[u].son, u, 1);
  calc(u, fa, 1, p[u].son);
  ans[u] = sum;
  if (!opt) calc(u, fa, -1, u), maxn = sum = 0;
}
signed main() {
  n = read();
  for (int i = 1; i < n; i++) {
    int x = read(), y = read();
    add(x, y);
    add(y, x);
  }
  prework(1, 0);
  dfs(1, 0, 1);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i] - p[i].col);
  return 0;
}
