#include using namespace std; using ll = long long; #define ALL(v) v.begin(),v.end() #define dbg(x) cerr << #x << ": " << (x) << endl; int n,m; vector> e; struct WUF { WUF(int n) { par.resize(n); iota(par.begin(), par.end(), 0); rank.assign(n, 1); w.resize(n, 0); } // 根ノード int root(int x) { if (par[x] == x) return x; int rx = root(par[x]); w[x] += w[par[x]]; return par[x] = rx; } // weight(y) = weight(x) + dwとなるように結合 bool unite(int x, int y, ll dw) { dw += weight(x); dw -= weight(y); x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x,y), dw = -dw; if (rank[x] == rank[y]) ++rank[x]; par[y] = x; w[y] = dw; return true; } // 同じ集合に所属するか bool same(int x, int y) { return root(x) == root(y); } // 親ノードを0とするときの重み long long weight(int x) { root(x); return w[x]; } // 頂点xと頂点yの重みの差 // 制約:xとyが連結 long long diff(int x, int y) { return weight(y) - weight(x); } vector> groups() { vector> gr(par.size()); for (int v = 0; v < par.size(); ++v) { gr[root(v)].push_back(v); } vector> res; for (int v = 0; v < (int)par.size(); ++v) { if (gr[v].size()) res.push_back(gr[v]); } return res; } vector par; vector rank; vector w; }; int main() { cin >> n >> m; e.resize(m); for (int i = 0; i < m; ++i) { auto& [u,v,w,id] = e[i]; cin >> u >> v >> w; --u; --v; id = i; } vector>> g(n); WUF uf(n); for (auto&& [u,v,w,i] : e) { // check; // for (int j = 0; j < n; ++j) { // cerr << uf.weight(j) << " \n"[j==n-1]; // } if (uf.same(u,v)) { if (uf.diff(u,v) == w) { /// ok } else { // bfs queue q; vector> pre(n, {-1,-1}); q.push(u); pre[u] = {-2, -2}; while (q.size()) { int pos = q.front(); q.pop(); for (auto [to,id] : g[pos]) { if (pre[to].first == -1) { pre[to].first = pos; pre[to].second = id; q.push(to); } } } vector path; int now = v; while (pre[now].second != -2) { path.push_back(pre[now].second); now = pre[now].first; } path.push_back(i); // out cout << v+1 << '\n'; cout << path.size() << '\n'; for (int j = 0; j < path.size(); ++j) { cout << path[j]+1 << " \n"[j==path.size()-1]; } return 0; } } else { uf.unite(u, v, w); g[u].emplace_back(v, i); g[v].emplace_back(u, i); } } cout << "-1\n"; }