Wheel Rotation

No attempts yetTime limit1sMemory limit128 MB

Problem

Farmer John has an old-time thresher (a wheat harvester) that requires belts to be installed on its gears to turn the parts. The engine drives pulley $1$ in a clockwise direction; it attaches via a belt to pulley $2$, pulley $2$ attaches via a belt to pulley $3$, and so on through a total of $N$ ($2 \le N \le 1{,}000$) pulleys (and $N-1$ belts).

The diagram above shows the two ways a belt can be installed between two gears. Here, pulley $1$'s belt directly drives pulley $2$ (a "straight" connection), so they rotate in the same direction. Pulley $3$ drives pulley $4$ via a "crossed" belt, which reverses the direction of rotation.

Given the list of belt types connecting the pulleys, and the fact that pulley $1$ is driven clockwise by the engine, determine the rotation direction of pulley $N$. Each belt is described by three integers:

  • $S_i$ — the driving (source) pulley
  • $D_i$ — the driven (destination) pulley
  • $C_i$ — the connection type ($0$ = straight, $1$ = crossed)

The belts are listed in an arbitrary order.

As an example, consider $N = 4$ with pulley $1$ driven clockwise. Straight belts drive pulley $2$ and then pulley $3$, so they rotate clockwise. A crossed belt reverses the rotation, so pulley $4$ (pulley $N$) rotates counterclockwise.

Input

  • Line $1$: a single integer $N$.
  • Lines $2 \ldots N$: each line describes a belt with three integers $S_i$, $D_i$, and $C_i$.

Output

  • Line $1$: a single integer giving the rotation direction of pulley $N$ ($0$ = clockwise, $1$ = counterclockwise).