#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e3 + 5;
const int MAXM = 1e5 + 5;
int n, m, tot;
int fa[MAXN];
bool in[MAXM];
struct Edge {
  int u, v, id, val;
  bool operator<(const Edge &a) const { return val < a.val; }
} edge[MAXM];
int find_fa(int x) {
  if (fa[x] == x) return x;
  return fa[x] = find_fa(fa[x]);
}
void print() {
  printf("%d\n", n - 1);
  for (int i = 1; i <= m; i++)
    if (in[i]) printf("%d ", i);
  exit(0);
}
int main() {
  int u, v, val, id;
  char op[5];
  scanf("%d%d", &n, &m);
  if (n == 1) {
    printf("0");
    return 0;
  }
  if (!(n & 1)) {
    printf("-1");
    return 0;
  }
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%s", &edge[i].u, &edge[i].v, op);
    edge[i].id = i;
    edge[i].val = (op[0] == 'M' ? 0 : 1);
  }
  sort(edge + 1, edge + m + 1);
  for (int i = 1; i <= m; i++) {
    u = edge[i].u, v = edge[i].v, id = edge[i].id, val = edge[i].val;
    if (find_fa(u) != find_fa(v)) {
      fa[find_fa(u)] = find_fa(v);
      if (val == 1) tot++, in[id] = 1;
    }
  }
  if (tot > (n - 1) / 2) {
    printf("-1");
    return 0;
  }
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++)
    if (in[edge[i].id]) fa[find_fa(edge[i].u)] = find_fa(edge[i].v);
  for (int i = 1; i <= m; i++) {
    if (tot == (n - 1) / 2) break;
    u = edge[i].u, v = edge[i].v, id = edge[i].id, val = edge[i].val;
    if (!val || in[id]) continue;
    if (find_fa(u) != find_fa(v)) {
      fa[find_fa(u)] = find_fa(v);
      tot++, in[id] = 1;
    }
  }
  for (int i = 1; i <= m; i++) {
    u = edge[i].u, v = edge[i].v, id = edge[i].id, val = edge[i].val;
    if (val || in[id]) continue;
    if (find_fa(u) != find_fa(v)) {
      fa[find_fa(u)] = find_fa(v);
      in[id] = 1;
    }
  }
  if (tot < (n - 1) / 2) {
    printf("-1");
    return 0;
  }
  print();
}
