from typing import List, Tuple, Callable, TypeVar, Optional import sys import itertools import heapq import bisect import math from collections import deque, defaultdict from functools import lru_cache, cmp_to_key input = sys.stdin.readline if __file__ != 'prog.py': sys.setrecursionlimit(10 ** 6) def readints(): return map(int, input().split()) def readlist(): return list(readints()) def readstr(): return input()[:-1] def readlist1(): return list(map(lambda x: int(x) - 1, input().split())) N = int(input()) E = [[] for _ in range(N)] for _ in range(N - 1): u, v = readints() u -= 1 v -= 1 E[u].append(v) E[v].append(u) R = [0] * N L = [N] * N stack = [(0, -1)] while stack: v, p = stack.pop() if v < 0: v = ~v is_leaf = True for dst in E[v]: if dst == p: continue is_leaf = False L[v] = min(L[v], L[dst] + 1) if is_leaf: L[v] = 0 else: if ~p: R[v] = R[p] + 1 stack.append((~v, p)) for dst in E[v]: if dst == p: continue stack.append((dst, v)) for i in range(N): print(min(R[i], L[i]))