#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 6;
int siz[maxn];
int son[maxn];
int skip[maxn];
int n;
int dep[maxn];
int head[maxn];
int tot;
long long ans[maxn];
long long cnt[maxn];
struct node {
  int u, v;
  int net;
} E[maxn * 2];
void init() {
  memset(head, -1, sizeof(head));
  tot = 0;
}
void build(int u, int v) {
  E[tot].u = u;
  E[tot].v = v;
  E[tot].net = head[u];
  head[u] = tot++;
}
void dfs0(int u, int fa, int deep) {
  siz[u] = 1;
  dep[u] = deep;
  for (int i = head[u]; ~i; i = E[i].net) {
    int v = E[i].v;
    if (v == fa) {
      continue;
    }
    dfs0(v, u, deep + 1);
    siz[u] += siz[v];
    if (siz[son[u]] < siz[v]) {
      son[u] = v;
    }
  }
}
long long mx = 0;
long long sum = 0;
void getans(int u, int fa, int vv, int uu) {
  cnt[dep[u]] += vv;
  if (vv > 0 && cnt[dep[u]] > mx) {
    mx = cnt[dep[u]];
    sum = dep[u];
  } else if (vv > 0 && cnt[dep[u]] == mx) {
    sum = min(sum, (long long)dep[u]);
  }
  for (int i = head[u]; ~i; i = E[i].net) {
    if (E[i].v == fa || skip[E[i].v]) continue;
    getans(E[i].v, u, vv, uu);
  }
}
void dfs(int u, int fa, int op) {
  for (int i = head[u]; ~i; i = E[i].net) {
    int v = E[i].v;
    if (v == fa || v == son[u]) continue;
    dfs(v, u, 0);
  }
  if (son[u]) {
    dfs(son[u], u, 1);
    skip[son[u]] = 1;
  }
  getans(u, fa, 1, u);
  ans[u] = sum - dep[u];
  if (son[u]) {
    skip[son[u]] = 0;
  }
  if (op == 0) {
    getans(u, fa, -1, u);
    sum = mx = 0;
  }
}
int main() {
  init();
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    build(u, v);
    build(v, u);
  }
  dfs0(1, 0, 1);
  dfs(1, 0, 1);
  for (int i = 1; i <= n; i++) {
    printf("%lld\n", ans[i]);
  }
}
