#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include using namespace std; using lint = long long int; using P = pair; using PL = pair; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) #define ALL(a) (a).begin(),(a).end() constexpr int MOD = 1000000007; constexpr lint B1 = 1532834020; constexpr lint M1 = 2147482409; constexpr lint B2 = 1388622299; constexpr lint M2 = 2147478017; constexpr int INF = 2147483647; void yes(bool expr) {cout << (expr ? "Yes" : "No") << "\n";} templatevoid chmax(T &a, const T &b) { if (avoid chmin(T &a, const T &b) { if (b V; int N, M; struct Node { int idx; vector> nbr; set vst; void dfs(vector &e, int mode) { if(vst.find(mode) != vst.end()) return; vst.insert(mode); if(mode == 26 && idx == N-1) { if(e.size() > 2 * N) return; cout << e.size() << "\n"; REP(i, e.size()) cout << e[i] << "\n"; exit(0); } for(auto t : nbr) { int n = get<0>(t); int c = get<1>(t) - 'a'; int ei = get<2>(t); int nmode = c; if(mode == 26 || (mode != -1 && mode != c)) nmode = 26; e.push_back(ei); V[n].dfs(e, nmode); e.pop_back(); } }; }; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> N >> M; V = vector(N); REP(i, N) V[i].idx = i; set st; REP(i, M) { int A, B; char C; cin >> A >> B >> C; st.insert(C); A--; B--; V[A].nbr.push_back(make_tuple(B, C, i+1)); V[B].nbr.push_back(make_tuple(A, C, i+1)); } if(st.size() == 1) { cout << -1 << endl; return 0; } vector e; V[0].dfs(e, -1); }