#include <bits/stdc++.h>
using namespace std;
struct edge {
  int u, v, id;
} s[100005], m[100005];
int n, e;
int ls, lm;
int cs, cm;
int fa[1005];
bool ans[100005], sum[100005];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> e;
  if ((n - 1) % 2) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= e; i++) {
    int u, v;
    char ch;
    cin >> u >> v >> ch;
    if (ch == 'M') m[++lm] = (edge){u, v, i};
    if (ch == 'S') s[++ls] = (edge){u, v, i};
  }
  for (int i = 1; i <= lm; i++) {
    int x = find(m[i].u);
    int y = find(m[i].v);
    if (x != y) {
      fa[x] = y;
      ++cm;
    }
  }
  if (cm < (n - 1) / 2) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 1; i <= ls; i++) {
    int x = find(s[i].u);
    int y = find(s[i].v);
    if (x != y) {
      sum[i] = 1;
      fa[x] = y;
      ++cs;
    }
  }
  if (cm + cs < n - 1) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= ls; i++)
    if (sum[i]) {
      int x = find(s[i].u);
      int y = find(s[i].v);
      if (x != y) {
        fa[x] = y;
        ans[s[i].id] = 1;
      }
    }
  for (int i = 1; i <= ls && cs < (n - 1) / 2; i++) {
    if (!sum[i]) {
      int x = find(s[i].u);
      int y = find(s[i].v);
      if (x != y) {
        cs++;
        fa[x] = y;
        ans[s[i].id] = 1;
      }
    }
  }
  if (cs < (n - 1) / 2) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 1; i <= lm; i++) {
    int x = find(m[i].u);
    int y = find(m[i].v);
    if (x != y) {
      fa[x] = y;
      ans[m[i].id] = 1;
    }
  }
  cout << n - 1 << endl;
  for (int i = 1; i <= e; i++)
    if (ans[i]) cout << i << " ";
  return 0;
}
