#include #include #include using namespace std; using ll = long long; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n,m; cin>>n>>m; vector>> g(n); vector u(m),v(m),w(m); for(int i = 0;i>u[i]>>v[i]>>w[i]; u[i]--;v[i]--; g[u[i]].push_back(make_pair(v[i],i)); g[v[i]].push_back(make_pair(u[i],i)); } vector que; vector used(m,0); vector vis(n,0); vector p(n,0); vector ans; int st; bool fn = false; auto dfs = [&](auto dfs,int ni,ll now) -> void { vis[ni]++; p[ni] = now; for(auto itr:g[ni]){ int id = itr.second; if(used[id]) continue; int to = itr.first; if(vis[to]==0){ ll nxt = now; if(u[id]==ni) nxt += w[id]; else nxt -= w[id]; used[id] = 1; que.push_back(id); dfs(dfs,to,nxt); if(fn) return; que.pop_back(); }else{ ll nxt = now; if(u[id]==ni) nxt += w[id]; else nxt -= w[id]; if(p[to]==nxt) continue; int pp = ni; while(pp!=to){ int nj = que.back(); que.pop_back(); ans.push_back(nj); if(pp==u[nj]) pp = v[nj]; else pp = u[nj]; } ans.push_back(id); st = ni; fn = true; return; } } }; for(int i = 0;i