#include <bits/stdc++.h>
using namespace std;
template <typename T>
void _R(T &x) {
  cin >> x;
}
void _R(int &x) { scanf("%d", &x); }
void _R(long long &x) { scanf("%" PRId64, &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template <typename T, typename... U>
void R(T &head, U &...tail) {
  _R(head);
  R(tail...);
}
template <typename T>
void _W(const T &x) {
  cout << x;
}
void _W(const int &x) { printf("%d", x); }
template <typename T>
void _W(const vector<T> &x) {
  for (auto i = x.cbegin(); i != x.cend(); i++) {
    if (i != x.cbegin()) putchar(' ');
    _W(*i);
  }
}
void W() {}
template <typename T, typename... U>
void W(const T &head, const U &...tail) {
  _W(head);
  putchar(sizeof...(tail) ? ' ' : '\n');
  W(tail...);
}
const int N = 5140;
struct SCC {
  int n;
  vector<vector<int>> e1, e2, scc;
  vector<int> vis, ord;
  void init(int _n) {
    n = _n;
    e1.resize(n + 1);
    e2.resize(n + 1);
    for (int i = (1); i <= int(n); i++) {
      e1[i].clear();
      e2[i].clear();
    }
  }
  void add(int a, int b) {
    e1[a].push_back(b);
    e2[b].push_back(a);
  }
  void dfs1(int u) {
    vis[u] = 1;
    for (int v : e1[u])
      if (vis[v] == 0) dfs1(v);
    ord.push_back(u);
  }
  void dfs2(int u) {
    vis[u] = 2;
    scc.back().push_back(u);
    for (int v : e2[u])
      if (vis[v] == 1) dfs2(v);
  }
  void solve() {
    vis.clear();
    vis.resize(n + 1);
    ord.clear();
    ord.reserve(n + 1);
    for (int i = (1); i <= int(n); i++)
      if (vis[i] == 0) dfs1(i);
    reverse(begin(ord), end(ord));
    scc.clear();
    for (int i : ord)
      if (vis[i] == 1) {
        scc.emplace_back();
        dfs2(i);
      }
  }
} scc;
int n, m;
vector<int> e[N];
void BFS(int st, int d[N]) {
  int ql = 0, qr = 0, q[N];
  for (int i = (1); i <= int(n); i++) d[i] = N;
  d[st] = 0;
  q[qr++] = st;
  while (ql != qr) {
    int p = q[ql++];
    for (int i : e[p])
      if (d[i] == N) {
        d[i] = d[p] + 1;
        q[qr++] = i;
      }
  }
}
int dis[N][N], mc[N];
bool done[N];
int main() {
  R(n, m);
  for (int i = 0; i < int(m); i++) {
    int a, b;
    R(a, b);
    e[a].push_back(b);
  }
  for (int i = (1); i <= int(n); i++) BFS(i, dis[i]);
  for (int i = (1); i <= int(n); i++) mc[i] = N;
  for (int i = (1); i <= int(n); i++)
    for (int j = (i + 1); j <= int(n); j++) {
      int now = dis[i][j] + dis[j][i];
      mc[i] = min(mc[i], now);
      mc[j] = min(mc[j], now);
    }
  scc.init(n);
  for (int i = (1); i <= int(n); i++)
    for (int j : e[i]) scc.add(i, j);
  scc.solve();
  reverse(begin(scc.scc), end(scc.scc));
  int ans = 0;
  for (auto v : scc.scc) {
    ;
    ans += ((int)(v).size());
    bool ok = 0;
    for (int i : v) {
      if (e[i].empty()) ok = 1;
      for (int j : e[i])
        if (done[j]) ok = 1;
    }
    if (!ok) {
      int mi = N;
      for (int i : v) mi = min(mi, mc[i]);
      ans += 998 * mi + 1;
    }
    for (int i : v) done[i] = 1;
  }
  W(ans);
  return 0;
}
