#include <bits/stdc++.h>
using namespace std;
const int sz = 1e6 + 5;
int sbz[sz], depth[sz], cnt[sz], ans[sz], who;
vector<int> g[sz];
bitset<sz> heavy;
set<int> st[sz];
void add(int u, int p, int f) {
  int d = depth[u], &w = cnt[d];
  if (w) st[w].erase(d);
  w += f;
  who = max(who, w);
  if (w) st[w].insert(d);
  if (st[who].empty()) who--;
  for (int v : g[u])
    if (v ^ p && !heavy[v]) add(v, u, f);
}
void dfs(int u = 1, int p = 0, bool keep = 0) {
  int mx = 0, bigc = 0;
  for (int v : g[u])
    if (v ^ p && sbz[v] > mx) mx = sbz[v], bigc = v;
  for (int v : g[u])
    if (v ^ p && v ^ bigc) dfs(v, u);
  if (bigc) heavy[bigc] = 1, dfs(bigc, u, 1);
  add(u, p, 1);
  ans[u] = *st[who].begin() - depth[u];
  heavy[bigc] = 0;
  if (!keep) add(u, p, -1);
}
int getsbz(int u = 1, int p = 0, int lvl = 1) {
  sbz[u] = 1;
  depth[u] = lvl;
  for (int v : g[u])
    if (v ^ p) sbz[u] += getsbz(v, u, lvl + 1);
  return sbz[u];
}
int main() {
  int n;
  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);
  }
  getsbz();
  dfs();
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
}
