#include using namespace std; using ll = long long; const int INF = 1e9 + 10; const ll INFL = 4e18; template struct DsuPotentialized { DsuPotentialized() = default; DsuPotentialized(int n) { par = vector(n); sz = vector(n); diff_weight = vector(n); for (int i = 0; i < n; i++) { par[i] = i; sz[i] = 1; diff_weight[i] = e(); } } int find(int x) { if (par[x] == x) return x; int root = find(par[x]); diff_weight[x] = op(diff_weight[x], diff_weight[par[x]]); return par[x] = root; } bool unite(int x, int y, Group w) { w = op(w, diff_weight[x]); w = op(inv(diff_weight[y]), w); x = find(x); y = find(y); if (x == y) return false; if (sz[x] < sz[y]) { swap(x, y); w = inv(w); } par[y] = x; sz[x] += sz[y]; diff_weight[y] = w; return true; } bool same(int x, int y) { return find(x) == find(y); } Group diff(int x, int y) { return op(diff_weight[y], inv(diff_weight[x])); } int size(int x) { return sz[find(x)]; } private: vector par, sz; vector diff_weight; }; ll op(ll a, ll b) { return a + b; } ll e() { return 0; } ll inv(ll a) { return -a; } int main() { int N, M; cin >> N >> M; DsuPotentialized dsu(N); vector> G(N); map, int> edgeid; for (int i = 0; i < M; i++) { int u, v, w; cin >> u >> v >> w; u--; v--; edgeid[{u, v}] = i; edgeid[{v, u}] = i; if (dsu.same(u, v)) { if (dsu.diff(u, v) != w) { vector path; auto dfs = [&](auto&& dfs, int now, int pre) -> bool { if (now == v) { path.push_back(now); return true; } for (int nxt : G[now]) { if (nxt == pre) continue; if (dfs(dfs, nxt, now)) { path.push_back(now); return true; } } return false; }; dfs(dfs, u, -1); path.push_back(u); if (dsu.diff(u, v) < w) ranges::reverse(path); cout << ssize(path) - 1 << endl; cout << u + 1 << endl; for (int i = 0; i < ssize(path) - 1; i++) { cout << edgeid[{path[i], path[i + 1]}] + 1 << ' '; } cout << endl; return 0; } } dsu.unite(u, v, w); G[u].push_back(v); G[v].push_back(u); } cout << -1 << endl; }