#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); for (int i = 0; i < M; i++) { int u, v, w; cin >> u >> v >> w; u--; v--; 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) return true; for (auto [nxt, id] : G[now]) { if (nxt == pre) continue; if (dfs(dfs, nxt, now)) { path.push_back(id); return true; } } return false; }; dfs(dfs, u, -1); path.push_back(i); if (dsu.diff(u, v) > w) ranges::reverse(path); cout << ssize(path) << endl; cout << v + 1 << endl; for (int x : path) cout << x + 1 << ' '; cout << endl; return 0; } } dsu.unite(u, v, w); G[u].push_back({v, i}); G[v].push_back({u, i}); } cout << -1 << endl; }