#include "bits/stdc++.h" //#include "atcoder/all" using namespace std; //using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; //using mint = modint998244353; //const int mod = 998244353; const int INF = 1e9; //const long long LINF = 1e18; //const bool debug = false; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" #define P pair template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } int popcount(long long n) { n = (n & 0x5555555555555555) + (n >> 1 & 0x5555555555555555); n = (n & 0x3333333333333333) + (n >> 2 & 0x3333333333333333); n = (n & 0x0f0f0f0f0f0f0f0f) + (n >> 4 & 0x0f0f0f0f0f0f0f0f); n = (n & 0x00ff00ff00ff00ff) + (n >> 8 & 0x00ff00ff00ff00ff); n = (n & 0x0000ffff0000ffff) + (n >> 16 & 0x0000ffff0000ffff); n = (n & 0x00000000ffffffff) + (n >> 32 & 0x00000000ffffffff); return n; } struct Edge { int to, id, c; Edge(int _to, int _id, char _c) :to(_to), id(_id), c(_c) {} }; vectorg[200005]; vector

ans; void bfs2(int s, int e) { int sz = ans.size(); //cout << s << e << endl; vectord(100005, INF); queue q; q.emplace(s); d[s] = 0; while (!q.empty()) { int p = q.front(); q.pop(); for (auto e : g[p]) { int np = e.to; if (d[np] > (d[p] + 1)) { d[np] = d[p] + 1; q.emplace(np); } } } //後半復元。 //復元パート。 int np = e; int nd = d[e]; rrep(i, d[e]) { for (auto e : g[np]) { if (d[np] - 1 == d[e.to]) { ans.push_back({ e.id,e.c }); np = e.to; nd = d[e.to]; break; } } } rep(i, d[e]) { if (sz + i < ans.size() - 1 - i) { swap(ans[sz + i], ans[ans.size() - 1 - i]); } } } void bfs(int s, int c0, int c1, int e) { vectord(100005, INF); queue q; q.emplace(s); d[s] = 0; while (!q.empty()) { int p = q.front(); q.pop(); for (auto e : g[p]) { int np = e.to; if (d[np] > (d[p] + 1)) { d[np] = d[p] + 1; q.emplace(np); } } } if (d[c0] > d[c1])swap(c0, c1); //復元パート。 int np = c0; int nd = d[c0]; rrep(i, d[c0]) { for (auto e : g[np]) { if (d[np] - 1 == d[e.to]) { ans.push_back({ e.id,e.c }); np = e.to; nd = d[e.to]; break; } } } rep(i, ans.size() - 1) { if (1 + i < ans.size() - i - 1) { swap(ans[i + 1], ans[ans.size() - i - 1]); } } bfs2(c1, e); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; int bit = 0; vector, int>>e(m); rep(i, m) { int a, b; char c; cin >> a >> b >> c; a--; b--; g[a].emplace_back(b, i, c - 'a'); g[b].emplace_back(a, i, c - 'a'); bit |= 1 << (c - 'a'); e[i] = { {a,b},c - 'a' }; } if (popcount(bit) == 1) { cout << -1 << endl; return 0; } vectora(n); int s; char fc; rep(i, m) { if ((0 == e[i].first.first) || (0 == e[i].first.second)) { fc = e[i].second; if (0 != e[i].first.first)s = e[i].first.first; else s = e[i].first.second; ans.push_back({ i,e[i].second }); break; } } int c0, c1; rep(i, m) { if (fc != e[i].second) { c0 = e[i].first.first; c1 = e[i].first.second; ans.push_back({ i,e[i].second }); break; } } //cout << s << c0 << c1 << endl; bfs(s, c0, c1, n - 1); //回分なら追加 bool b = true; rep(i, ans.size()) { if (ans[i].second != ans[ans.size() - 1 - i].second) { b = false; break; } } if (b) { int sz = ans.size() - 1; ans.push_back(ans[sz]); ans.push_back(ans[sz]); } cout << ans.size() << endl; rep(i, ans.size()) { cout << ans[i].first + 1 << endl; } return 0; }