import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;

public class Nightmare {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int nVerts = sc.nextInt();
        int nEdges = sc.nextInt();

        Vert[] verts = new Vert[nVerts];
        boolean[] done = new boolean[nVerts];
        boolean[] started = new boolean[nVerts];

        for (int i = 0; i < nVerts; i++) {
            verts[i] = new Vert(i);
            done[i] = false;
            started[i] = false;
        }

        for (int i = 0; i < nEdges; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();

            Vert A = verts[a];
            Vert B = verts[b];

            verts[a].add_n(B, c);
            verts[b].add_n(A, c);
        }
        LinkedList<Frame> frames = new LinkedList<>();
        int i = 0;
        boolean sleeps = true;
        while (sleeps) {
            if (frames.isEmpty()) {  //sme done
                if (i >= nVerts) {  //sme done
                    System.out.println("YES");
                    return;
                }
                while (i < nVerts) {  //najdi frame
                    if (started[i]) {
                        i++;
                        continue;
                    }
                    frames.add(new Frame(verts[i]));
                    started[i] = true;
                    i++;
                    break;
                }
            }

            Frame now = frames.pop();

            if (done[now.vert.name]) {
                continue;
            }
            done[now.vert.name] = now.last;

            int vertI = now.cesta.indexOf(now.vert.name);
            if (vertI != -1) {
                sleeps &= now.counts.getLast()[0]-now.counts.get(vertI)[0]!=1;
                sleeps &= now.counts.getLast()[1]-now.counts.get(vertI)[1]!=1;
                continue;
            }

            for (Vert v : now.vert.neighbours.values()) {
                if (done[v.name]) {
                    continue;
                }
                Frame f = new Frame(v);
                f.cesta = new ArrayList<>(now.cesta);
                f.counts = new ArrayList<>(now.counts);
                f.cesta.add(now.vert.name);
                var arr = now.counts.getLast().clone();
                arr[0] += (now.vert.color.get((v.name))+1)%2;
                arr[1] += now.vert.color.get(v.name);
                f.counts.add(arr);
                frames.add(f);
            }
            frames.getLast().last = true;
        }
        System.out.println("NO");
    }


    static class Vert {
        public int name;
        HashMap<Integer, Vert> neighbours;
        HashMap<Integer, Integer> color;

        public Vert(int n) {
            this.name = n;
            this.neighbours = new HashMap<>();
            this.color = new HashMap<>();
        }

        public void add_n(Vert v, int c) {
            neighbours.put(v.name, v);
            color.put(v.name, c);
        }

    }

    static class Frame {
        public ArrayList<Integer> cesta;
        public ArrayList<int[]> counts;
        public Vert vert;
        public boolean last;

        public Frame(Vert vert) {
            this.vert = vert;
            this.last = last;
            cesta = new ArrayList<>();
            counts = new ArrayList<>();
            int[] a = {0, 0};
            counts.add(a);
            this.last = false;
        }
    }
}