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 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) for i in xrange(N): print min(R[i], L[i])