from collections import deque
n, m = map(int, input().split())
G = [[] for i in range(n)]
for i in range(m):
    x, y, w = map(int, input().split())
    G[x-1].append((y-1, i))
    if w == 2:
        G[y-1].append((x-1, i))
used = {(i, -1) for i in range(n)}
prev = {(i, -1): [] for i in range(n)}
counter = {}
que = deque((i, -1) for i in range(n))
while que:
    v, e = source = que.popleft()
    counter[source] = 0
    for target in G[v]:
        t, f = target
        if e == f:
            continue
        if target not in used:
            used.add(target)
            prev[target] = []
            que.append(target)
        prev[target].append(source)
        counter[source] += 1
rest = len(counter)
for p in counter:
    if counter[p] == 0:
        que.append(p)
        rest -= 1
while que:
    target = que.popleft()
    for source in prev[target]:
        counter[source] -= 1
        if counter[source] == 0:
            que.append(source)
            rest -= 1
if rest > 0:
    print("Infinite")
    exit(0)

memo = {}
def dfs(source):
    if source in memo:
        return memo[source]
    res = 0
    v, e = source
    for target in G[v]:
        t, f = target
        if e == f:
            continue
        res = max(res, dfs(target) + 1)
    memo[source] = res
    return res
print(max(dfs((i, -1)) for i in range(n)))