#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define ln cout<<'\n'
void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<' ';pr(b...);}
template<class T> inline bool chmin(T& a, T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b){if(a<b){a=b;return true;}return false;}

bool temp[2010];
vector<vector<int>> to;

void dfs(int a) {
	if (temp[a]) return;
	temp[a] = true;
	for (auto v:to[a]) dfs(v);
}

void solve() {
	int n , m; cin >> n >> m;
	to.resize(n);
	rep(i, m) {
		int a, b; cin >> a >> b;
		to[a-1].push_back(b-1);
	}
	int ans = 0;
	rep(i, n) {
		rep(i, n) temp[i] = false;
		dfs(i);
		rep(i, n) if (temp[i]) ++ans;
	}
	cout << ans << endl;
}

int main(){
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	solve();
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
int main()
{
    int n,m;
  	int vdnsddglkdk; 
    cin>>n>>m;
    vector<pair<int,int>>a(m);
    for(int i=0;i<m;i++)
    {
        cin>>a[i].first>>a[i].second;
    }
    int k;
    cin>>k;
    vector<pair<int,int>>b(k);
    for(int j=0;j<k;j++)
    {
        cin>>b[j].first>>b[j].second;
    }
    ll l=0;
    ll tot=1<<k;
    for(ll mask=0;mask<tot;mask++)
    {
        vector<int>v(n+1);
        ll ans=0;
        for(int i=0;i<k;i++)
        {
            if(mask & (1<<i))
                v[b[i].second]=1;
            else
                v[b[i].first]=1;
        }
        for(int i=0;i<m;i++)
        {
            if(v[a[i].first] && v[a[i].second])
                ans++;
        }
        l=max(l,ans);
    }
    cout<<l<<endl;
    return 0;
}