import queue INF = 1e9 class Node: def __init__(self, id): self.id = id self.children = [] N = int(input()) nodes = [None] * (N) for n in range(N-1): x,y = list(map(int, input().split(' '))) x -= 1 y -= 1 if nodes[x] == None: nodes[x] = Node(x) nodes[x].children.append(y) if nodes[y] == None: nodes[y] = Node(y) nodes[y].children.append(x) # find leaves leaves = [] for i in range(N): if len(nodes[i].children) == 1 and i != 0: leaves.append(i) leaves.sort() # find distance from the root d1 = [INF] * 100010 q = queue.Queue() q.put((0,-1)) d1[0] = 0 while not q.empty(): m,n = q.get() for c in nodes[m].children: if c != n and d1[c] == INF: d1[c] = min(d1[c], d1[m] + 1) q.put((c,m)) # find distance from the leaf d2 = [INF] * 100010 for i in leaves: q = queue.Queue() q.put((i,-1)) d2[i] = 0 while not q.empty(): m,n = q.get() for c in nodes[m].children: if c != n: d2[c] = min(d2[c], d2[m]+1) q.put((c,m)) for i in range(N): print(min(d1[i], d2[i]))