// WA #include #include #include #include #include using namespace std; #include using mint = atcoder::modint1000000007; template int rank_of_matrix(std::vector> &M) { const int H = M.size(), W = M[0].size(); int rank = 0; for (int i = 0; i < H; ++i) { int ti = i; while (ti < H and M[ti][i] == 0) ti++; if (ti == H) continue; ++rank; M[i].swap(M[ti]); T inv = T(1) / M[i][i]; for (int j = i + 1; j < W; ++j) M[i][j] *= inv; for (int h = 0; h < H; ++h) { if (i == h) continue; const T c = -M[h][i]; for (int j = i + 1; j < W; ++j) M[h][j] += M[i][j] * c; } } return rank; } int wrong_answer(int N, const vector> &uvws) { vector> M; for (auto [u, v, w] : uvws) { vector a(N), b(N); a[u]++; a[v]--; M.push_back(a); b[u]++; b[w]--; M.push_back(b); } return rank_of_matrix(M) / 2; } int main() { int N, M; cin >> N >> M; vector> uvws; while (M--) { int u, v, w; cin >> u >> v >> w; u--, v--, w--; uvws.emplace_back(u, v, w); } cout << max(wrong_answer(N, uvws), wrong_answer(N, uvws)) << '\n'; }