#include <bits/stdc++.h>
using namespace std;
int n, m;
int x[100005], y[100005], w[100005];
vector<int> G[100005];
vector<int> g[100005];
vector<int> group[100005];
int id[100005];
bool visited[100005];
vector<int> vd;
int dp[100005];
bool dfs(int pos, int prev, int root) {
  id[pos] = root;
  visited[pos] = true;
  group[root].push_back(pos);
  for (int i = 0; i < (int)G[pos].size(); i++) {
    int to = G[pos][i];
    if (to == prev) continue;
    if (i && to == G[pos][i - 1]) return true;
    if (visited[to]) return true;
    if (dfs(to, pos, root)) return true;
  }
  return false;
}
bool check() {
  memset(visited, false, sizeof(visited));
  for (int i = 0; i < n; i++) {
    if (visited[i]) continue;
    if (dfs(i, -1, i)) return true;
  }
  vector<int> deg(n);
  vector<vector<int> > graph(n);
  for (int i = 0; i < m; i++) {
    if (w[i] == 1) {
      int X = id[x[i]];
      int Y = id[y[i]];
      if (X == Y) return true;
      graph[X].push_back(Y);
      deg[Y]++;
    }
  }
  queue<int> Q;
  for (int i = 0; i < n; i++)
    if (deg[i] == 0 && id[i] == i) Q.push(i);
  while (!Q.empty()) {
    int pos = Q.front();
    Q.pop();
    vd.push_back(pos);
    for (int to : graph[pos]) {
      deg[to]--;
      if (deg[to] == 0) Q.push(to);
    }
  }
  for (int i = 0; i < n; i++)
    if (deg[i] > 0) return true;
  return false;
}
int d[100005];
void init(int root) {
  root = id[root];
  for (int pos : group[root]) d[pos] = -1;
}
void bfs(int S) {
  init(S);
  queue<int> Q;
  Q.push(S);
  d[S] = 0;
  while (!Q.empty()) {
    int pos = Q.front();
    Q.pop();
    for (int to : G[pos]) {
      if (d[to] != -1) continue;
      d[to] = d[pos] + 1;
      Q.push(to);
    }
  }
}
void update(int root, int dif) {
  for (int pos : group[root])
    for (int to : g[pos]) dp[to] = max(dp[to], d[pos] + dif + 1);
}
int solve(int S, int &ans) {
  bfs(S);
  update(id[S], dp[S]);
  int maxmid = S;
  for (int pos : group[id[S]])
    if (d[pos] + dp[pos] > d[maxmid] + dp[maxmid]) maxmid = pos;
  ans = max(ans, d[maxmid] + dp[maxmid]);
  return maxmid;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d", &x[i], &y[i], &w[i]);
    x[i]--, y[i]--;
    if (w[i] == 1) {
      g[x[i]].push_back(y[i]);
    }
    if (w[i] == 2) {
      G[x[i]].push_back(y[i]);
      G[y[i]].push_back(x[i]);
    }
  }
  if (check()) {
    printf("Infinite\n");
    return 0;
  }
  int ans = 0;
  for (int root : vd) {
    if (root != id[root]) continue;
    solve(solve(solve(root, ans), ans), ans);
  }
  printf("%d\n", ans);
  return 0;
}
