Road Repairs

A country named Berland has n cities. They are numbered with integers from 1 to n. City with index 1 is the capital of the country. Some pairs of cities have monodirectional roads built between them. However, not all of them are in good condition. For each road we know whether it needs repairing or not. If a road needs repairing, then it is forbidden to use it. However, the Berland government can repair the road so that it can be used.

Right now Berland is being threatened by the war with the neighbouring state. So the capital officials decided to send a military squad to each city. The squads can move only along the existing roads, as there’s no time or money to build new roads. However, some roads will probably have to be repaired in order to get to some cities.

Of course the country needs much resources to defeat the enemy, so you want to be careful with what you’re going to throw the forces on. That’s why the Berland government wants to repair the minimum number of roads that is enough for the military troops to get to any city from the capital, driving along good or repaired roads. Your task is to help the Berland government and to find out, which roads need to be repaired.

Input

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the number of cities and the number of roads in Berland.

Next m lines contain three space-separated integers a i, b i, c i (1 ≤ a i, b i ≤ n, a i ≠ b i, 0 ≤ c i ≤ 1), describing the road from city a i to city b i. If c i equals 0, than the given road is in a good condition. If c i equals 1, then it needs to be repaired.

It is guaranteed that there is not more than one road between the cities in each direction.

Output

If even after all roads are repaired, it is still impossible to get to some city from the capital, print  - 1. Otherwise, on the first line print the minimum number of roads that need to be repaired, and on the second line print the numbers of these roads, separated by single spaces.

The roads are numbered starting from 1 in the order, in which they are given in the input.

If there are multiple sets, consisting of the minimum number of roads to repair to make travelling to any city from the capital possible, print any of them.

If it is possible to reach any city, driving along the roads that already are in a good condition, print 0 in the only output line.

Examples

input

3 2
1 3 0
3 2 1

output

1
2

input

4 4
2 3 0
3 4 0
4 1 0
4 2 1

output

-1

input

4 3
1 2 0
1 3 0
1 4 0

output

0

Solution:

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <memory.h>
#include <string>
#include <sstream>

using namespace std;

const int N = 444444;

int n, m, i, j, e, kw, kc, ii;
int ss[N], ff[N], dd[N], cs[N], cf[N], cd[N], pred[N], last[N], x[N], was[N], c[N], w[N], deg[N], r[N];

void rec(int v) {
was[v] = 1;
int j = last[v];
while (j > 0) {
if (!was]) rec(cf[j]);
j = pred[j];
}
w[kw++] = v;
}

void go(int v) {
c[v] = kc;
int j = last[v];
while (j > 0) {
if (!c[cs[j]]) go(cs[j]);
j = pred[j];
}
}

int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d %d", &n, &m);
for (i=1;i<=m;i++) scanf("%d %d %d", &ss[i], &ff[i], &dd[i]);
  kc = n;
  for (i=1;i<=n;i++) c[i] = i;
  for (i=1;i<=m;i++) { cs[i] = ss[i]; cf[i] = ff[i]; cd[i] = dd[i]; }
  int root = 1, cnt = 0;
  while (1) {
    memset(last, 0, sizeof(last));
    for (i=1;i<=m;i++)
      if (!cd[i]) {
        pred[i] = last[cs[i]];
        last[cs[i]] = i;
      }
    memset(was, 0, sizeof(was));
    kw = 0;
    for (i=1;i<=kc;i++)
      if (!was[i]) rec(i);
    memset(last, 0, sizeof(last));
    for (i=1;i<=m;i++)
      if (!cd[i]) {
        pred[i] = last];
        last] = i;
      }
    memset(c, 0, sizeof(c));
    int old = kc;
    kc = 0;
    for (ii=kw-1;ii>=0;ii--)
if (!c[w[ii]]) {
kc++;
go(w[ii]);
}
if (kc == old) cnt++;
if (cnt > 2) {
printf("%d\n", -1);
return 0;
}
memset(last, 0, sizeof(last));
for (i=1;i<=m;i++) {
      cs[i] = c[cs[i]];
      cf[i] = c];
      if (cs[i] != cf[i]) {
        pred[i] = last[cs[i]];
        last[cs[i]] = i;
      }
    }
    memset(deg, 0, sizeof(deg));
    for (i=1;i<=m;i++)
      if (cs[i] != cf[i] && !cd[i] && cf[i] != c[root]) deg] = 1;
    int ok = 1;
    for (i=1;i<=kc;i++)
      if (deg[i] == 0 && i != c[root]) {
        ok = 0;
        break;
      }
    if (ok) break;
    for (i=1;i<=m;i++)
      if (cs[i] != cf[i])
        if (deg] == 0) cd[i] = 0;
    root = c[root];
  }
  memset(last, 0, sizeof(last));
  for (i=1;i<=m;i++) {
    if (cd[i]) continue;
    pred[i] = last[ss[i]];
    last[ss[i]] = i;
  }
  memset(was, 0, sizeof(was));
  int kr = 0;
  i = 1; e = 1;
  x[1] = 1;
  was[1] = 1;
  while (i <= e) {
    j = last[x[i]];
    while (j > 0) {
if (!was[ff[j]]) {
e++;
x[e] = ff[j];
if (dd[j]) r[kr++] = j;
was[x[e]] = 1;
}
j = pred[j];
}
i++;
}
if (kr > 0) sort(r, r+kr);
if (e < n) {
    printf("%d\n", -1);
    return 0;
  }
  printf("%d\n", kr);
  for (i=0;i<kr-1;i++) printf("%d ", r[i]);
  if (kr > 0) printf("%d", r[kr-1]);
printf("\n");
return 0;
}