#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int, int> > biEdge[123456];
vector<int> uniEdge[123456];
vector<int> nodes[123456];
struct Edge {
  int t, w;
  Edge(int t, int w) : t(t), w(w) {}
};
vector<Edge> edge[567891];
void addE(int u, int v, int w) { edge[u].push_back(Edge(v, w)); }
int findNode(int u, int id, int tp) {
  int pos = lower_bound(biEdge[u].begin(), biEdge[u].end(), make_pair(id, -1)) -
            biEdge[u].begin();
  return nodes[u][2 + biEdge[u].size() * tp + pos];
}
int indeg[567891];
int main() {
  cin >> n >> m;
  for (int it = 0; it < m; it++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    --u, --v;
    if (w == 2) {
      biEdge[u].push_back(make_pair(it, v));
      biEdge[v].push_back(make_pair(it, u));
    } else {
      uniEdge[u].push_back(v);
    }
  }
  int V = 0;
  for (int i = 0; i < n; i++) {
    nodes[i].push_back(V++);
    nodes[i].push_back(V++);
    sort(biEdge[i].begin(), biEdge[i].end());
    for (int it = 0; it < biEdge[i].size(); it++) {
      nodes[i].push_back(V++);
      nodes[i].push_back(V++);
      nodes[i].push_back(V++);
    }
    int m = biEdge[i].size();
    for (int it = 0; it < biEdge[i].size(); it++) {
      int lt = it + 2;
      int rt = m + it + 2;
      int ct = m * 2 + it + 2;
      if (it > 0) addE(nodes[i][lt], nodes[i][lt - 1], 0);
      if (it + 1 < m) addE(nodes[i][rt], nodes[i][rt + 1], 0);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int it = 0; it < uniEdge[i].size(); it++) {
      int v = uniEdge[i][it];
      addE(nodes[i][1], nodes[v][0], 1);
    }
    int m = biEdge[i].size();
    if (m > 0) {
      for (int it = 0; it < biEdge[i].size(); it++) {
        int lt = it + 2;
        int rt = m + it + 2;
        int ct = m * 2 + it + 2;
        if (it > 0) {
          int id = biEdge[i][it - 1].first;
          int v = biEdge[i][it - 1].second;
          addE(nodes[i][lt], findNode(v, id, 2), 1);
        }
        if (it + 1 < m) {
          int id = biEdge[i][it + 1].first;
          int v = biEdge[i][it + 1].second;
          addE(nodes[i][rt], findNode(v, id, 2), 1);
        }
        addE(nodes[i][ct], nodes[i][lt], 0);
        addE(nodes[i][ct], nodes[i][rt], 0);
        addE(nodes[i][ct], nodes[i][1], 0);
      }
      addE(nodes[i][0], nodes[i][2 + m], 0);
      {
        int id = biEdge[i][0].first;
        int v = biEdge[i][0].second;
        addE(nodes[i][0], findNode(v, id, 2), 1);
      }
    }
    addE(nodes[i][0], nodes[i][1], 0);
  }
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < edge[i].size(); j++) indeg[edge[i][j].t]++;
  }
  static int que[567891], qh = 0, qt = 0;
  for (int i = 0; i < V; i++)
    if (indeg[i] == 0) que[qt++] = i;
  while (qh < qt) {
    int u = que[qh++];
    for (int j = 0; j < edge[u].size(); j++) {
      int v = edge[u][j].t;
      indeg[v]--;
      if (!indeg[v]) que[qt++] = v;
    }
  }
  static int dp[567891];
  if (qt != V) {
    puts("Infinite");
  } else {
    for (int i = 0; i < V; i++) {
      int u = que[V - 1 - i];
      dp[u] = 0;
      for (int j = 0; j < edge[u].size(); j++) {
        int v = edge[u][j].t;
        dp[u] = max(dp[u], dp[v] + edge[u][j].w);
      }
    }
    int ans = *max_element(dp, dp + V);
    printf("%d\n", ans);
  }
}
