You are given an undirected graph that consists of n vertices and m edges. Initially, each edge is colored either red or blue. Each turn a player picks a single vertex and switches the color of all edges incident to it. That is, all red edges with an endpoint in this vertex change the color to blue, while all blue edges with an endpoint in this vertex change the color to red.

Find the minimum possible number of moves required to make the colors of all edges equal.


-----Input-----

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) — the number of vertices and edges, respectively.

The following m lines provide the description of the edges, as the i-th of them contains two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n, u_{i} ≠ v_{i}) — the indices of the vertices connected by the i-th edge, and a character c_{i} ($c_{i} \in \{B, R \}$) providing the initial color of this edge. If c_{i} equals 'R', then this edge is initially colored red. Otherwise, c_{i} is equal to 'B' and this edge is initially colored blue. It's guaranteed that there are no self-loops and multiple edges.


-----Output-----

If there is no way to make the colors of all edges equal output  - 1 in the only line of the output. Otherwise first output k — the minimum number of moves required to achieve the goal, then output k integers a_1, a_2, ..., a_{k}, where a_{i} is equal to the index of the vertex that should be used at the i-th move.

If there are multiple optimal sequences of moves, output any of them.


-----Examples-----
Input
3 3
1 2 B
3 1 R
3 2 B

Output
1
2 

Input
6 5
1 3 R
2 3 R
3 4 B
4 5 R
4 6 R

Output
2
3 4 

Input
4 5
1 2 R
1 3 R
2 3 B
3 4 B
1 4 B

Output
-1

Source: codeforces | Difficulty: HARD
Tags: Graph algorithms

Graph traversal


Solutions:
from collections import deque
(n, m) = map(int, input().split())
adj = [[] for i in range(n)]
for i in range(m):
	(u, v, c) = input().split()
	(u, v) = (int(u) - 1, int(v) - 1)
	adj[u].append((v, c))
	adj[v].append((u, c))
visited = S = T = None

def bfs(i, k):
	q = deque([(i, 0)])
	while q:
		(u, p) = q.pop()
		if visited[u] >= 0:
			if visited[u] == p:
				continue
			else:
				return False
		visited[u] = p
		if p:
			S.append(u)
		else:
			T.append(u)
		for (v, c) in adj[u]:
			nxt = p if c == k else p ^ 1
			q.appendleft((v, nxt))
	return True

def solve(k):
	global visited, S, T
	visited = [-1] * n
	res = []
	for i in range(n):
		if visited[i] < 0:
			(S, T) = ([], [])
			if not bfs(i, k):
				return [0] * (n + 1)
			else:
				res.extend(S if len(S) < len(T) else T)
	return res
res1 = solve('R')
res2 = solve('B')
if min(len(res1), len(res2)) > n:
	print(-1)
else:
	print(min(len(res1), len(res2)))
	print(' '.join(map(lambda x: str(x + 1), res1 if len(res1) < len(res2) else res2)))