#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; template struct Edge { int src, dst; CostType cost; Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {} inline bool operator<(const Edge &x) const { return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src; } inline bool operator<=(const Edge &x) const { return !(x < *this); } inline bool operator>(const Edge &x) const { return x < *this; } inline bool operator>=(const Edge &x) const { return !(*this < x); } }; int main() { int n, m; cin >> n >> m; vector a(m), b(m); vector c(m); vector> graph(n); REP(i, m) { cin >> a[i] >> b[i] >> c[i]; --a[i]; --b[i]; graph[a[i]].emplace_back(i); graph[b[i]].emplace_back(i); } if (set(ALL(c)).size() == 1) { cout << "-1\n"; return 0; } vector prev(n, -1); prev[0] = -2; queue que({0}); while (!que.empty()) { int ver = que.front(); que.pop(); for (int id : graph[ver]) { const int dst = (a[id] == ver ? b : a)[id]; if (prev[dst] == -1) { prev[dst] = id; que.emplace(dst); } } } vector ans; string s = ""; int cur = n - 1; while (cur != 0) { ans.emplace_back(prev[cur]); s += c[prev[cur]]; cur = (a[prev[cur]] == cur ? b : a)[prev[cur]]; } reverse(ALL(ans)); reverse(ALL(s)); if (equal(ALL(s), s.rbegin())) { if (set(ALL(s)).size() > 1) { ans.resize(n * 2, ans.back()); } else { const char ch = s.front(); fill(ALL(prev), -1); for (int id : ans) prev[a[id]] = prev[b[id]] = -2; REP(i, n) que.emplace(i); cur = -1; while (!que.empty()) { int ver = que.front(); que.pop(); for (int id : graph[ver]) { const int dst = (a[id] == ver ? b : a)[id]; if (prev[dst] == -1) { prev[dst] = id; que.emplace(dst); if (cur == -1 && c[id] != ch) cur = dst; } } } assert(cur != -1); vector sub_ans; string sub_s = ""; while (prev[cur] != -2) { sub_ans.emplace_back(prev[cur]); sub_s += c[prev[cur]]; cur = (a[prev[cur]] == cur ? b : a)[prev[cur]]; } reverse(ALL(sub_ans)); reverse(ALL(sub_s)); for (int i = static_cast(sub_ans.size()) - 1; i >= 0; --i) { sub_ans.emplace_back(sub_ans[i]); sub_s += sub_s[i]; } if (cur == 0) { ans.insert(ans.begin(), ALL(sub_ans)); s.insert(s.begin(), ALL(sub_s)); } else { int pos = 0; REP(i, ans.size()) { pos = (a[ans[i]] == pos ? b : a)[ans[i]]; if (pos == cur) { ans.insert(ans.begin() + (i + 1), ALL(sub_ans)); s.insert(s.begin() + (i + 1), ALL(sub_s)); break; } } } if (equal(ALL(s), s.rbegin())) ans.resize(n * 2, ans.back()); } } const int k = ans.size(); cout << k << '\n'; REP(i, k) cout << ans[i] + 1 << '\n'; return 0; }