n = read_line.to_i g = Array.new(n) { [] of Int32 } flag = [false] * n n.pred.times do a, b = read_line.split.map &.to_i.pred g[a] << b flag[b] = true end def dfs(v : Int32, dist : Int32, g : Array(Array(Int32))) res = (1..dist).sum.to_i64 g[v].each do |u| res += dfs(u, dist + 1, g) end res end puts dfs(flag.index(false).not_nil!, 0, g)