eolymp
bolt
Try our new interface for solving problems
Problems

Come and Go

Come and Go

In a certain city there are n 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 v and w it must be possible to travel from v to w and from w to v.

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.

Input

The input contains several test cases. The first line of a test case contains two integers n (2n2000) and m (2mn * (n1) / 2), separated by a space, indicating the number of intersections and number of streets. The next m lines describe the city street system, with each line describing one street. A street description consists of three integers v, w (1v, wn, vw) and p, separated by a blank space, where v and w are distinct identifiers for intersections and p can be 1 or 2. If p = 1 the street is one-way, and traffic goes from v to w, if p = 2 then the street is two-way and links v and w. 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.

Output

For each test case your program should print a single line containing an integer g, where g is equal to one if the condition of connectedness is satisfied, and g is zero otherwise.

Time limit 1 second
Memory limit 128 MiB
Input example #1
4 5
1 2 1
1 3 2
2 4 1
3 4 1
4 1 2
3 2
1 2 2
1 3 2
3 2
1 2 2
1 3 1
4 2
1 2 2
3 4 2
0 0
Output example #1
1
1
0
0