#include #include #include #include using namespace std; int N,M; vector > >G[1<<17]; int p1[1<<17],pN[1<<17]; int e1[1<<17],eN[1<<17]; int U1[1<<17][26],V1[1<<17][26],E1[1<<17][26]; int UN[1<<17][26],VN[1<<17][26],EN[1<<17][26]; main() { cin>>N>>M; for(int i=1;i<=M;i++) { int u,v;char c;cin>>u>>v>>c; G[u].push_back(make_pair(v,make_pair(i,c))); G[v].push_back(make_pair(u,make_pair(i,c))); } { queueP; P.push(1); vectord(N+1,1e9); d[1]=0; p1[1]=0; e1[1]=0; while(!P.empty()) { int u=P.front(); P.pop(); for(pair >e:G[u]) { int v=e.first; char c=e.second.second; int nd=d[u]+1; U1[nd][c-'a']=u; V1[nd][c-'a']=v; E1[nd][c-'a']=e.second.first; if(d[v]>nd) { d[v]=nd; p1[v]=u; e1[v]=e.second.first; P.push(v); } } } } { queueP; P.push(N); vectord(N+1,1e9); d[N]=0; pN[N]=0; eN[N]=0; while(!P.empty()) { int u=P.front(); P.pop(); for(pair >e:G[u]) { int v=e.first; char c=e.second.second; int nd=d[u]+1; UN[nd][c-'a']=u; VN[nd][c-'a']=v; EN[nd][c-'a']=e.second.first; if(d[v]>nd) { d[v]=nd; pN[v]=u; eN[v]=e.second.first; P.push(v); } } } } for(int d=1;d<=N;d++) { for(int i=0;i<26;i++)if(U1[d][i]) { for(int j=0;j<26;j++)if(i!=j&&UN[d][j]) { vectorans; { vectorD(N+1,1e9),p(N+1),E(N+1); D[V1[d][i]]=0; queueP; P.push(V1[d][i]); while(!P.empty()) { int u=P.front();P.pop(); for(pair >e:G[u]) { int v=e.first; int ei=e.second.first; if(D[v]>D[u]+1) { D[v]=D[u]+1; p[v]=u; E[v]=ei; P.push(v); } } } int v=VN[d][j]; int u=V1[d][i]; while(v!=u) { ans.push_back(E[v]); v=p[v]; } } { ans.push_back(E1[d][i]); int u=U1[d][i]; while(p1[u]) { ans.push_back(e1[u]); u=p1[u]; } } reverse(ans.begin(),ans.end()); { ans.push_back(EN[d][j]); int u=UN[d][j]; while(pN[u]) { ans.push_back(eN[u]); u=pN[u]; } } cout<