#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 3e6 + 100;
int n;
int v[N], head[N], tot, nxt[N];
int sz[N], deep[N], ans[N];
int Son;
int zson[N];
map<int, int> mp;
int mx = 0;
void add(int x, int y) {
  v[++tot] = y;
  nxt[tot] = head[x];
  head[x] = tot;
}
void getson(int x, int f, int dep) {
  deep[x] = dep;
  sz[x] = 1;
  for (int i = head[x]; i; i = nxt[i]) {
    int y = v[i];
    if (y == f) continue;
    getson(y, x, dep + 1);
    sz[x] += sz[y];
    if (sz[y] > sz[zson[x]]) {
      zson[x] = y;
    }
  }
}
void js(int x, int f) {
  mp[deep[x]]++;
  if (mp[deep[x]] > mp[mx] || mp[deep[x]] == mp[mx] && deep[x] < mx) {
    mx = deep[x];
  }
  for (int i = head[x]; i; i = nxt[i]) {
    int y = v[i];
    if (y == f || y == Son) continue;
    js(y, x);
  }
}
void dsu(int x, int f, int keep) {
  for (int i = head[x]; i; i = nxt[i]) {
    int y = v[i];
    if (y == zson[x] || y == f) continue;
    dsu(y, x, 0);
  }
  if (zson[x]) {
    dsu(zson[x], x, 1);
    Son = zson[x];
  }
  js(x, f);
  Son = 0;
  ans[x] = mx - deep[x];
  if (!keep) {
    mp.clear();
    mx = 0;
  }
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  getson(1, 0, 1);
  dsu(1, 0, 1);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
