#include <bits/stdc++.h>
using namespace std;
int fa[1050], used[100500], u[100500], v[100500];
char k[100500];
int n, m;
int Find(int u) { return fa[u] == u ? u : fa[u] = Find(fa[u]); }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) fa[i] = i;
  int num = 0;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d %c", &u[i], &v[i], &k[i]);
    int x = Find(u[i]);
    int y = Find(v[i]);
    if (x != y) {
      fa[x] = y;
      used[i] = 1;
      if (k[i] == 'S') num++;
    }
  }
  if (n == 1) {
    printf("0\n");
    return 0;
  }
  if ((n - 1) % 2) {
    printf("-1\n");
    return 0;
  }
  if (num * 2 + 1 != n) {
    for (int i = 1; i <= n; i++) fa[i] = i;
    if (num * 2 + 1 < n) {
      for (int i = 1; i <= m; i++)
        if (used[i] && k[i] == 'S')
          fa[Find(u[i])] = Find(v[i]);
        else
          used[i] = 0;
      for (int i = 1; i <= m; i++)
        if (!used[i] && k[i] == 'S') {
          int x = Find(u[i]);
          int y = Find(v[i]);
          if (x == y) continue;
          fa[x] = y;
          used[i] = 1;
          num++;
          if (2 * num + 1 == n) break;
        }
      for (int i = 1; i <= m; i++)
        if (!used[i] && k[i] == 'M') {
          int x = Find(u[i]);
          int y = Find(v[i]);
          if (x != y) {
            fa[x] = y;
            used[i] = 1;
          }
        }
    } else {
      for (int i = 1; i <= m; i++)
        if (used[i] && k[i] == 'M')
          fa[Find(u[i])] = Find(v[i]);
        else
          used[i] = 0;
      for (int i = 1; i <= m; i++)
        if (!used[i] && k[i] == 'M') {
          int x = Find(u[i]);
          int y = Find(v[i]);
          if (x == y) continue;
          fa[x] = y;
          used[i] = 1;
          num--;
          if (2 * num + 1 == n) break;
        }
      for (int i = 1; i <= m; i++)
        if (!used[i] && k[i] == 'S') {
          int x = Find(u[i]);
          int y = Find(v[i]);
          if (x != y) {
            fa[x] = y;
            used[i] = 1;
          }
        }
    }
  }
  if (2 * num + 1 == n) {
    printf("%d\n", n - 1);
    int z = 0;
    for (int i = 1; i <= m; i++)
      if (used[i]) {
        printf("%d", i);
        z++;
        if (z < n - 1)
          printf(" ");
        else
          printf("\n");
      }
  } else
    printf("-1\n");
  return 0;
}
