#include <bits/stdc++.h>
using namespace std;
int n, h[1010100], cnt[1010100], sz[1010100], answer[1010100], best;
vector<int> adj[1010100];
void dfs(int u, int p) {
  h[u] = h[p] + 1;
  sz[u] = 1;
  for (int v : adj[u]) {
    if (v == p) continue;
    dfs(v, u);
    sz[u] += sz[v];
  }
}
void add(int u, int p, int flag) {
  cnt[h[u]] += flag;
  if (cnt[h[u]] > cnt[best] || cnt[h[u]] == cnt[best] && h[u] < best)
    best = h[u];
  for (int v : adj[u]) {
    if (v == p) continue;
    add(v, u, flag);
  }
}
void solve(int u, int p, int flag) {
  int big = -1;
  for (int v : adj[u]) {
    if (v == p) continue;
    if (big == -1 || sz[v] > sz[big]) big = v;
  }
  best = 0;
  for (int v : adj[u]) {
    if (v == p || v == big) continue;
    solve(v, u, 0);
  }
  if (big != -1) solve(big, u, 1);
  int ans = answer[big];
  best = ans;
  for (int v : adj[u]) {
    if (v == big || v == p) continue;
    add(v, u, 1);
  }
  cnt[h[u]]++;
  if (cnt[h[u]] >= cnt[best]) best = h[u];
  answer[u] = best;
  if (flag == 0) {
    add(u, p, -1);
  }
  best = 0;
}
int main() {
  scanf("%d", &n);
  int a, b;
  for (int i = 1; i < n; i++) {
    scanf("%d %d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs(1, 0);
  solve(1, 0, 0);
  for (int i = 1; i <= n; i++) {
    printf("%d\n", answer[i] - h[i]);
  }
}
