#include #include #include #include #include using namespace std; #include using mint = atcoder::modint1000000007; template int rank_of_matrix(std::vector> &M) { const int N = M.size(); int rank = 0; vector js; for (int i = 0; i < N; ++i) { int ti = i; while (ti < N and M[ti][i] == 0) ti++; if (ti == N) continue; ++rank; M[i].swap(M[ti]); T inv = T(1) / M[i][i]; js.clear(); for (int j = i + 1; j < N; ++j) { if (M[i][j] != 0) { M[i][j] *= inv; js.push_back(j); } } for (int h = 0; h < N; ++h) { if (i == h) continue; const T c = -M[h][i]; for (auto j : js) M[h][j] += M[i][j] * c; } } return rank; } mt19937 mt(1967176539); uniform_int_distribution rndgen(0, mint::mod()); int solve(int r, const vector> &uvws) { const int m = uvws.size(); // Geelen & Iwata (2005) vector mat(r + m * 2, vector(r + m * 2)); for (int i = 0; i < m; ++i) { auto [u, v, w] = uvws[i]; mat[u][r + i * 2]++; mat[w][r + i * 2]--; mat[v][r + i * 2 + 1]++; mat[w][r + i * 2 + 1]--; mat[r + i * 2][u]++; mat[r + i * 2][w]--; mat[r + i * 2 + 1][v]++; mat[r + i * 2 + 1][w]--; } for (int i = 0; i < m; ++i) { int x = rndgen(mt); mat[r + i * 2][r + i * 2 + 1] += x; mat[r + i * 2 + 1][r + i * 2] -= x; } return rank_of_matrix(mat) / 2 - m; } 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(solve(N, uvws), solve(N, uvws)) << '\n'; }