#include<bits/stdc++.h>

using namespace std;

long long INF = 1000000000000;
std::vector<long long> AA;
std::vector<long long> visited;

bool comp(int a, int b) {
    return AA[a] < AA[b];
}

long long bfs(long long now, std::vector<long long> &A, vector<vector<long long>> &edges){
    long long m = -INF;
    queue<long long> q;
    q.push(now);
    while(!q.empty()){
        long long p = q.front();
        q.pop();
        int l = edges[p].size();
        for(int i = 0 ; i < l ; i++){
            if(visited[edges[p][i]] == false){
                visited[edges[p][i]] = true;
                q.push(edges[p][i]);
                m = max(m, A[edges[p][i]]);
            }
        }
    }
    return m;
}

void solve(long long N, long long M, std::vector<long long> A, std::vector<long long> X, std::vector<long long> Y){
    AA = A;
    vector<long long> index(N);
    for(int i = 0 ; i < N ; i++){
        index[i] = i;
    }
    sort(index.begin(), index.end(), comp);

    vector<vector<long long>> edges(N);
    for(int i = 0 ; i < M ; i++){
        edges[X[i] - 1].push_back(Y[i] - 1);
    }
    visited.resize(N);
    for(int i = 0 ; i < N ; i++){
        visited[i] = false;
        // cout<<index[i]<<" ";
    }
    // cout<<endl;
    long long max_profit = -INF;
    for(int i = 0 ; i < N ; i++){
        if(visited[index[i]] == false){
            // visited[index[i]] = true;
            long long m = bfs(index[i], A, edges);
            max_profit = max(max_profit, m - A[index[i]]);
        }
    }

    printf("%lld\n", max_profit);
}

int main(){
    long long N;
    scanf("%lld",&N);
    long long M;
    scanf("%lld",&M);
    std::vector<long long> A(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&A[i]);
    }
    std::vector<long long> X(M);
    std::vector<long long> Y(M);
    for(int i = 0 ; i < M ; i++){
        scanf("%lld %lld",&X[i], &Y[i]);
    }
    solve(N, M, std::move(A), std::move(X), std::move(Y));

    return 0;
}

#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define all(vr) vr.begin(),vr.end()
#define vi vector<int>
#define vll vector<ll>
const int N = 1e5 + 10;

vi G[N];
int vis[N];

void dfs(int u)
{
    vis[u] = 1;
    for (int v : G[u])
        if (!vis[v]) dfs(v);
}

int main()
{
    //freopen("ss.inp", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1, u, v; i <= m; ++i)
    {
        cin >> u >> v;
        G[u].eb(v);
    }
    int res = 0;
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= n; ++j) vis[j] = 0;
        dfs(i);
        for (int j = 1; j <= n; ++j) res += vis[j];
    }
    cout << res;
    return 0;
}
