#include <bits/stdc++.h>
using namespace std;
static const int N = (int)1e6 + 10;
int n, sz[N], in[N], out[N], cnt[N], t = 1, dep[N], ver[N], ans[N];
pair<int, int> mx[N];
vector<int> adj[N];
void pre(int v, int lv, int pa) {
  sz[v] = 1;
  in[v] = t;
  ver[t++] = v;
  dep[v] = lv;
  for (auto u : adj[v]) {
    if (u == pa) continue;
    pre(u, lv + 1, v);
    sz[v] += sz[u];
  }
  out[v] = t;
}
void dfs(int v, int pa, bool keep) {
  int big = -1, child = -1;
  for (auto u : adj[v]) {
    if (u == pa) continue;
    if (sz[u] > big) {
      big = sz[u];
      child = u;
    }
  }
  for (auto u : adj[v]) {
    if (u == pa) continue;
    if (u != child) dfs(u, v, 0);
  }
  if (child != -1) {
    dfs(child, v, 1);
    mx[v] = mx[child];
  }
  cnt[dep[v]]++;
  if (cnt[dep[v]] >= mx[v].first) mx[v] = {cnt[dep[v]], dep[v]};
  for (auto u : adj[v]) {
    if (u == pa) continue;
    if (u != child) {
      for (int p = in[u]; p < out[u]; p++) {
        cnt[dep[ver[p]]]++;
        if (cnt[dep[ver[p]]] > mx[v].first)
          mx[v] = {cnt[dep[ver[p]]], dep[ver[p]]};
        else if (cnt[dep[ver[p]]] == mx[v].first)
          mx[v].second = min(mx[v].second, dep[ver[p]]);
      }
    }
  }
  ans[v] = mx[v].second - dep[v];
  if (!keep) {
    for (int p = in[v]; p < out[v]; p++) {
      cnt[dep[ver[p]]]--;
    }
    mx[v] = {0, 0};
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int v, u;
    scanf("%d%d", &v, &u);
    adj[v].push_back(u);
    adj[u].push_back(v);
  }
  pre(1, 1, 1);
  dfs(1, 1, 0);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
  return 0;
}
