#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 30;
int Par[N], cnt_S, cnt_M, n, m;
struct Edge {
  int v, u;
  char col;
  bool mark;
} E[N];
int Find(int x) { return (Par[x] == x ? x : Par[x] = Find(Par[x])); }
bool Union(int e) {
  int x = Find(E[e].v), y = Find(E[e].u);
  if (x == y) return 0;
  Par[x] = y;
  return 1;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) cin >> E[i].v >> E[i].u >> E[i].col;
  for (int i = 0; i < N; i++) Par[i] = i;
  for (int i = 0; i < m; i++)
    if (E[i].col == 'M') Union(i);
  for (int i = 0; i < m; i++)
    if (E[i].col == 'S' && Union(i)) E[i].mark = true, cnt_S++;
  for (int i = 0; i < N; i++) Par[i] = i;
  for (int i = 0; i < m; i++)
    if (E[i].mark) Union(i);
  for (int i = 0; i < m; i++)
    if (cnt_S != (n - 1) >> 1 && E[i].col == 'S' && Union(i))
      E[i].mark = true, cnt_S++;
  for (int i = 0; i < m; i++)
    if (E[i].col == 'M' && Union(i)) E[i].mark = true, cnt_M++;
  if (cnt_S != (n - 1) >> 1 || cnt_M != (n - 1) >> 1 || (n - 1) & 1)
    return cout << "-1\n", 0;
  cout << n - 1 << '\n';
  for (int i = 0; i < m; i++)
    if (E[i].mark) cout << i + 1 << ' ';
  return (0);
}
