#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 * 1000 + 10;
const int maxn5 = 1 * 1000 * 100 + 10;
const int maxnJ = 1000 + 10;
const long long mod = 1000 * 1000 * 1000 + 7;
const long long inf = 1LL * 2 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 10;
int par[maxn5], rnk[maxn5], a[maxn5], b[maxn5], num = 0;
vector<int> fir, edgem, edges;
bool used[maxn5];
int get_par(int a) { return (par[a] == 0 ? a : par[a] = get_par(par[a])); }
void join(int a, int b) {
  a = get_par(a);
  b = get_par(b);
  if (a == b) return;
  if (rnk[a] > rnk[b]) swap(a, b);
  if (rnk[a] == rnk[b]) rnk[b]++;
  par[a] = b;
  num--;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m, cntm = 0, cnts = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    char ty;
    cin >> a[i] >> b[i] >> ty;
    if (ty == 'M') {
      cntm++;
      edgem.push_back(i);
    } else {
      cnts++;
      edges.push_back(i);
    }
  }
  if (n % 2 == 0 or cntm < n / 2 or cnts < n / 2) return cout << -1 << endl, 0;
  num = n;
  int useds = 0;
  for (auto ind : edgem) join(a[ind], b[ind]);
  if (num - 1 > n / 2) return cout << -1 << endl, 0;
  for (auto ind : edges) {
    int x = get_par(a[ind]), y = get_par(b[ind]);
    if (x != y) {
      used[ind] = true;
      fir.push_back(ind);
      join(a[ind], b[ind]);
      useds++;
    }
  }
  if (num != 1 or useds > n / 2) return cout << -1 << endl, 0;
  fill(par, par + n + 10, 0);
  fill(rnk, rnk + n + 10, 0);
  for (auto ind : fir) join(a[ind], b[ind]);
  for (auto ind : edges)
    if (useds < n / 2 and !used[ind]) {
      int x = get_par(a[ind]), y = get_par(b[ind]);
      if (x != y) {
        used[ind] = true;
        join(x, y);
        useds++;
      }
    }
  if (useds != n / 2) return cout << -1 << endl, 0;
  for (auto ind : edgem) {
    int x = get_par(a[ind]), y = get_par(b[ind]);
    if (x != y) {
      used[ind] = true;
      join(x, y);
    }
  }
  cout << n - 1 << endl;
  for (int i = 0; i < m; i++)
    if (used[i]) cout << i + 1 << ' ';
  cout << '\n';
  return 0;
}
