#include using namespace std; void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } int main() { fast_io(); int n, m; cin >> n >> m; vector>> g(n); for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; u--, v--; g[u].push_back({v, w, i}); g[v].push_back({u, -w, i}); } vector vis(n); vector potential(n); vector> par(n); for (int i = 0; i < n; i++) { if (vis[i]) { continue; } deque dq; dq.push_back(i); vis[i] = true; par[i] = {-1, -1}; while (!dq.empty()) { int u = dq.front(); dq.pop_front(); for (auto [v, w, i] : g[u]) { if (!vis[v]) { vis[v] = true; par[v] = {u, i}; potential[v] = potential[u] + w; dq.push_back(v); continue; } if (potential[v] == potential[u] + w) { continue; } if (potential[v] < potential[u] + w) { swap(u, v); } vector path_u, path_v; int x = u, y = v; while (x != -1) { path_u.push_back(par[x].second); x = par[x].first; } while (y != -1) { path_v.push_back(par[y].second); y = par[y].first; } while (!path_u.empty() && !path_v.empty() && path_u.back() == path_v.back()) { path_u.pop_back(); path_v.pop_back(); } vector ans = path_u; reverse(path_v.begin(), path_v.end()); ans.insert(ans.end(), path_v.begin(), path_v.end()); ans.push_back(i); int M = ans.size(); cout << M << endl; cout << u + 1 << endl; for (int i = 0; i < M; i++) { cout << ans[i] << (i + 1 == M ? "\n" : " "); } return 0; } } } cout << -1 << endl; }