#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 20010;
int head[maxn], ip, indeg[maxn];
int n, m, seq[maxn];
struct note
{
int v,next;
} edge[maxn];
void Init()
{
memset(head, -1, sizeof(head));
memset(indeg, 0, sizeof(indeg));
ip = 0;
}
void addedge(int u, int v)
{
edge[ip].v = v;
edge[ip].next = head[u];
head[u] = ip++;
}
int topo()
{
queue<int> q;
for(int i=1; i<=n; i++)
if(indeg[i] == 0)
q.push(i);
int k = 0;
bool res = false;
while(!q.empty())
{
if(q.size() != 1)
res = true;
int u = q.front();
q.pop();
k++;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v = edge[i].v;
indeg[v]--;
if(indeg[v] == 0)
{
q.push(v);
}
}
}
if(k < n) return -1;
if(res) return 0;
return 1;
}