import sys sys.setrecursionlimit(1000000) def dfs(i, d): R[i] = d if len(chil[i]) > 0: mn = 1e9 for j in chil[i]: mn = min(mn, dfs(j, d + 1)) L[i] = mn else: L[i] = 0 return L[i] + 1 def dfs2(i, d): if d < L[i]: L[i] = d else: d = L[i] for j in chil[i]: dfs2(j, d + 1) N = int(raw_input()) chil = [[] for i in xrange(N)] for loop in xrange(N - 1): x, y = map(int, raw_input().split()) x -= 1 y -= 1 chil[x].append(y) R, L = [0] * N, [1e9] * N dfs(0, 0) dfs2(0, L[0]) for i in xrange(N): print min(R[i], L[i])