import sys sys.setrecursionlimit(1000006) # sys.set_int_max_str_digits(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] # inf = -1-(-1 << 31) inf = -1-(-1 << 62) # md = 10**9+7 md = 998244353 def dfs(ok,u): if ok(u):return u for v,i in to[u]: if fin[v]:continue fin[v]=1 ii.append(i) ret=dfs(ok,v) if ret!=-1:return ret ii.pop() fin[v]=0 return -1 n,m=LI() to=[[] for _ in range(n)] cc=[] for i in range(m): u,v,c=SI().split() u=int1(u) v=int1(v) c=ord(c)-97 to[u].append((v,i)) to[v].append((u,i)) cc.append(c) if all(c==cc[0] for c in cc): print(-1) exit() s,si=to[0][0] ii=[si] fin=[0]*n fin[s]=1 ok1=lambda x:cc[ii[-1]]!=cc[si] v=dfs(ok1,s) # print(ii,v) if v!=n-1: pre,ii=ii,[] fin=[0]*n fin[v]=1 ok2=lambda x:x==n-1 dfs(ok2,v) ii=pre+ii # print(ii) ee=[cc[i] for i in ii] if ee==ee[::-1]:ii=[si,si]+ii print(len(ii)) for i in ii:print(i+1)