import java.util.*; public class Main { static ArrayList> graph = new ArrayList<>(); static int[] roots; static int[] leaves; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); roots = new int[n]; leaves = new int[n]; for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < n - 1; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; graph.get(a).add(b); graph.get(b).add(a); } getDist(0, 0, 0); StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(Math.min(roots[i], leaves[i])).append("\n"); } System.out.print(sb); } static int getDist(int idx, int parent, int depth) { roots[idx] = depth; if (idx != 0 && graph.get(idx).size() == 1) { leaves[idx] = 0; } else { int min = Integer.MAX_VALUE; for (int x : graph.get(idx)) { if (x == parent) { continue; } min = Math.min(min, getDist(x, idx, depth + 1)); } leaves[idx] = min; } return leaves[idx] + 1; } }