import Foundation
let N = Int(readLine()!)!

// -1: not determined, 0: black, 1: white
var graph: [[(Int, Int)]] = .init(repeating: [], count: N)
var colors: [Int] = .init(repeating: 0, count: N)
var indegrees: [Int] = .init(repeating: 0, count: N)

for _ in 0..<N-1 {
  let input = readLine()!.split(separator: " ").map { Int($0)! }
  graph[input[0] - 1].append((input[1] - 1, input[2]))
  indegrees[input[1] - 1] += 1
}


var queue = Set<Int>()
for i in 0..<indegrees.count {
  if indegrees[i] == 0 {
    queue.insert(i)
  }
}
while !queue.isEmpty {
  var tmp = Set<Int>()
  for top in queue {
    for nei in graph[top] {
      let index = nei.0
      let distance = nei.1
      if distance & 1 == 0 && colors[top] != colors[index] {
        colors[index] = colors[top]
      } else if distance & 1 == 1 && colors[top] == colors[index] {
        colors[index] = colors[top] ^ 1
      }
      indegrees[index] -= 1
      if indegrees[index] == 0 {
        tmp.insert(index)
      }
    }
  }
  queue = tmp
}