Graph Coloring

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 ≤ nu i ≠ v i) — the indices of the vertices connected by the i-th edge, and a character c i () 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

Solution:

#include <bits/stdc++.h>

using namespace std;

const int N = 400010;

bool was[N];
int color[N];
vector < pair <int, int> > g[N];
vector <int> comp;

void go(int v, int into, int root) {
was[v] = true;
color[v] = root;
comp.push_back(v);
int sz = g[v].size();
for (int j = 0; j < sz; j++) {
    int u = g[v][j].first;
    if (was[u]) {
      continue;
    }
    int w = g[v][j].second;
    int new_root = into ^ root ^ w;
    go(u, into, new_root);
  }
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    g[i].clear();
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    x--; y--;
    char z = getchar();
    while (z != 'B' && z != 'R') {
      z = getchar();
    }
    g[x].push_back(make_pair(y, z == 'B'));
    g[y].push_back(make_pair(x, z == 'B'));
  }
  vector <int> best_res;
bool any_res = false;
for (int into = 0; into < 2; into++) {
    for (int i = 0; i < n; i++) {
      was[i] = false;
    }
    bool bad = false;
    vector <int> res;
for (int i = 0; i < n; i++) {
      if (was[i]) {
        continue;
      }
      vector <int> best;
bool any = false;
for (int root = 0; root < 2; root++) {
        comp.clear();
        go(i, into, root);
        bool ok = true;
        int z = -1;
        for (int j = 0; j < (int) comp.size(); j++) {
          int u = comp[j];
          for (int q = 0; q < (int) g[u].size(); q++) {
            int v = g[u][q].first;
            int w = g[u][q].second;
            int cur = color[u] ^ color[v] ^ w;
            if (z == -1) {
              z = cur;
            } else {
              if (cur != z) {
                ok = false;
                break;
              }
            }
          }
        }
        if (ok) {
          vector <int> cand;
for (int j = 0; j < (int) comp.size(); j++) {
            int u = comp[j];
            if (color[u]) {
              cand.push_back(u);
            }
          }
          if (!any || cand.size() < best.size()) {
            best = cand;
            any = true;
          }
        }
        if (root == 0) {
          for (int j = 0; j < (int) comp.size(); j++) {
            int u = comp[j];
            was[u] = false;
          }
        }
      }
      if (!any) {
        bad = true;
        break;
      }
      for (int j = 0; j < (int) best.size(); j++) {
        res.push_back(best[j]);
      }
    }
    if (!bad) {
      if (!any_res || res.size() < best_res.size()) {
        best_res = res;
        any_res = true;
      }
    }
  }
  if (!any_res) {
    puts("-1");
    return 0;
  }
  sort(best_res.begin(), best_res.end());
  int sz = best_res.size();
  printf("%d\n", sz);
  for (int i = 0; i < sz; i++) {
    if (i > 0) {
putchar(' ');
}
printf("%d", best_res[i] + 1);
}
printf("\n");
return 0;
}