#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &x) {
  char t(getchar()), flg(0);
  x = 0;
  for (; !isdigit(t); t = getchar()) flg = t == '-';
  for (; isdigit(t); t = getchar()) x = x * 10 + (t & 15);
  flg ? x = -x : x;
}
template <class T, class... Args>
inline void read(T &x, Args &...args) {
  read(x);
  read(args...);
}
const int N = 1e6 + 10, inf = 0x3f3f3f3f;
int n;
vector<int> v[N];
int size[N], depth[N], son[N];
int vis[N], cnt[N], ans[N], mxp, mnd;
void dfs1(int u, int f) {
  size[u] = 1, depth[u] = depth[f] + 1;
  for (vector<int>::iterator it = v[u].begin(); it != v[u].end(); ++it)
    if (*it != f) {
      dfs1(*it, u);
      size[u] += size[*it];
      if (size[son[u]] < size[*it]) son[u] = *it;
    }
}
void calc(int u, int f, int k) {
  cnt[depth[u]] += k;
  if (k && cnt[depth[u]] >= mxp) {
    if (cnt[depth[u]] > mxp)
      mxp = cnt[depth[u]], mnd = depth[u];
    else if (cnt[depth[u]] == mxp && depth[u] < mnd)
      mnd = depth[u];
  }
  for (vector<int>::iterator it = v[u].begin(); it != v[u].end(); ++it)
    if (*it != f && !vis[*it]) calc(*it, u, k);
}
void dfs(int u, int f, int k) {
  for (vector<int>::iterator it = v[u].begin(); it != v[u].end(); ++it)
    if (*it != f && *it != son[u]) dfs(*it, u, 0);
  if (son[u]) dfs(son[u], u, 1), vis[son[u]] = 1;
  calc(u, f, 1);
  ans[u] = mnd - depth[u];
  if (son[u]) vis[son[u]] = 0;
  if (!k) calc(u, f, -1), mnd = inf, mxp = 0;
}
int main() {
  read(n);
  for (int i = 1; i < n; ++i) {
    int x, y;
    read(x, y);
    v[x].push_back(y), v[y].push_back(x);
  }
  dfs1(1, 0);
  dfs(1, 0, 0);
  for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]);
}
