#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
inline void base() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(6);
}
const int N = 1e6;
vector<int> adj[N + 5];
int sz[N + 5], col[N + 5];
int bigCnt[N + 5];
int idx[N + 5];
bool bigCh[N + 5];
int ans[N + 5];
int tmp, idxAns;
void dfs(int x, int par, int dep) {
  sz[x] = 1;
  col[x] = dep;
  for (int i = 0; i < (int)adj[x].size(); ++i) {
    int nx = adj[x][i];
    if (nx == par) continue;
    dfs(nx, x, dep + 1);
    sz[x] += sz[nx];
  }
}
void add(int x, int par, int val) {
  bigCnt[col[x]] += val;
  if (bigCnt[col[x]] > tmp) {
    tmp = bigCnt[col[x]];
    idxAns = col[x];
  } else if (bigCnt[col[x]] == tmp) {
    if (idxAns > col[x]) idxAns = col[x];
  }
  for (int i = 0; i < (int)adj[x].size(); ++i) {
    int nx = adj[x][i];
    if (nx == par || bigCh[nx]) continue;
    add(nx, x, val);
  }
}
void dsu(int x, int par, bool big) {
  int mx = -1, id = -1;
  for (int i = 0; i < (int)adj[x].size(); ++i) {
    int nx = adj[x][i];
    if (nx == par) continue;
    if (sz[nx] > mx) mx = sz[nx], id = nx;
  }
  for (int i = 0; i < (int)adj[x].size(); ++i) {
    int nx = adj[x][i];
    if (nx == par || nx == id) continue;
    dsu(nx, x, 0);
  }
  if (id != -1) dsu(id, x, 1), bigCh[id] = 1;
  add(x, par, 1);
  ans[x] = idxAns - col[x];
  if (id != -1) bigCh[id] = 0;
  if (big == 0) add(x, par, -1), tmp = idxAns = -1;
}
int getInt() {
  char x = getchar();
  int res = 0;
  while (!isdigit(x)) x = getchar();
  for (; isdigit(x); x = getchar()) {
    res *= 10;
    res += x - '0';
  }
  return res;
}
int main() {
  int n;
  n = getInt();
  for (int i = 1; i < n; ++i) {
    int a, b;
    a = getInt();
    b = getInt();
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  tmp = idxAns = -1;
  dfs(1, -1, 0);
  dsu(1, -1, 1);
  for (int i = 1; i <= n; ++i) {
    printf("%d\n", ans[i]);
  }
  return 0;
}
