#include <bits/stdc++.h>
using namespace std;
int n, ans[1000010], sz[1000010], son[1000010];
int sum[1000010], d[1000010], maxd;
bool is[1000010];
vector<int> v[1000010];
void read(int& x) {
  char c = getchar();
  x = 0;
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
}
void dfs(int x) {
  sz[x] = 1;
  for (int y : v[x]) {
    if (!sz[y]) {
      d[y] = d[x] + 1;
      dfs(y);
      if (sz[son[x]] < sz[y]) son[x] = y;
      sz[x] += sz[y];
    }
  }
}
inline void add(int x, int op) {
  sum[d[x]] += op;
  if (sum[d[x]] > sum[maxd] || (sum[d[x]] == sum[maxd] && d[x] < maxd))
    maxd = d[x];
}
void update(int x, int fa, int op) {
  add(x, op);
  for (int y : v[x])
    if (y != fa && !is[y]) update(y, x, op);
}
void dfs(int x, int fa, bool keep) {
  for (int y : v[x])
    if (y != fa && y != son[x]) dfs(y, x, 0);
  if (son[x]) dfs(son[x], x, 1);
  is[son[x]] = 1;
  update(x, fa, 1);
  is[son[x]] = 0;
  ans[x] = maxd - d[x];
  if (!keep) {
    update(x, fa, -1);
    maxd = 0;
  }
}
int main() {
  read(n);
  for (int i = 1; i < n; i++) {
    int x, y;
    read(x);
    read(y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  d[1] = 1;
  dfs(1);
  dfs(1, 0, 0);
  for (int i = 1; i <= n; i++) {
    printf("%d\n", ans[i]);
  }
}
