2 0
 100000 0
 8 9
1 2
2 3
3 4
4 1
5 6
6 7
7 8
8 5
6 8
 2 1
1 2
 11 9
5 9
8 5
11 3
11 1
3 4
7 11
3 1
11 2
7 2
 7 9
7 3
7 4
1 2
2 3
3 1
3 4
4 5
5 6
6 4
 7 5
5 4
7 3
7 1
6 7
3 1
 7 8
1 2
2 3
3 4
4 1
3 5
5 6
6 7
7 3
 6 9
1 2
2 3
1 4
4 2
5 2
5 3
4 5
6 4
6 5
 7 9
1 2
2 4
4 3
3 1
2 3
4 5
5 6
6 7
7 4
 1 0
 6 7
1 2
2 3
3 1
3 4
4 5
5 6
6 4
 4 5
1 2
2 3
3 4
4 1
1 3
 100000 3
99998 99999
99999 100000
100000 99998

0

 0

 4
1 2 3 4 
 0

 6
3 4 6 7 8 9 
 9
1 2 3 4 5 6 7 8 9 
 3
2 3 5 
 8
1 2 3 4 5 6 7 8 
 0

 4
6 7 8 9 
 0

 6
1 2 3 5 6 7 
 0

 3
1 2 3 

#include <bits/stdc++.h>
using namespace std;
int read() {
  int t = 1, r = 0, c = getchar();
  while (c < '0' || c > '9') {
    t = c == '-' ? -1 : 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    r = (r << 3) + (r << 1) + (c ^ 48);
    c = getchar();
  }
  return r * t;
}
vector<int> t[1000010], nt[1000010];
int n, m, u[1000010], v[1000010], g[1000010], f[1000010], dfn[1000010],
    low[1000010], top, s[1000010], sum, ins[1000010], now, w[1000010], ans;
void dfs(int x) {
  now++;
  dfn[x] = now;
  low[x] = dfn[x];
  s[++top] = x;
  ins[x] = 1;
  for (int i = (0), _end_ = (t[x].size() - 1); i <= _end_; i++) {
    if (dfn[t[x][i]]) {
      if (ins[t[x][i]]) low[x] = min(dfn[t[x][i]], low[x]);
    } else {
      dfs(t[x][i]);
      low[x] = min(low[t[x][i]], low[x]);
    }
  }
  if (dfn[x] == low[x]) {
    sum++;
    while (s[top + 1] != x) {
      ins[s[top]] = 0;
      w[sum]++;
      f[s[top]] = sum;
      top--;
    }
  }
}
int main() {
  n = read();
  m = read();
  for (int i = (1), _end_ = (m); i <= _end_; i++) {
    u[i] = read();
    v[i] = read();
    t[u[i]].push_back(v[i]);
  }
  for (int i = (1), _end_ = (n); i <= _end_; i++)
    if (!dfn[i]) dfs(i);
  int ans = 0;
  for (int i = (1), _end_ = (m); i <= _end_; i++)
    if (f[u[i]] == f[v[i]]) ans++;
  printf("%d\n", ans);
  for (int i = (1), _end_ = (m); i <= _end_; i++)
    if (f[u[i]] == f[v[i]]) printf("%d ", i);
  putchar(10);
  return 0;
}
