1.

package contestVolumes.volume118;

import java.util.Scanner;

/**
 * FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS10
 * Problem: 11838 - Come and Go
 * Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=226&problem=2938&mosmsg=Submission+received+with+ID+8260040
 *
 * @author Siegfried Ippisch
 * @version 1.0
 *
 * Method : Floyd–Warshall algorithm
 * Status : Accepted
 * Runtime: 1.352
 */
public class ComeAndGo {
   
    public static void floydWarshall(boolean[][] adja){
        int lenght = adja.length;
        for (int k = 0; k < lenght; k++)
            for (int i = 0; i < lenght; i++)
                for (int j = 0; j < lenght; j++)
                    adja[i][j] = adja[i][j] || adja[i][k] && adja[k][j];
    }
   
    public static boolean check(boolean[][] adja){
        for(boolean[] bArray: adja)
            for(boolean b: bArray)
                if(!b) return false;
        return true;
    }
   
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
       
        int n = in.nextInt();
        int m = in.nextInt();
        while(n != 0 && m != 0){
            boolean[][] adja = new boolean[n][n];
           
            for(int i=0; i<n; i++)
                adja[i][i] = true;
           
            for(int i=0; i<m; i++){
                int v = in.nextInt()-1;
                int w = in.nextInt()-1;
                adja[v][w] = true;
                if(in.nextInt() == 2)
                    adja[w][v] = true;
            }
           
            floydWarshall(adja);
            boolean res = check(adja);
           
            System.out.println(res ? 1: 0);
           
            n = in.nextInt();
            m = in.nextInt();
        }
       
        in.close();
    }
   
}