#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 { vector par, rank; vector w; int n; WUF(int n) : n(n), par(n), rank(n), w(n) { iota(ALL(par),0); } int root(int x) { if (x == par[x]) return x; return par[x] = root(par[x]); } bool same(int x, int y) { return (root(x) == root(y)); } bool unite(int x, int y, ll dw) { x = root(x); y = root(y); dw += w[x]; dw -= w[y]; if (x == y) return false; if (rank[x] > rank[y]) swap(x,y), dw = -dw; if (rank[x] == rank[y]) ++rank[x]; par[x] = y; w[y] = dw; return true; } ll weight(int x) { return w[x]; } ll diff(int x, int y) { return weight(y) - weight(x); } }; 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"; }