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;
class node {
 public:
  int parent = -1;
  int f = -1;
  int d = -1;
};
vector<pair<int, int> > cycles;
map<pair<int, int>, int> m1;
class graph {
 public:
  int V;
  int E;
  list<int> *adj;
  graph(int v, int e) {
    this->V = v;
    this->E = e;
    adj = new list<int>[V];
  }
  void addEdge(int u, int v) { adj[u].push_back(v); }
};
void count_cycles(node **nodes) {
  vector<int> edges;
  int n = cycles.size();
  for (int i = 0; i < n; i++) {
    int s = cycles[i].first;
    int f = cycles[i].second;
    int p = f;
    edges.push_back(m1[{f, s}]);
    while (p != s) {
      edges.push_back(m1[{nodes[p]->parent, p}]);
      p = nodes[p]->parent;
    }
  }
  int l = edges.size();
  sort(edges.begin(), edges.end());
  for (int i = 0; i < l; i++) {
    cout << edges[i] << " ";
  }
  cout << endl;
  return;
}
void dfs_util(graph g, node **nodes, int i, int &time, int &start, int &finish,
              bool &flag) {
  nodes[i]->d = time;
  list<int>::iterator it;
  for (it = g.adj[i].begin(); it != g.adj[i].end(); it++) {
    nodes[*it]->parent = i;
    if (nodes[*it]->d != -1 && nodes[*it]->f == -1) {
      start = *it;
      finish = i;
      cycles.push_back({start, finish});
    } else if (nodes[*it]->d == -1)
      dfs_util(g, nodes, *it, ++time, start, finish, flag);
  }
  nodes[i]->f = ++time;
}
void dfs(graph g) {
  int v = g.V;
  node *nodes[v];
  for (int i = 0; i < v; i++) {
    nodes[i] = new node();
  }
  int s = -1;
  int f = -1;
  int time = 1;
  bool flag = false;
  dfs_util(g, nodes, 0, time, s, f, flag);
  if (cycles.size() == 0) {
    cout << 0 << endl;
  } else {
    count_cycles(nodes);
  }
  return;
}
int main() {
  int n, m;
  cin >> n >> m;
  graph g(n, m);
  int a, b;
  int k = 1;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    g.addEdge(a - 1, b - 1);
    m1[{a - 1, b - 1}] = k;
    k++;
  }
  dfs(g);
  return 0;
}
