#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
vector<int> g[maxn];
int deep[maxn], vis[maxn], f[maxn], son[maxn], num[maxn], ans[maxn];
int res, n;
void prework(int u, int fa) {
  son[u] = 0, f[u] = 1;
  for (auto v : g[u]) {
    if (v == fa) continue;
    deep[v] = deep[u] + 1;
    prework(v, u);
    f[u] += f[v];
    if (!son[u] || f[v] > f[son[u]]) son[u] = v;
  }
}
void upd(int u, int fa, int v) {
  num[deep[u]] += v;
  if (v > 0) {
    if (num[deep[u]] > num[res])
      res = deep[u];
    else if (num[deep[u]] == num[res])
      res = min(deep[u], res);
  }
  for (auto it : g[u]) {
    if (vis[it] || it == fa) continue;
    upd(it, u, v);
  }
}
void dfs(int u, int fa, int keep) {
  for (auto v : g[u]) {
    if (v == son[u] || v == fa) continue;
    dfs(v, u, 0);
  }
  int x = 0;
  if (son[u]) dfs(son[u], u, 1), vis[son[u]] = 1;
  upd(u, fa, 1);
  ans[u] = res;
  if (son[u]) vis[son[u]] = 0;
  if (!keep) upd(u, fa, -1), res = 0;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  deep[1] = 0;
  prework(1, 0);
  dfs(1, 0, 0);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i] - deep[i]);
  return 0;
}
