#include<bits/stdc++.h>
using namespace std;
#define arep(i,x,n) for(int i=int(x);i<(int)(n);i++)
#define rep(i,n) for(ll i = 0;i < (ll)n;++i)
#define rrep(i,n) for(int i=int(n-1);i>=0;i--)
#define fs first
#define sc second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define coy cout<<"Yes"<<endl
#define con cout<<"No"<<endl
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9+7
#define LINF (ll)1e18+10  
using ll = long long;

using P = pair<ll,ll>;
using fP = pair<double, double>;
using PPI = pair<P, int>;
using PIP = pair<int, P>;
using Ps = pair<int, string>;

using vi = vector<int>;
using vl = vector<ll>;
using vc = vector<char>;
using vd = vector<double>;
using vs = vector<string>;
using vp = vector<P>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<double>>;
using vvc = vector<vector<char>>;
using vvp = vector<vector<P>>;
using vvb = vector<vector<bool>>;
template <typename T>
bool chmax(T& a, const T b) { if (a < b) { a = b; return true; } return false; }
template <typename T>
bool chmin(T& a, const T b) { if (a > b) { a = b; return true; } return false; }

//const ll mod=998244353;
const ll mod = 1e9 + 7;
const ll MAX = 100;
template <typename T>
T abs(T a) { if (a < 0)return -a; else return a; }//2020/09/30 stdlib has abs(long) abs(long long) error
//////////////////////////////////////


int main(){
    int n,m;
    cin>>n>>m;
    vvi e(n);
    rep(i,m){
        int a,b;
        cin>>a>>b;
        --a,--b;
        e[a].push_back(b);
    }
    vi ans(n);
    rep(i,n){
        queue<int>q;
        q.push(i);
        vi used(n);
        used[i]=1;
        while(q.size()){
            int now=q.front();q.pop();
            for(int to : e[now]){
                if(used[to])continue;
                q.push(to);
                ans[i]++;
                used[to]=1;
            }
        }
    }
    int sum=0;
    rep(i,n)sum+=ans[i];
    cout<<sum+n<<endl;
    return 0;
}
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>


using namespace std;
using namespace __gnu_pbds;

#define  pb push_back
#define inf 1e18
// 1000000007 , 998244353
// cout<<"? "<<endl for interactive

#define MOD 998244353
#define  mp make_pair
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define clr(x,y,z) (x).clear(),(x).resize((y),(z))

typedef long long int lli;
typedef vector<lli> vli;
typedef vector<int> vi;
typedef tuple<int, int, long long int>tup;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<lli, lli> pll;
typedef pair<int, int> pii;

#define csb(x) __builtin_popcount(x)
#define clz(x) __builtin_clz(x)
#define ctz(x) __builtin_ctz(x)
#define clin(x,n,m) x>=n*m||x<0||(x%m==0 && j==2) || (x%m==m-1 && j==3)
#define cgrid(x,y,n,m) x>=n||y>=m||x<=0||y<=0
#define PI 3.1415926535897932384626

// #define deb(x) cout << #x << " = " << x << endl;
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
#define show(x) {for( auto i : x) cout << i << '\t'; cout << endl;}
#define show2(x,l1,l2) {for( int i=0;i<l1;i++){for(int j=0;j<l2;j++) cout << x[i][j] << '\t'; cout << endl;}}
#define rep(i,st,ed) for(int i=(st);i<(ed);i++)
#define rrep(i,ed,st) for(int i=ed;i>=st;i--)
#define ff first
#define ss second
#define minheap(type) priority_queue<type,vector<type>,greater<type>>
#define sze(x) (int)x.size()
#define rgenerate mt19937 rgen(chrono::high_resolution_clock::now().time_since_epoch().count())
#define indexed_set tree<lli,null_type,less<lli>,rb_tree_tag,tree_order_statistics_node_update>
#define yes cout<<"YES\n"
#define no cout<<"NO\n"

template <typename T> void ckmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void ckmax(T &a, const T &b) { a = max(a, b); }
template<typename ...Args>void logger(string vars, Args&&... values) {cout << vars << " = "; string delim = ""; (..., (cout << delim << values, delim = ", ")); cout << "\n";}


long long gcd(ull a, ull b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
void sieve(vector<int>&spf) {int n = spf.size(); rep(i, 2, n) {if (!spf[i]) {/*prime.pb(i);*/ for (int j = i; j < n; j += i) spf[j] = i; }}}
lli power(lli x, lli y, long long int m = MOD) { lli ans = 1; x %= m; while (y) {if (y & 1)ans = (x * ans) % m; x = (x * x) % m; y >>= 1;} return ans;}
lli ncr(vli&fact, int n, int r) {return (n >= r) ? ((fact[n] * ((power(fact[r], MOD - 2) * power(fact[n - r], MOD - 2) ) % MOD)) % MOD) : 0;}

int add(int x, int y) {x += y; while (x >= MOD) x -= MOD; while (x < 0) x += MOD; return x;}
int mul(int x, int y) {return (x * 1ll * y) % MOD;}
int divide(int x, int y) {return mul(x, power(y, MOD - 2));}

// int dx[] = {1, -1, 0, 0};
// int dy[] = {0, 0, 1, -1};
// int dl[] = {m, -m, 1, -1};

vector<vi>adj;
vector<bool>visited;
int sz=0;
void dfs(int c){
    visited[c]=true;
    sz++;
    for(int i=0;i<adj[c].size();i++){
        int nxt =adj[c][i];
        if(visited[nxt])continue;
        dfs(nxt);
    }
}

void solve() {

    int n,m;
    cin>>n>>m;

    adj.resize(n);
    visited.assign(n,false);
    rep(i,0,m){
        int f,t;
        cin>>f>>t;
        f--,t--;
        adj[f].pb(t);
    }

    int ans=0;
    rep(i,0,n){
        sz=0;
        dfs(i),ans+=sz;
        rep(j,0,n)visited[j]=false;
    }



    cout<<ans;


}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int z = 1;
    // cin >> z;

    while (z--) {
        solve();
    }
    return 0;

}