In a certain city there are intersections connected by one-way and two-way streets. It is a modern city, and several of the streets have tunnels or overpasses. Evidently it must be possible to travel between any two intersections. More precisely given two intersections and it must be possible to travel from to and from to .
Your task is to write a program that reads a description of the city street system and determines whether the requirement of connectedness is satisfied or not.
The input contains several test cases. The first line of a test case contains two integers (2 \le n \le 2000) and (2 \le m \le n \cdot (n - 1) / 2), separated by a space, indicating the number of intersections and number of streets. The next lines describe the city street system, with each line describing one street. A street description consists of three integers , (1 \le v, w \le n, v \neq w) and , separated by a blank space, where and are distinct identifiers for intersections and can be or . If p = 1 the street is one-way, and traffic goes from to , if p = 2 then the street is two-way and links and . A pair of intersections is connected by at most one street.
The last test case is followed by a line that contains only two zero numbers separated by a blank space.
For each test case your program should print a single line containing an integer , where is equal to one if the condition of connectedness is satisfied, and is zero otherwise.
Example 1