#include #include #include using namespace std; using ll = long long; #include #include int n,m; vector>>> g; vector C; vector solve1(){ if(n==2){ int ni = 0,nj; for(int i = 0;i ans; ans.push_back(g[0][0].second.second); ans.push_back(g[0][nj].second.second); ans.push_back(g[0][nj].second.second); return ans; }else if(n==3){ vector ans; for(int i = 0;i<3;i++){ set a; set b; vector use; for(int j = 0;j now; for(auto itr:g[i]){ now.insert(itr.second.first); } if(now.size()==1) continue; wu = i; break; } vector vis(n,0); bool fn = false; vector que; auto dfs = [&](auto dfs,int ni,int to) -> void { vis[ni]++; if(ni==to){ fn = true; return; } for(auto&itr:g[ni]){ int nj = itr.first; if(vis[nj]) continue; que.push_back(itr.second.second); dfs(dfs,nj,to); if(fn) return; que.pop_back(); } }; dfs(dfs,0,wu); vector ans = que; que.clear(); int k = wu; if(wu==0){ int ni = 0; int nj; for(int j = 1;j solve2(){ char no = g[n-1][0].second.first; int wu,ni; ni = -1; int wv; for(int i = 0;i vis(n,0); bool fn = false; vector que; auto dfs = [&](auto dfs,int ni,int to) -> void { vis[ni]++; if(ni==to){ fn = true; return; } for(auto&itr:g[ni]){ int nj = itr.first; if(vis[nj]) continue; que.push_back(itr.second.second); dfs(dfs,nj,to); if(fn) return; que.pop_back(); } }; dfs(dfs,0,wu); vector ans = que; int cnt = ans.size(); que.clear(); for(int i = 0;i s; cin>>n>>m; g.resize(n); C.resize(m); vector uu(m),vv(m); for(int i = 0;i>u>>v>>c; C[i] = c; u--;v--; s.insert(c); g[u].push_back({v,{c,i+1}}); g[v].push_back({u,{c,i+1}}); uu[i] = u; vv[i] = v; } if(s.size()==1){ cout<<-1<