#include <bits/stdc++.h>
int inp() {
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  int sum = 0;
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum;
}
int head[5010], nxt[10010], end[10010], q[5010];
int dis[5010], dfn[5010], low[5010], res[5010], st[5010], bel[5010];
bool used[5010], ins[5010], flg[5010];
int cnt = 0, idx = 0, cou = 0;
void link(int a, int b) {
  nxt[++cou] = head[a];
  head[a] = cou;
  end[cou] = b;
}
int top = 0;
void tarjan(int cur) {
  dfn[cur] = low[cur] = ++idx;
  ins[cur] = true;
  st[++top] = cur;
  for (int x = head[cur]; x != -1; x = nxt[x]) {
    if (!dfn[end[x]]) {
      tarjan(end[x]);
      low[cur] = std::min(low[cur], low[end[x]]);
    } else if (ins[end[x]])
      low[cur] = std::min(low[cur], dfn[end[x]]);
  }
  if (low[cur] == dfn[cur]) {
    cnt++;
    while (st[top] != cur) {
      bel[st[top]] = cnt;
      ins[st[top]] = false;
      top--;
    }
    top--;
    ins[cur] = false;
    bel[cur] = cnt;
  }
}
int bfs(int s) {
  int qf = 1, qe = 1;
  q[1] = s;
  memset(dis, -1, sizeof(dis));
  dis[s] = 0;
  int ans = 2147483647;
  while (qf <= qe) {
    int u = q[qf++];
    for (int x = head[u]; x != -1; x = nxt[x]) {
      if (dis[end[x]] == -1) {
        dis[end[x]] = dis[u] + 1;
        q[++qe] = end[x];
      } else if (end[x] == s)
        ans = std::min(ans, dis[u] + 1);
    }
  }
  return ans;
}
int main() {
  memset(head, -1, sizeof(head));
  int n = inp();
  int m = inp();
  for (int i = 1; i <= m; i++) {
    int u = inp();
    int v = inp();
    link(u, v);
  }
  memset(res, 0x3f, sizeof(res));
  for (int i = 1; i <= n; i++)
    if (!dfn[i]) tarjan(i);
  for (int i = 1; i <= n; i++)
    for (int x = head[i]; x != -1; x = nxt[x])
      if (bel[end[x]] != bel[i]) flg[bel[i]] = true;
  int ans = n;
  for (int i = 1; i <= n; i++) {
    res[bel[i]] = std::min(res[bel[i]], bfs(i));
  }
  for (int i = 1; i <= cnt; i++)
    if (!flg[i] && res[i] != res[0]) ans += res[i] * 998 + 1;
  printf("%d\n", ans);
}

### Prompt

Develop a solution in CPP to the problem described below:

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

### Response

```cpp
#include <bits/stdc++.h>
int inp() {
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  int sum = 0;
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum;
}
int head[5010], nxt[10010], end[10010], q[5010];
int dis[5010], dfn[5010], low[5010], res[5010], st[5010], bel[5010];
bool used[5010], ins[5010], flg[5010];
int cnt = 0, idx = 0, cou = 0;
void link(int a, int b) {
  nxt[++cou] = head[a];
  head[a] = cou;
  end[cou] = b;
}
int top = 0;
void tarjan(int cur) {
  dfn[cur] = low[cur] = ++idx;
  ins[cur] = true;
  st[++top] = cur;
  for (int x = head[cur]; x != -1; x = nxt[x]) {
    if (!dfn[end[x]]) {
      tarjan(end[x]);
      low[cur] = std::min(low[cur], low[end[x]]);
    } else if (ins[end[x]])
      low[cur] = std::min(low[cur], dfn[end[x]]);
  }
  if (low[cur] == dfn[cur]) {
    cnt++;
    while (st[top] != cur) {
      bel[st[top]] = cnt;
      ins[st[top]] = false;
      top--;
    }
    top--;
    ins[cur] = false;
    bel[cur] = cnt;
  }
}
int bfs(int s) {
  int qf = 1, qe = 1;
  q[1] = s;
  memset(dis, -1, sizeof(dis));
  dis[s] = 0;
  int ans = 2147483647;
  while (qf <= qe) {
    int u = q[qf++];
    for (int x = head[u]; x != -1; x = nxt[x]) {
      if (dis[end[x]] == -1) {
        dis[end[x]] = dis[u] + 1;
        q[++qe] = end[x];
      } else if (end[x] == s)
        ans = std::min(ans, dis[u] + 1);
    }
  }
  return ans;
}
int main() {
  memset(head, -1, sizeof(head));
  int n = inp();
  int m = inp();
  for (int i = 1; i <= m; i++) {
    int u = inp();
    int v = inp();
    link(u, v);
  }
  memset(res, 0x3f, sizeof(res));
  for (int i = 1; i <= n; i++)
    if (!dfn[i]) tarjan(i);
  for (int i = 1; i <= n; i++)
    for (int x = head[i]; x != -1; x = nxt[x])
      if (bel[end[x]] != bel[i]) flg[bel[i]] = true;
  int ans = n;
  for (int i = 1; i <= n; i++) {
    res[bel[i]] = std::min(res[bel[i]], bfs(i));
  }
  for (int i = 1; i <= cnt; i++)
    if (!flg[i] && res[i] != res[0]) ans += res[i] * 998 + 1;
  printf("%d\n", ans);
}

```