#include <bits/stdc++.h>
using namespace std;
inline long long getint() {
  long long _x = 0, _tmp = 1;
  char _tc = getchar();
  while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
  if (_tc == '-') _tc = getchar(), _tmp = -1;
  while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
  return _x * _tmp;
}
inline long long add(long long _x, long long _y,
                     long long _mod = 1000000007ll) {
  long long _ = _x + _y;
  if (_ >= _mod) _ -= _mod;
  return _;
}
inline long long sub(long long _x, long long _y,
                     long long _mod = 1000000007ll) {
  long long _ = _x - _y;
  if (_ < 0) _ += _mod;
  return _;
}
inline long long mul(long long _x, long long _y,
                     long long _mod = 1000000007ll) {
  long long _ = _x * _y;
  if (_ >= _mod) _ %= _mod;
  return _;
}
long long mypow(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 1ll;
  long long _tmp = mypow(_a, _x / 2, _mod);
  _tmp = mul(_tmp, _tmp, _mod);
  if (_x & 1) _tmp = mul(_tmp, _a, _mod);
  return _tmp;
}
long long mymul(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 0ll;
  long long _tmp = mymul(_a, _x / 2, _mod);
  _tmp = add(_tmp, _tmp, _mod);
  if (_x & 1) _tmp = add(_tmp, _a, _mod);
  return _tmp;
}
inline bool equal(double _x, double _y) {
  return _x > _y - 1e-9 && _x < _y + 1e-9;
}
int __ = 1, _cs;
int n, m, scc[5140], scnt;
vector<int> g[5140], rg[5140];
vector<int> st;
bool vst[5140], odeg[5140];
void build() {}
void dfs(int now) {
  vst[now] = true;
  for (int nxt : g[now])
    if (!vst[nxt]) dfs(nxt);
  st.push_back(now);
}
void rdfs(int now) {
  vst[now] = true;
  scc[now] = scnt;
  for (int nxt : rg[now])
    if (!vst[nxt]) rdfs(nxt);
}
inline void kosaraju() {
  for (int i = 1; i <= n; i++)
    if (!vst[i]) dfs(i);
  reverse((st).begin(), (st).end());
  for (int i = 1; i <= n; i++) vst[i] = false;
  for (int i : st) {
    if (vst[i]) continue;
    rdfs(i);
    scnt++;
  }
}
void init() {
  n = getint();
  m = getint();
  while (m--) {
    int ui = getint();
    int vi = getint();
    g[ui].push_back(vi);
    rg[vi].push_back(ui);
  }
  kosaraju();
  for (int i = 1; i <= n; i++)
    for (int nxt : g[i])
      if (scc[i] != scc[nxt]) odeg[scc[i]] = true;
}
int dep[5140], mn, ss;
void go(int now, int ndep) {
  dep[now] = ndep;
  vst[now] = true;
  for (int nxt : g[now]) {
    if (scc[now] != scc[nxt]) continue;
    if (nxt == ss)
      mn = min(mn, dep[now] + 1);
    else if (!vst[nxt])
      go(nxt, ndep + 1);
  }
}
inline int find_cyc(int now) {
  for (int i = 1; i <= n; i++) vst[i] = false;
  mn = 5140;
  ss = now;
  go(now, 0);
  return mn;
}
void solve() {
  int sum = 0, cnt = 0;
  for (int i = 0; i < scnt; i++) {
    if (odeg[i]) continue;
    int omn = 5140;
    for (int j = 1; j <= n; j++)
      if (scc[j] == i) omn = min(omn, find_cyc(j));
    if (omn == 5140) continue;
    cnt++;
    sum += omn;
  }
  printf("%d\n", n + cnt + 998 * sum);
}
int main() {
  build();
  while (__--) {
    init();
    solve();
  }
}
