#include #define rep(i, n) for(long long i = 0; i < n; i++) #define ALL(v) (v).begin(), (v).end() using namespace std; using lint = long long; template struct WeightedUnionFind { private: int n; vector par, par2, dep; vector diff_weight; public: WeightedUnionFind() {} WeightedUnionFind(int n_) : n(n_), par(n, -1), par2(n, -1), dep(n, 0), diff_weight(n, T(0)) {} int leader(int x) { assert(0 <= x && x < n); if (par[x] < 0) { return x; } else { int r = leader(par[x]); diff_weight[x] += diff_weight[par[x]]; return par[x] = r; } } T weight(int x) { assert(0 <= x && x < n); leader(x); return diff_weight[x]; } T diff(int x, int y) { assert(0 <= x && x < n); assert(0 <= y && y < n); return weight(y) - weight(x); } int merge(int a, int b, T w) { assert(0 <= a && a < n); assert(0 <= b && b < n); w += weight(a); w -= weight(b); int x = leader(a); int y = leader(b); if (x == y) { return x; } if (-par[x] < -par[y]) { swap(x, y); w *= T(-1); } par[x] += par[y]; par[y] = x; diff_weight[y] = w; par2[b] = a; dep[b] = dep[a] + 1; return x; } bool same(int x, int y) { assert(0 <= x && x < n); assert(0 <= y && y < n); return leader(x) == leader(y); } int size(int x) { assert(0 <= x && x < n); return -par[leader(x)]; } vector route(int a, int b) { assert(0 <= a && a < n); assert(0 <= b && b < n); int x = (dep[a] < dep[b] ? a : b); int y = (dep[a] > dep[b] ? a : b); vector res; int cur = y; while (cur != x) { res.emplace_back(cur); cur = par2[cur]; } res.emplace_back(x); if (res[0] != a) { reverse(res.begin(), res.end()); } return res; } vector> groups() { vector> member(n); for (int i = 0; i < n; i++) { member[leader(i)].emplace_back(i); } vector> res; for (int i = 0; i < n; i++) { if (member[i].size() > 0) { res.emplace_back(member[i]); } } return res; } }; int main() { int n, m; cin >> n >> m; WeightedUnionFind uf(n); map, int> es; rep(i, m) { int u, v; lint w; cin >> u >> v >> w; u--; v--; if (!uf.same(u, v)) { uf.merge(v, u, w); } else if (uf.diff(v, u) != w) { auto r = (uf.diff(v, u) > w ? uf.route(u, v) : uf.route(v, u)); int sz = r.size(); cout << sz << endl; cout << (uf.diff(v, u) > w ? u + 1 : v + 1) << endl; rep(j, sz - 1) { cout << es[{r[j], r[j + 1]}] + 1 << " "; } cout << i + 1 << endl; return 0; } es[{u, v}] = es[{v, u}] = i; } cout << -1 << endl; }