#include <bits/stdc++.h>
using namespace std;
int n, fa[1000005], len[1000005], son[1000005], ans[1000005];
vector<int> G[1000005], f[1000005];
inline void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    (x *= 10) += ch - '0';
    ch = getchar();
  }
  return x * f;
}
void dfs1(int u, int c_fa) {
  fa[u] = c_fa;
  for (int v : G[u]) {
    if (v == c_fa) continue;
    dfs1(v, u);
    if (len[v] >= len[son[u]]) son[u] = v, len[u] = len[v] + 1;
  }
}
void dfs2(int u) {
  if (son[u]) {
    dfs2(son[u]);
    swap(f[u], f[son[u]]);
    f[u].push_back(1);
    ans[u] = ans[son[u]];
    if (f[u][ans[u]] == 1) ans[u] = len[u];
    for (int v : G[u]) {
      if (v == fa[u] || v == son[u]) continue;
      dfs2(v);
      for (int i = len[v]; ~i; i--) {
        int tmp = i + len[u] - len[v] - 1;
        f[u][tmp] += f[v][i];
        if (f[u][tmp] > f[u][ans[u]] ||
            (f[u][tmp] == f[u][ans[u]] && tmp > ans[u]))
          ans[u] = tmp;
      }
    }
  } else {
    f[u].push_back(1);
    ans[u] = 0;
  }
}
int main() {
  n = read();
  for (int i = 1; i < n; i++) add_edge(read(), read());
  dfs1(1, 0);
  dfs2(1);
  for (int i = 1; i <= n; i++) printf("%d\n", len[i] - ans[i]);
  return 0;
}
