import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from scipy.sparse import csr_matrix from scipy.sparse.csgraph import connected_components import numpy as np N, M = map(int, readline().split()) AB1 = [] AB2 = [] m = map(int, read().split()) for a, b, c in zip(m, m, m): if c == 1: AB1.append((a, b)) else: AB2.append((a, b)) A1, B1 = zip(*AB1) A2, B2 = zip(*AB2) U = A1 + B1 + A2 V = B1 + A1 + B2 G = csr_matrix(([1] * len(U), (U, V)), (N + 1, N + 1)) _, comp = connected_components(G, directed=True, connection='strong') comp_size = np.bincount(comp) C = comp.max() comp = list(comp) n_edges = [0] * (C + 1) for a, b in AB1 + AB2: ca = comp[a] cb = comp[b] if ca != cb: continue n_edges[ca] += 1 bl = any(x <= y for x, y in zip(comp_size, n_edges)) answer = 'Yes' if bl else 'No' print(answer)