#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6 + 5;
int nxt[MAX << 1], to[MAX << 1], head[MAX], cnt;
inline void add(int x, int y) {
  ++cnt;
  nxt[cnt] = head[x], to[cnt] = y, head[x] = cnt;
  ++cnt;
  nxt[cnt] = head[y], to[cnt] = x, head[y] = cnt;
}
int fa[MAX], depth[MAX], son[MAX], max_depth[MAX], top[MAX];
int dp[MAX << 1], start[MAX], cc, ans[MAX];
inline void dfs1(int now, int father) {
  max_depth[now] = 1;
  fa[now] = father, depth[now] = depth[father] + 1;
  for (int aim, i = head[now]; i; i = nxt[i]) {
    aim = to[i];
    if (aim != father) {
      dfs1(aim, now);
      if (max_depth[aim] + 1 > max_depth[now])
        max_depth[now] = max_depth[aim] + 1;
      if (max_depth[aim] > max_depth[son[now]]) son[now] = aim;
    }
  }
}
int n;
inline void dfs2(int now, int tt, int type) {
  top[now] = tt;
  if (type == 0) start[now] = cc, cc += max_depth[now];
  for (int aim, i = head[now]; i; i = nxt[i]) {
    aim = to[i];
    if (aim != fa[now] && aim != son[now]) dfs2(aim, aim, 0);
  }
  if (son[now]) dfs2(son[now], tt, 1);
  dp[start[top[now]] + depth[now] - depth[top[now]]]++;
  int amax = 1, place = depth[now];
  for (int aim, i = head[now]; i; i = nxt[i]) {
    aim = to[i];
    if (aim != fa[now] && aim != son[now]) {
      for (int j = 0; j < max_depth[aim]; j++) {
        dp[start[top[now]] + depth[now] - depth[top[now]] + j + 1] +=
            dp[start[aim] + j];
        if (dp[start[top[now]] + depth[now] - depth[top[now]] + j + 1] > amax)
          amax = dp[start[top[now]] + depth[now] - depth[top[now]] + j + 1],
          place = depth[now] + j + 1;
        else if (dp[start[top[now]] + depth[now] - depth[top[now]] + j + 1] ==
                 amax)
          place = min(place, depth[now] + j + 1);
      }
    }
  }
  if (son[now] && dp[start[top[son[now]]] + depth[now] - depth[top[son[now]]] +
                     ans[son[now]] + 1] > amax)
    amax = dp[start[top[son[now]]] + depth[now] - depth[top[son[now]]] +
              ans[son[now]] + 1],
    place = ans[son[now]] + 1 + depth[now];
  ans[now] = place - depth[now];
}
int main() {
  scanf("%d", &n);
  for (int x, y, i = 1; i < n; i++) scanf("%d %d", &x, &y), add(x, y);
  dfs1(1, 1), dfs2(1, 1, 0);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
  return 0;
}
