# Here your code ! INF = 10 ** 8 n = gets.to_i P = Array.new(n+1,1) R = Array.new(n+1,0) L = Array.new(n+1,INF) leaf = Array.new(n+1,true) (n - 1).times do from, to = gets.split.map(&:to_i) P[to] = from R[to] = R[from] + 1 leaf[from] = false end (1..n).select {|v| leaf[v]}.each do |i| d = R[i] + 1 d.times.inject(i) do |j, dep| L[j] = [L[j], dep].min P[j] end end puts (1..n).map {|i| [R[i], L[i]].min }