Today Pari gave Arya a cool graph problem. Arya wrote a non-optimal solution for it, because he believes in his ability to optimize non-optimal solutions. In addition to being non-optimal, his code was buggy and he tried a lot to optimize it, so the code also became dirty! He keeps getting Time Limit Exceeds and he is disappointed. Suddenly a bright idea came to his mind!

Here is how his dirty code looks like:
    
    
      
    dfs(v)  
    {  
         set count[v] = count[v] + 1  
         if(count[v] < 1000)  
         {  
              foreach u in neighbors[v]  
              {  
                   if(visited[u] is equal to false)  
                   {  
                        dfs(u)  
                   }  
                   break  
              }  
         }  
         set visited[v] = true  
    }  
      
    main()  
    {  
         input the digraph()  
         TOF()  
         foreach 1<=i<=n  
         {  
              set count[i] = 0 , visited[i] = false  
         }  
         foreach 1 <= v <= n  
         {  
              if(visited[v] is equal to false)  
              {  
                   dfs(v)  
              }  
         }  
         ... // And do something cool and magical but we can't tell you what!  
    }  
    

He asks you to write the TOF function in order to optimize the running time of the code with minimizing the number of calls of the dfs function. The input is a directed graph and in the TOF function you have to rearrange the edges of the graph in the list neighbors for each vertex. The number of calls of dfs function depends on the arrangement of neighbors of each vertex.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 5000) — the number of vertices and then number of directed edges in the input graph.

Each of the next m lines contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n), meaning there is a directed <image> edge in the input graph. 

You may assume that the graph won't contain any self-loops and there is at most one edge between any unordered pair of vertices.

Output

Print a single integer — the minimum possible number of dfs calls that can be achieved with permuting the edges.

Examples

Input

3 3
1 2
2 3
3 1


Output

2998


Input

6 7
1 2
2 3
3 1
3 4
4 5
5 6
6 4


Output

3001
#include <bits/stdc++.h>
const int MAX_N = 5123;
const int inf = (int)1e9 + 123;
using namespace std;
int n, m;
vector<int> g[MAX_N], first[MAX_N];
vector<int> st;
bool u[MAX_N];
int cl[MAX_N];
vector<int> v[MAX_N], q[MAX_N];
void dfs(int v) {
  u[v] = 1;
  for (auto to : g[v])
    if (!u[to]) dfs(to);
  st.push_back(v);
}
void go(int now, int color) {
  cl[now] = color;
  v[color].push_back(now);
  for (auto to : first[now])
    if (!cl[to]) go(to, color);
}
int dist[MAX_N];
queue<int> Q;
int calc(int v) {
  int color = cl[v];
  Q.push(v);
  for (int i = 1; i <= n; i++) dist[i] = inf;
  dist[v] = 0;
  int res = inf;
  while (!Q.empty()) {
    int now = Q.front();
    for (auto to : g[now])
      if (to == v) res = min(res, dist[now] + 1);
    Q.pop();
    for (auto to : g[now])
      if (cl[to] == color && dist[to] > dist[now] + 1) {
        dist[to] = dist[now] + 1;
        Q.push(to);
      }
  }
  return res;
}
int bad, cycle;
int main() {
  ios_base ::sync_with_stdio(NULL), cin.tie(NULL), cout.tie(NULL);
  cin >> n >> m;
  for (int i = 1, u, v; i <= m; i++) {
    cin >> u >> v;
    g[u].push_back(v);
    first[v].push_back(u);
  }
  for (int i = 1; i <= n; i++)
    if (!u[i]) dfs(i);
  reverse(st.begin(), st.end());
  for (auto i : st)
    if (!cl[i]) go(i, i);
  for (int i = 1; i <= n; i++)
    for (auto to : g[i])
      if (cl[i] != cl[to]) q[cl[i]].push_back(cl[to]);
  for (int i = 1; i <= n; i++)
    if (q[i].empty() && !v[i].empty()) {
      int mini = inf;
      for (auto j : v[i]) mini = min(mini, calc(j));
      if (mini == inf) continue;
      bad += mini, cycle++;
    }
  cout << bad * 998 + cycle + n;
  return 0;
}
