← Home
package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

type Edge struct {
	to    int
	color int
}

func solve(n int, adj [][]Edge, target int) ([]int, bool) {
	val := make([]int, n+1)
	for i := 1; i <= n; i++ {
		val[i] = -1
	}

	queue := make([]int, 0, n)
	res := make([]int, 0, n)

	for i := 1; i <= n; i++ {
		if val[i] != -1 {
			continue
		}

		queue = queue[:0]
		queue = append(queue, i)
		val[i] = 0
		cnt0, cnt1 := 1, 0

		for q := 0; q < len(queue); q++ {
			u := queue[q]
			vu := val[u]
			for _, e := range adj[u] {
				w := vu ^ (e.color ^ target)
				if val[e.to] == -1 {
					val[e.to] = w
					if w == 0 {
						cnt0++
					} else {
						cnt1++
					}
					queue = append(queue, e.to)
				} else if val[e.to] != w {
					return nil, false
				}
			}
		}

		if cnt1 <= cnt0 {
			for _, v := range queue {
				if val[v] == 1 {
					res = append(res, v)
				}
			}
		} else {
			for _, v := range queue {
				if val[v] == 0 {
					res = append(res, v)
				}
			}
		}
	}

	return res, true
}

func main() {
	data, _ := io.ReadAll(os.Stdin)
	idx := 0

	nextInt := func() int {
		for idx < len(data) && data[idx] <= ' ' {
			idx++
		}
		sign := 1
		if data[idx] == '-' {
			sign = -1
			idx++
		}
		num := 0
		for idx < len(data) && data[idx] > ' ' {
			num = num*10 + int(data[idx]-'0')
			idx++
		}
		return num * sign
	}

	nextByte := func() byte {
		for idx < len(data) && data[idx] <= ' ' {
			idx++
		}
		b := data[idx]
		idx++
		return b
	}

	n := nextInt()
	m := nextInt()

	adj := make([][]Edge, n+1)
	for i := 0; i < m; i++ {
		u := nextInt()
		v := nextInt()
		c := nextByte()
		col := 0
		if c == 'B' {
			col = 1
		}
		adj[u] = append(adj[u], Edge{to: v, color: col})
		adj[v] = append(adj[v], Edge{to: u, color: col})
	}

	resR, okR := solve(n, adj, 0)
	resB, okB := solve(n, adj, 1)

	out := bufio.NewWriterSize(os.Stdout, 1<<20)
	defer out.Flush()

	if !okR && !okB {
		fmt.Fprintln(out, -1)
		return
	}

	var ans []int
	if okR && (!okB || len(resR) <= len(resB)) {
		ans = resR
	} else {
		ans = resB
	}

	fmt.Fprintln(out, len(ans))
	for i, v := range ans {
		if i > 0 {
			fmt.Fprint(out, " ")
		}
		fmt.Fprint(out, v)
	}
	if len(ans) > 0 {
		fmt.Fprintln(out)
	}
}