#include <bits/stdc++.h>
using namespace std;
inline long long in() {
  int32_t x;
  scanf("%d", &x);
  return x;
}
inline long long lin() {
  long long x;
  scanf("%lld", &x);
  return x;
}
inline string get() {
  char ch[2000010];
  scanf("%s", ch);
  return ch;
}
inline void read(long long *a, long long n) {
  for (long long i = 0; i < n; i++) a[i] = in();
}
template <class blank>
inline void out(blank x) {
  cout << x << "\n";
  exit(0);
}
const long long maxn = 1e5 + 10;
const long long maxm = 1e6 + 10;
const long long maxlg = 21;
const long long base = 29;
const long long mod = 1e9 + 7;
const long long INF = 1e18 + 10;
const long long SQ = 317 + 5;
vector<long long> g[2][maxn];
long long comp[maxn];
bool mark[maxn];
vector<long long> cmp[maxn];
long long cyclecnt;
inline void dfs(long long node, long long fl, vector<long long> &vertices) {
  if (mark[node]) return;
  mark[node] = true;
  for (auto u : g[fl][node]) dfs(u, fl, vertices);
  vertices.push_back(node);
}
long long di[maxn];
long long pre[maxn];
long long q[maxn];
inline long long findcycle(long long node) {
  vector<long long> cycle;
  for (auto sink : cmp[comp[node]]) {
    for (auto others : cmp[comp[node]]) di[others] = INF, pre[others] = -1;
    long long h = 0, t = 1;
    q[0] = sink;
    di[sink] = 0;
    while (h < t) {
      long long v = q[h++];
      for (auto u : g[0][v]) {
        if (u == sink) {
          if (di[v] < cycle.size() || cycle.size() == 0) {
            cycle.clear();
            for (auto cur = v; cur != -1; cur = pre[cur]) cycle.push_back(cur);
          }
        }
        if (di[u] > di[v] + 1) {
          pre[u] = v;
          di[u] = di[v] + 1;
          q[t++] = u;
        }
      }
    }
  }
  return cycle.size();
}
long long res;
long long compCnt;
int32_t main() {
  long long n = in(), m = in();
  for (long long i = 0; i < m; i++) {
    long long v = in(), u = in();
    g[0][v].push_back(u);
    g[1][u].push_back(v);
  }
  vector<long long> topol;
  for (long long i = 1; i <= n; i++)
    if (!mark[i]) dfs(i, 0, topol);
  memset(mark, 0, sizeof mark);
  reverse(topol.begin(), topol.end());
  for (auto v : topol)
    if (!mark[v]) {
      dfs(v, 1, cmp[compCnt++]);
      for (auto u : cmp[compCnt - 1]) comp[u] = compCnt - 1;
    }
  memset(mark, 0, sizeof mark);
  for (long long i = 1; i <= n; i++) {
    if (mark[comp[i]]) continue;
    mark[comp[i]] = true;
    bool fl = true;
    for (auto v : cmp[comp[i]])
      for (auto u : g[0][v]) fl &= comp[u] == comp[v];
    if (!fl) continue;
    long long sz = findcycle(i);
    if (sz == 0) continue;
    res += sz * 998;
    cyclecnt++;
  }
  res += n + cyclecnt;
  cout << res << "\n";
}

### Prompt

Create a solution in Cpp for the following problem:

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>
using namespace std;
inline long long in() {
  int32_t x;
  scanf("%d", &x);
  return x;
}
inline long long lin() {
  long long x;
  scanf("%lld", &x);
  return x;
}
inline string get() {
  char ch[2000010];
  scanf("%s", ch);
  return ch;
}
inline void read(long long *a, long long n) {
  for (long long i = 0; i < n; i++) a[i] = in();
}
template <class blank>
inline void out(blank x) {
  cout << x << "\n";
  exit(0);
}
const long long maxn = 1e5 + 10;
const long long maxm = 1e6 + 10;
const long long maxlg = 21;
const long long base = 29;
const long long mod = 1e9 + 7;
const long long INF = 1e18 + 10;
const long long SQ = 317 + 5;
vector<long long> g[2][maxn];
long long comp[maxn];
bool mark[maxn];
vector<long long> cmp[maxn];
long long cyclecnt;
inline void dfs(long long node, long long fl, vector<long long> &vertices) {
  if (mark[node]) return;
  mark[node] = true;
  for (auto u : g[fl][node]) dfs(u, fl, vertices);
  vertices.push_back(node);
}
long long di[maxn];
long long pre[maxn];
long long q[maxn];
inline long long findcycle(long long node) {
  vector<long long> cycle;
  for (auto sink : cmp[comp[node]]) {
    for (auto others : cmp[comp[node]]) di[others] = INF, pre[others] = -1;
    long long h = 0, t = 1;
    q[0] = sink;
    di[sink] = 0;
    while (h < t) {
      long long v = q[h++];
      for (auto u : g[0][v]) {
        if (u == sink) {
          if (di[v] < cycle.size() || cycle.size() == 0) {
            cycle.clear();
            for (auto cur = v; cur != -1; cur = pre[cur]) cycle.push_back(cur);
          }
        }
        if (di[u] > di[v] + 1) {
          pre[u] = v;
          di[u] = di[v] + 1;
          q[t++] = u;
        }
      }
    }
  }
  return cycle.size();
}
long long res;
long long compCnt;
int32_t main() {
  long long n = in(), m = in();
  for (long long i = 0; i < m; i++) {
    long long v = in(), u = in();
    g[0][v].push_back(u);
    g[1][u].push_back(v);
  }
  vector<long long> topol;
  for (long long i = 1; i <= n; i++)
    if (!mark[i]) dfs(i, 0, topol);
  memset(mark, 0, sizeof mark);
  reverse(topol.begin(), topol.end());
  for (auto v : topol)
    if (!mark[v]) {
      dfs(v, 1, cmp[compCnt++]);
      for (auto u : cmp[compCnt - 1]) comp[u] = compCnt - 1;
    }
  memset(mark, 0, sizeof mark);
  for (long long i = 1; i <= n; i++) {
    if (mark[comp[i]]) continue;
    mark[comp[i]] = true;
    bool fl = true;
    for (auto v : cmp[comp[i]])
      for (auto u : g[0][v]) fl &= comp[u] == comp[v];
    if (!fl) continue;
    long long sz = findcycle(i);
    if (sz == 0) continue;
    res += sz * 998;
    cyclecnt++;
  }
  res += n + cyclecnt;
  cout << res << "\n";
}

```