TOF

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 u i and v i (1  ≤  u i,  v i  ≤  n), meaning there is a directed  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

Solution:

#include <bits/stdc++.h>

using namespace std;

const int N = 40010;

bool was[N];
vector <int> g[N], gr[N];
int kw, w[N];
int comp[N];

void dfs1(int v) {
was[v] = true;
int sz = gr[v].size();
for (int j = 0; j < sz; j++) {
    int u = gr[v][j];
    if (!was[u]) {
      dfs1(u);
    }
  }
  w[kw++] = v;
}

void dfs2(int v) {
  int sz = g[v].size();
  for (int j = 0; j < sz; j++) {
    int u = g[v][j];
    if (comp[u] == -1) {
      comp[u] = comp[v];
      dfs2(u);
    }
  }
}

int d[N], x[N];
vector <int> who[N];
int value[N], best[N];
int from[N], to[N];

const int inf = (int) 1e9;

int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
    g[i].clear();
    gr[i].clear();
  }
  for (int i = 0; i < m; i++) {
    scanf("%d %d", from + i, to + i);
    from[i]--; to[i]--;
    g[from[i]].push_back(to[i]);
    gr[to[i]].push_back(from[i]);
  }
  for (int i = 0; i < n; i++) {
    was[i] = false;
  }
  kw = 0;
  for (int i = 0; i < n; i++) {
    if (!was[i]) {
      dfs1(i);
    }
  }
  for (int i = 0; i < n; i++) {
    comp[i] = -1;
  }
  int nc = 0;
  for (int it = kw - 1; it >= 0; it--) {
int i = w[it];
if (comp[i] == -1) {
comp[i] = nc++;
dfs2(i);
}
}
for (int start = 0; start < n; start++) {
    for (int i = 0; i < n; i++) {
      d[i] = -1;
    }
    d[start] = 0;
    int b = 0, e = 1;
    x[0] = start;
    while (b < e) {
      int sz = g[x[b]].size();
      for (int j = 0; j < sz; j++) {
        int u = g[x[b]][j];
        if (d[u] == -1) {
          d[u] = d[x[b]] + 1;
          x[e++] = u;
        }
      }
      b++;
    }
    int sz = gr[start].size();
    int shortest = inf;
    for (int j = 0; j < sz; j++) {
      int u = gr[start][j];
      if (d[u] != -1) {
        shortest = min(shortest, d[u] + 1);
      }
    }
    value[start] = shortest;
  }
  for (int i = 0; i < nc; i++) {
    best[i] = inf;
    who[i].clear();
  }
  for (int i = 0; i < n; i++) {
    best[comp[i]] = min(best[comp[i]], value[i]);
    who[comp[i]].push_back(i);
  }
  int ans = n;
  for (int i = 0; i < nc; i++) {
    bool is_sink = true;
    int sz = who[i].size();
    for (int j = 0; j < sz; j++) {
      int v = who[i][j];
      int z = g[v].size();
      for (int q = 0; q < z; q++) {
        int u = g[v][q];
        if (comp[u] != comp[v]) {
          is_sink = false;
          break;
        }
      }
      if (!is_sink) {
        break;
      }
    }
    if (is_sink) {
      if (best[i] != inf) {
        ans += 998 * best[i] + 1;
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}