#include using ll = long long; using std::cin; using std::cout; using std::endl; std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count()); template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const int inf = (int)1e9 + 7; const long long INF = 1LL << 60; bool palindrome(const std::string &s) { int l = 0; int r = (int)s.size() - 1; while (l < r) { if(s[l] != s[r]) return false; l += 1, r -= 1; } return true; } void solve() { int n, m; cin >> n >> m; std::vector>> g(n); std::vector> edge(26); std::vector> vp(m); std::vector c(m); for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; u -= 1, v -= 1; vp[i] = {u, v}; cin >> c[i]; g[u].emplace_back(v, i); g[v].emplace_back(u, i); edge[c[i] - 'a'].emplace_back(i); } std::vector> e(2, { -1, -1 }); std::vector eid(2, -1); for (int i = 0; i < 26; ++i) { if(edge[i].empty()) continue; if(e[0].first == -1) { eid[0] = edge[i][0]; e[0] = vp[edge[i][0]]; } else { eid[1] = edge[i][0]; e[1] = vp[edge[i][0]]; } } if(e[1].first == -1) { cout << -1 << "\n"; return; } { std::vector d(n, inf); std::queue q; q.emplace(0); d[0] = 0; while(not q.empty()) { const int cur = q.front(); q.pop(); for (const auto &[nxt, id] : g[cur]) { if(chmin(d[nxt], d[cur] + 1)) { q.emplace(nxt); } } } for (int i = 0; i < 2; i++) if(d[e[i].first] > d[e[i].second]) std::swap(e[i].first, e[i].second); if(d[e[0].first] > d[e[1].first]) { std::swap(eid[0], eid[1]); std::swap(e[0], e[1]); } } std::vector res; std::string s; { const int st = 0; std::vector d(n, inf), b(n, -1), bid(n, -1); std::queue q; q.emplace(st); d[st] = 0; while(not q.empty()) { const int cur = q.front(); q.pop(); for (const auto &[nxt, id] : g[cur]) { if(chmin(d[nxt], d[cur] + 1)) { b[nxt] = cur; bid[nxt] = id; q.emplace(nxt); } } } std::string t; std::vector ans; int to = e[0].first; while (to != st) { t += c[bid[to]]; ans.emplace_back(bid[to]); to = b[to]; } std::reverse(t.begin(), t.end()); std::reverse(ans.begin(), ans.end()); s += t; for (const auto &e : ans) res.emplace_back(e); } s += c[eid[0]]; res.emplace_back(eid[0]); { const int st = e[0].second; std::vector d(n, inf), b(n, -1), bid(n, -1); std::queue q; q.emplace(st); d[st] = 0; while(not q.empty()) { const int cur = q.front(); q.pop(); for (const auto &[nxt, id] : g[cur]) { if(chmin(d[nxt], d[cur] + 1)) { b[nxt] = cur; bid[nxt] = id; q.emplace(nxt); } } } if(d[e[1].first] > d[e[1].second]) std::swap(e[1].first, e[1].second); std::string t; std::vector ans; int to = e[1].first; while (to != st) { t += c[bid[to]]; ans.emplace_back(bid[to]); to = b[to]; } std::reverse(t.begin(), t.end()); std::reverse(ans.begin(), ans.end()); s += t; for (const auto &e : ans) res.emplace_back(e); } s += c[eid[1]]; res.emplace_back(eid[1]); { const int st = e[1].second; std::vector d(n, inf), b(n, -1), bid(n, -1); std::queue q; q.emplace(st); d[st] = 0; while(not q.empty()) { const int cur = q.front(); q.pop(); for (const auto &[nxt, id] : g[cur]) { if(chmin(d[nxt], d[cur] + 1)) { b[nxt] = cur; bid[nxt] = id; q.emplace(nxt); } } } std::string t; std::vector ans; int to = n - 1; while (to != st) { t += c[bid[to]]; ans.emplace_back(bid[to]); to = b[to]; } std::reverse(t.begin(), t.end()); std::reverse(ans.begin(), ans.end()); s += t; for (const auto &e : ans) res.emplace_back(e); } if (palindrome(s)) { s += c[g[n - 1][0].second]; s += c[g[n - 1][0].second]; res.emplace_back(g[n - 1][0].second); res.emplace_back(g[n - 1][0].second); } if((int)res.size() > n * 2) { assert(false); } cout << res.size() << "\n"; for (const auto & e : res) cout << e + 1 << "\n"; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int kkt = 1; // cin >> kkt; while(kkt--) solve(); return 0; }