#include #include #define rep(i,n) for(int i=0;i vi; typedef vector vl; typedef vector> vvi; typedef vector> vvl; typedef long double ld; typedef pair P; template ostream& operator<<(ostream& os, const static_modint& a) {os << a.val(); return os;} template ostream& operator<<(ostream& os, const dynamic_modint& a) {os << a.val(); return os;} template istream& operator>>(istream& is, static_modint& a) {long long x; is >> x; a = x; return is;} template istream& operator>>(istream& is, dynamic_modint& a) {long long x; is >> x; a = x; return is;} template istream& operator>>(istream& is, vector& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;} template ostream& operator<<(ostream& os, const pair& p){os << p.first << ' ' << p.second; return os;} template ostream& operator<<(ostream& os, const vector& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;} template ostream& operator<<(ostream& os, const vector>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;} template ostream& operator<<(ostream& os, const set& se){for(T x : se) os << x << " "; os << "\n"; return os;} template ostream& operator<<(ostream& os, const unordered_set& se){for(T x : se) os << x << " "; os << "\n"; return os;} template ostream& operator<<(ostream& os, const atcoder::segtree& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;} template ostream& operator<<(ostream& os, const atcoder::lazy_segtree& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;} template void chmin(T& a, T b){a = min(a, b);} template void chmax(T& a, T b){a = max(a, b);} int main(){ int n, m; cin >> n >> m; vector>> graph(n); vector> uv; rep(i, m){ int u, v; long long c; cin >> u >> v >> c; u--; v--; graph[u].emplace_back(i + 1, v, c); graph[v].emplace_back(i + 1, u, -c); uv.emplace_back(u, v); } vector check(n); vector potential(n); vector ans; deque de; int ans_from; auto dfs = [&](auto self, int from, long long prev) -> void{ check[from] = 1; for(auto [idx, to, c] : graph[from]){ if(ans.size()) return; if(idx == prev) continue; if(check[to]){ if(potential[to] != potential[from] + c){ int pos = from; ans.push_back(idx); while(pos != to){ flush(cout); int idx = de.back(); de.pop_back(); if(uv[idx].first == pos) pos = uv[idx].second; else pos = uv[idx].first; ans.push_back(idx); } ans_from = to; if(potential[to] > potential[from] + c){ }else{ reverse(ans.begin(), ans.end()); } } }else{ potential[to] = potential[from] + c; de.push_back(idx); self(self, to, idx); if(ans.size() == int(0)) de.pop_back(); } } }; rep(i, n){ if(ans.size()) break; if(check[i]) continue; dfs(dfs, i, -1); } if(ans.size()){ cout << ans.size() << "\n"; cout << ans_from + 1 << "\n"; cout << ans; }else{ cout << "-1\n"; } return 0; }