Problem: Mr. Endo wanted to write the code that performs breadth-first search (BFS), which is a search algorithm to explore all vertices on a directed graph. An example of pseudo code of BFS is as follows:


1: $current \leftarrow \{start\_vertex\}$
2: $visited \leftarrow current$
3: while $visited \ne$ the set of all the vertices
4:   $found \leftarrow \{\}$
5:   for $u$ in $current$
6:     for each $v$ such that there is an edge from $u$ to $v$
7:       $found \leftarrow found \cup \{v\}$
8:   $current \leftarrow found \setminus visited$
9:   $visited \leftarrow visited \cup found$


However, Mr. Endo apparently forgot to manage visited vertices in his code. More precisely, he wrote the following code:


1: $current \leftarrow \{start\_vertex\}$
2: while $current \ne$ the set of all the vertices
3:   $found \leftarrow \{\}$
4:   for $u$ in $current$
5:     for each $v$ such that there is an edge from $u$ to $v$
6:       $found \leftarrow found \cup \{v\}$
7:   $current \leftarrow found$


You may notice that for some graphs, Mr. Endo's program will not stop because it keeps running infinitely. Notice that it does not necessarily mean the program cannot explore all the vertices within finite steps. Your task here is to make a program that determines whether Mr. Endo's program will stop within finite steps for a given directed graph in order to point out the bug to him. Also, calculate the minimum number of loop iterations required for the program to stop if it is finite. Since the answer might be huge, thus print the answer modulo $10^9 +7$, which is a prime number.



Input

The input consists of a single test case formatted as follows.


$N$ $M$
$u_1$ $v_1$
:
$u_M$ $v_M$


The first line consists of two integers $N$ ($2 \leq N \leq 500$) and $M$ ($1 \leq M \leq 200,000$), where $N$ is the number of vertices and $M$ is the number of edges in a given directed graph, respectively. The $i$-th line of the following $M$ lines consists of two integers $u_i$ and $v_i$ ($1 \leq u_i, v_i \leq N$), which means there is an edge from $u_i$ to $v_i$ in the given graph. The vertex $1$ is the start vertex, i.e. $start\\_vertex$ in the pseudo codes. You can assume that the given graph also meets the following conditions.

* The graph has no self-loop, i.e., $u_i \ne v_i$ for all $1 \leq i \leq M$.
* The graph has no multi-edge, i.e., $(u_i, v_i) \le (u_j, v_j)$ for all $1 \leq i < j \leq M$.
* For each vertex $v$, there is at least one path from the start vertex $1$ to $v$.

Output

If Mr. Endo's wrong BFS code cannot stop within finite steps for the given input directed graph, print '-1' in a line. Otherwise, print the minimum number of loop iterations required to stop modulo $10^9+7$.

Examples

Input

4 4
1 2
2 3
3 4
4 1


Output

-1


Input

4 5
1 2
2 3
3 4
4 1
1 3


Output

7


Input

5 13
4 2
2 4
1 2
5 4
5 1
2 1
5 3
4 3
1 5
4 5
2 3
5 2
1 3


Output

3

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 500;

int n,m;

struct mat
{
    bitset<MAX_N> a[MAX_N];
};

mat operator*(const mat& m1,const mat& m2){
    mat ans;
    rep(i,MAX_N){
        rep(j,MAX_N){
            if(m1.a[i][j] == 0) continue;
            ans.a[i] |= m2.a[j];
        }
    }
    return ans;
}

bool possible(mat m, ll cnt)
{
    mat res;
    rep(i,n){
        res.a[i].set(i);
    }
    while(cnt){
        if(cnt & 1){
            res = res*m;
        }
        cnt >>= 1;
        m = m*m;
    }
    return ((int)res.a[0].count() == n);
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> m;
    mat A;
    rep(i,m){
        int x,y;
        cin >> x >> y;
        A.a[x-1].set(y-1);
    }
    ll l = 0, r = 50000000000LL;
    if(!possible(A,r)){
        cout << "-1\n";
        return 0;
    }
    while(r-l>1){
        ll mid = (l+r)/2;
        if(possible(A,mid)){
            r = mid;
        }else{
            l = mid;
        }
    }
    cout << r%MOD << "\n";
    return 0;
}