#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

vector<int> adj[100005];
int in[100005];
int e[100005];
int rd[100005];
bool has[100005];
int n, m;

int solve() {
    queue<int> q;
    for (int i = 0; i < n; ++i) {
        if (in[i] == 0) {
            rd[i] = 1;
            q.push(i);
        }
    }

    while (!q.empty()) {
        int now = q.front();
        q.pop();

        for (int next : adj[now]) {
            rd[next] = max(rd[next], rd[now] + (e[next] == 0 ? 1 : 0));

            --in[next];
            if (in[next] == 0)
                q.push(next);
        }
    }

    int res = 0;
    for (int i = 0; i < n; ++i) {
        if (e[i] == 1 && !has[rd[i]]) {
            ++res;
            has[rd[i]] = true;
        }
    }

    return res;
}

int main(int argc, char* argv[]) {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i)
        scanf("%d", &e[i]);
    for (int i = 0; i < m; ++i) {
        int t1, t2;
        scanf("%d%d", &t1, &t2);

        adj[t2].push_back(t1);
        ++in[t1];
    }

    int res = solve();
    printf("%d\n", res);

    return 0;
}


#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

vector<int> adj[100005];
int in[100005];
int e[100005];
int rd[100005];
bool has[100005];
int n, m;

int solve() {
    queue<int> q;
    for (int i = 0; i < n; ++i) {
        if (in[i] == 0) {
            rd[i] = 1;
            q.push(i);
        }
    }

    while (!q.empty()) {
        int now = q.front();
        q.pop();

        for (int next : adj[now]) {
            rd[next] = max(rd[next], rd[now] + (e[next] == 0 && e[now] == 1 ? 1 : 0));

            --in[next];
            if (in[next] == 0)
                q.push(next);
        }
    }

    int res = 0;
    for (int i = 0; i < n; ++i) {
        if (e[i] == 1 && !has[rd[i]]) {
            ++res;
            has[rd[i]] = true;
        }
    }

    return res;
}

int main(int argc, char* argv[]) {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i)
        scanf("%d", &e[i]);
    for (int i = 0; i < m; ++i) {
        int t1, t2;
        scanf("%d%d", &t1, &t2);

        adj[t2].push_back(t1);
        ++in[t1];
    }

    int res = solve();
    printf("%d\n", res);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 100005;

int n, m;
int c[N];
vector<int> a[N], b[N];

int q[N];
int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; ++ i)
        cin >> c[i];
    for (int i = 0; i < m; ++ i)
    {
        int x, y;
        cin >> x >> y;
        a[x].push_back(y);
        b[y].push_back(x);
    }
    queue<int> q0, q1;
    for (int i = 0; i < n; ++ i)
    {
        q[i] = a[i].size();
        if (!q[i])
        {
            if (c[i] == 0)
                q0.push(i);
            else
                q1.push(i);
        }
    }
    int ans = 0;
    while (1)
    {
        if (q0.empty() && q1.empty())
            break;
        else if (q0.empty())
        {
            while (!q1.empty())
            {
                int x = q1.front();
                q1.pop();
                for (int i = 0; i < b[x].size(); ++ i)
                {
                    int p = b[x][i];
                    q[p] --;
                    if (q[p] == 0)
                    {
                        if (c[p] == 0)
                            q0.push(p);
                        else
                            q1.push(p);
                    }
                }
            }
        }
        else
        {
            ans ++;
            while (!q0.empty())
            {
                int x = q0.front();
                q0.pop();
                for (int i = 0; i < b[x].size(); ++ i)
                {
                    int p = b[x][i];
                    q[p] --;
                    if (q[p] == 0)
                    {
                        if (c[p] == 0)
                            q0.push(p);
                        else
                            q1.push(p);
                    }
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 100005;

int n, m;
int c[N];
vector<int> a[N], b[N];

int q[N];
int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; ++ i)
        cin >> c[i];
    for (int i = 0; i < m; ++ i)
    {
        int x, y;
        cin >> x >> y;
        a[x].push_back(y);
        b[y].push_back(x);
    }
    queue<int> q0, q1;
    for (int i = 0; i < n; ++ i)
    {
        q[i] = a[i].size();
        if (!q[i])
        {
            if (c[i] == 0)
                q0.push(i);
            else
                q1.push(i);
        }
    }
    int ans = 0;
    while (1)
    {
        if (q0.empty() && q1.empty())
            break;
        else if (q0.empty())
        {
            ans ++;
            while (!q1.empty())
            {
                int x = q1.front();
                q1.pop();
                for (int i = 0; i < b[x].size(); ++ i)
                {
                    int p = b[x][i];
                    q[p] --;
                    if (q[p] == 0)
                    {
                        if (c[p] == 0)
                            q0.push(p);
                        else
                            q1.push(p);
                    }
                }
            }
        }
        else
        {
            while (!q0.empty())
            {
                int x = q0.front();
                q0.pop();
                for (int i = 0; i < b[x].size(); ++ i)
                {
                    int p = b[x][i];
                    q[p] --;
                    if (q[p] == 0)
                    {
                        if (c[p] == 0)
                            q0.push(p);
                        else
                            q1.push(p);
                    }
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define REV(i,j,k) for(int i=j;i>=k;i--)
#define FORR(i,j,k,l) for(int i=j;i<=k;i+=l)
#define inf         freopen("in.txt", "r", stdin)
#define outf        freopen("out.txt", "w", stdout)
#define pf          printf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c)    scanf("%d %d %d", &a, &b, &c)
#define clean(mat,n)   FOR(i,0,n) mat[i].clear()
#define minn          (long long)-1000000000000000000
#define maxx          (long long) 1000000000000000000
#define mod          998244353
#define M_PI           3.14159265358979323846  /* pi */
#define LL           long long
#define NL '\n'
#define cnd tree[idx]
#define lnd (idx<<1)
#define rnd ((idx<<1)+1)
#define PB push_back
#define F first
#define S second
#define MAX 100010
#define MP make_pair
#define valid(nx,ny)  ((nx >= 0) && (nx < row) && (ny >= 0) && (ny < col))
#define LD long double
typedef pair<int,int> pii;

vector<int> mat[MAX];
int deg[MAX], vis[MAX], n, ty[MAX], in[MAX];
queue<int> que, one;

int main()
{
    ios::sync_with_stdio(false);
    //inf;
    //outf;

    int u, v, m;

    cin >> n >> m;
    FOR(i,0,n-1) cin >> ty[i];
    FOR(i,1,m)
    {
        cin >> u >> v;
        mat[u].PB( v ); deg[v]++;
    }

    FOR(i,0,n-1)
        if(!deg[i] && ty[i]) que.push( i );
    FOR(i,0,n-1)
        if(!deg[i] && !ty[i]) one.push( i );

    int  prin = 0;
    while(que.size() || one.size())
    {
        while(!one.empty())
        {
            int p = one.front(); one.pop();
            for(auto x : mat[p])
            {
                deg[x]--;
                if(!deg[x] && !ty[x]) one.push( x );
                else if(!deg[x]) que.push( x );
            }
        }

        if(que.size()) prin++;
        while(!que.empty())
        {
            int p = que.front(); que.pop();
            for(auto x : mat[p])
            {
                deg[x]--;
                if(!deg[x] && ty[x]) que.push( x );
                else if(!deg[x]) one.push( x );
            }
        }
    }

    cout << prin << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define REV(i,j,k) for(int i=j;i>=k;i--)
#define FORR(i,j,k,l) for(int i=j;i<=k;i+=l)
#define inf         freopen("in.txt", "r", stdin)
#define outf        freopen("out.txt", "w", stdout)
#define pf          printf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c)    scanf("%d %d %d", &a, &b, &c)
#define clean(mat,n)   FOR(i,0,n) mat[i].clear()
#define minn          (long long)-1000000000000000000
#define maxx          (long long) 1000000000000000000
#define mod          998244353
#define M_PI           3.14159265358979323846  /* pi */
#define LL           long long
#define NL '\n'
#define cnd tree[idx]
#define lnd (idx<<1)
#define rnd ((idx<<1)+1)
#define PB push_back
#define F first
#define S second
#define MAX 100010
#define MP make_pair
#define valid(nx,ny)  ((nx >= 0) && (nx < row) && (ny >= 0) && (ny < col))
#define LD long double
typedef pair<int,int> pii;

vector<int> mat[MAX];
int deg[MAX], vis[MAX], n, ty[MAX], in[MAX];
queue<int> que, one;

int main()
{
    ios::sync_with_stdio(false);
    //inf;
    //outf;

    int u, v, m;

    cin >> n >> m;
    FOR(i,0,n-1) cin >> ty[i];
    FOR(i,1,m)
    {
        cin >> u >> v;
        mat[u].PB( v ); deg[v]++;
    }

    FOR(i,0,n-1)
        if(!deg[i] && ty[i]) que.push( i );
    FOR(i,0,n-1)
        if(!deg[i] && !ty[i]) one.push( i );

    int  prin = 0;
    while(que.size() || one.size())
    {
        if(que.size()) prin++;
        while(!que.empty())
        {
            int p = que.front(); que.pop();
            for(auto x : mat[p])
            {
                deg[x]--;
                if(!deg[x] && ty[x]) que.push( x );
                else if(!deg[x]) one.push( x );
            }
        }

        while(!one.empty())
        {
            int p = one.front(); one.pop();
            for(auto x : mat[p])
            {
                deg[x]--;
                if(!deg[x] && !ty[x]) one.push( x );
                else if(!deg[x]) que.push( x );
            }
        }
    }

    cout << prin << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define REV(i,j,k) for(int i=j;i>=k;i--)
#define FORR(i,j,k,l) for(int i=j;i<=k;i+=l)
#define inf         freopen("in.txt", "r", stdin)
#define outf        freopen("out.txt", "w", stdout)
#define pf          printf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c)    scanf("%d %d %d", &a, &b, &c)
#define clean(mat,n)   FOR(i,0,n) mat[i].clear()
#define minn          (long long)-1000000000000000000
#define maxx          (long long) 1000000000000000000
#define mod          998244353
#define M_PI           3.14159265358979323846  /* pi */
#define LL           long long
#define NL '\n'
#define cnd tree[idx]
#define lnd (idx<<1)
#define rnd ((idx<<1)+1)
#define PB push_back
#define F first
#define S second
#define MAX 100010
#define MP make_pair
#define valid(nx,ny)  ((nx >= 0) && (nx < row) && (ny >= 0) && (ny < col))
#define LD long double
typedef pair<int,int> pii;

vector<int> mat[MAX];
int deg[MAX], vis[MAX], n, ty[MAX], in[MAX];
queue<int> que, one;

int main()
{
    ios::sync_with_stdio(false);
    //inf;
    //outf;

    int u, v, m;

    cin >> n >> m;
    FOR(i,0,n-1) cin >> ty[i];
    FOR(i,1,m)
    {
        cin >> u >> v;
        mat[u].PB( v ); deg[v]++;
    }

    FOR(i,0,n-1)
        if(!deg[i] && ty[i]) que.push( i );

    int  prin = 0;
    while(que.size() || one.size())
    {
        if(!que.empty()) prin++;
        while(!que.empty())
        {
            int p = que.front(); que.pop();
            for(auto x : mat[p])
            {
                deg[x]--;
                if(!deg[x] && ty[x]) que.push( x );
                else if(!deg[x]) one.push( x );
            }
        }

        while(!one.empty())
        {
            int p = one.front(); one.pop();
            for(auto x : mat[p])
            {
                deg[x]--;
                if(!deg[x] && !ty[x]) one.push( x );
                else if(!deg[x]) que.push( x );
            }
        }
    }

    cout << prin << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define REV(i,j,k) for(int i=j;i>=k;i--)
#define FORR(i,j,k,l) for(int i=j;i<=k;i+=l)
#define inf         freopen("in.txt", "r", stdin)
#define outf        freopen("out.txt", "w", stdout)
#define pf          printf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c)    scanf("%d %d %d", &a, &b, &c)
#define clean(mat,n)   FOR(i,0,n) mat[i].clear()
#define minn          (long long)-1000000000000000000
#define maxx          (long long) 1000000000000000000
#define mod          998244353
#define M_PI           3.14159265358979323846  /* pi */
#define LL           long long
#define NL '\n'
#define cnd tree[idx]
#define lnd (idx<<1)
#define rnd ((idx<<1)+1)
#define PB push_back
#define F first
#define S second
#define MAX 100010
#define MP make_pair
#define valid(nx,ny)  ((nx >= 0) && (nx < row) && (ny >= 0) && (ny < col))
#define LD long double
typedef pair<int,int> pii;

vector<int> mat[MAX];
int deg[MAX], vis[MAX], n, ty[MAX], in[MAX];
queue<int> que, one;

int main()
{
    ios::sync_with_stdio(false);
    //inf;
    //outf;

    int u, v, m;

    cin >> n >> m;
    FOR(i,0,n-1) cin >> ty[i];
    FOR(i,1,m)
    {
        cin >> u >> v;
        mat[u].PB( v ); deg[v]++;
    }

    FOR(i,0,n-1)
        if(!deg[i]) que.push( i );

    int  prin = 0;
    while(que.size())
    {
        prin++;
        while(!que.empty())
        {
            int p = que.front(); que.pop();
            for(auto x : mat[p])
            {
                deg[x]--;
                if(!deg[x] && ty[x]) que.push( x );
                else if(!deg[x]) one.push( x );
            }
        }

        while(!one.empty())
        {
            int p = one.front(); one.pop();
            for(auto x : mat[p])
            {
                deg[x]--;
                if(!deg[x] && !ty[x]) one.push( x );
                else if(!deg[x]) que.push( x );
            }
        }
    }

    cout << prin << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define endl "\n"

const ll lmt=1e5+5;
ll n,m,deg[lmt];
vector<ll>adj[lmt];
bool e[lmt];

int main(){
	ios_base::sync_with_stdio(false);

	cin>>n>>m;
	for(ll i=1;i<=n;i++){
		cin>>e[i];
	}
	for(ll i=1;i<=m;i++){
		ll u,v;
		cin>>u>>v;
		u++,v++;
		deg[u]++;
		adj[v].push_back(u);
	}
	queue<ll>q[2];
	for(ll i=1;i<=n;i++){
		if(!deg[i]){
			q[e[i]].push(i);
		}
	}
	ll last=0,ans=0;
	while(!q[0].empty() || !q[1].empty()){
		ll u;
		if(!q[last].empty()){
			u=q[last].front();
			q[last].pop();
		}else{
			u=q[last^1].front();
			q[last^1].pop();
			last^=1;
			if(last==1) ans++;
		}
		for(ll v:adj[u]){
			deg[v]--;
			if(!deg[v]){
				q[e[v]].push(v);
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define endl "\n"

const ll lmt=1e5+5;
ll n,m,deg[lmt],reach[lmt];
vector<ll>adj[lmt];
bool vis[lmt],e[lmt];

int main(){
	ios_base::sync_with_stdio(false);

	cin>>n>>m;
	for(ll i=1;i<=n;i++){
		cin>>e[i];
	}
	for(ll i=1;i<=m;i++){
		ll u,v;
		cin>>u>>v;
		u++,v++;
		deg[u]++;
		adj[v].push_back(u);
	}
	queue<ll>q;
	for(ll i=1;i<=n;i++){
		if(!deg[i]){
			reach[i]=1;
			vis[i]=1;
			q.push(i);
		}
	}
	while(!q.empty()){
		ll u=q.front();
		q.pop();
		for(ll v:adj[u]){
			if(!vis[v]){
				deg[v]--;
				ll now=reach[u];
				if(!e[u] && e[v]){
					now++;
				}
				reach[v]=max(reach[v],now);
				if(!deg[v]){
					vis[v]=1;
					q.push(v);
				}
			}
		}
	}
	ll ans=0;
	for(ll i=1;i<=n;i++){
		ans=max(ans,reach[i]);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define endl "\n"

const ll lmt=1e5+5;
ll n,m,deg[lmt],reach[lmt];
vector<ll>adj[lmt];
bool vis[lmt],e[lmt];

int main(){
	ios_base::sync_with_stdio(false);

	cin>>n>>m;
	for(ll i=1;i<=n;i++){
		cin>>e[i];
	}
	for(ll i=1;i<=m;i++){
		ll u,v;
		cin>>u>>v;
		u++,v++;
		deg[u]++;
		adj[v].push_back(u);
	}
	queue<ll>q;
	for(ll i=1;i<=n;i++){
		if(!deg[i]){
			reach[i]=1;
			vis[i]=1;
			q.push(i);
		}
	}
	while(!q.empty()){
		ll u=q.front();
		q.pop();
		for(ll v:adj[u]){
			if(!vis[v]){
				deg[v]--;
				ll now=reach[u];
				if(!e[u] && e[v]) now++;
				reach[v]=max(reach[v],now);
				if(!deg[v]){
					vis[v]=1;
					q.push(v);
				}
			}
		}
	}
	ll ans=0;
	for(ll i=1;i<=n;i++){
		ans=max(ans,reach[i]);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define N 100005
using namespace std;
int n,m,cnt,head[N];
int a[N],ru[N],ans;
struct edge{
	int to,next;
}e[N];
inline void add(int x,int y){
	e[++cnt].to=y;
	e[cnt].next=head[x];
	head[x]=cnt;
}

queue<int> qmain,qco;

inline void topo(){
	for(int i=1;i<=n;i++){
		if(!ru[i]){
			if(!a[i]) qmain.push(i);
			else qco.push(i);
		}
	}
	while((!qmain.empty())||(!qco.empty())){
		while(!qmain.empty()){//优先处理主处理器能处理的
			int u=qmain.front();
			qmain.pop();
			for(int i=head[u];i;i=e[i].next){
				int v=e[i].to;
				ru[v]--;
				if(!ru[v]){
					if(!a[v]) qmain.push(v);
					else qco.push(v);
				}
			}
		}
		if(!qco.empty())
			ans++;//如果需要副处理器处理的话就将处理次数+1
		while(!qco.empty()){//一次性把所有能一起处理的任务都处理掉
			int u=qco.front();
			qco.pop();
			for(int i=head[u];i;i=e[i].next){
				int v=e[i].to;
				ru[v]--;
				if(!ru[v]){
					if(!a[v]) qmain.push(v);
					else qco.push(v);
				}
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=1,y,x;i<=m;i++){
		scanf("%d%d",&y,&x);//注意输入顺序与实际操作要求顺序相反
		add(x+1,y+1);ru[y+1]++;
	}
	topo();
	printf("%d",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
struct Edge{
    int from,to,nex;
}edge[N];
int h[N],idx;
void add(int u,int v)
{
    Edge E={u,v,h[u]};
    edge[idx]=E;
    h[u]=idx++;
}int n,m;
queue<int> qmain,qco;
int a[N],ru[N];
int ans=1;
void tpsort()
{
    for(int i=1;i<=n;i++)
    {
        if(!ru[i])
        {
           if(!a[i])qmain.push(i);
           else qco.push(i);
        }
    }
   while((!qmain.empty())||(!qco.empty()))
    {
        	while(!qmain.empty())
        	{
        	    int u=qmain.front();
        	    qmain.pop();
        	    for(int i=h[u];~i;i=edge[i].nex)
        	    {
        	         int v=edge[i].to;
        	         ru[v]--;
        	         if(!ru[v])
        	         {
        	             if(!a[v])qmain.push(v);
        	             else qco.push(v);
        	         }
        	    }
        	}
        	if(!qco.empty())
        	    ans++;
        	while(!qco.empty())
        	{
        	    int u=qco.front();
        	    qco.pop();
        	    for(int i=h[u];~i;i=edge[i].nex)
        	    {
        	        int v=edge[i].to;
        	         ru[v]--;
        	        if(!ru[v])
        	        {
        	            if(!a[v])qco.push(v);
        	            else qco.push(v);
        	        }
        	    }
        	}

    }

}
int main()
{
    cin>>n>>m;
 memset(h,-1,sizeof(h));
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
  for(int i=1;i<=m;i++)
  {
      int x,y;
     	scanf("%d%d",&y,&x);//注意输入顺序与实际操作要求顺序相反
	add(x+1,y+1);ru[y+1]++;
  }
    tpsort();
    	printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
struct Edge{
    int from,to,nex;
}edge[N];
int h[N],idx;
void add(int u,int v)
{
    Edge E={u,v,h[u]};
    edge[idx]=E;
    h[u]=idx++;
}int n,m;
queue<int> qmain,qco;
int a[N],ru[N];
int ans=0;
void tpsort()
{
    for(int i=1;i<=n;i++)
    {
        if(!ru[i])
        {
           if(!a[i])qmain.push(i);
           else qco.push(i);
        }
    }
   while((!qmain.empty())||(!qco.empty()))
    {
        	while(!qmain.empty())
        	{
        	    int u=qmain.front();
        	    qmain.pop();
        	    for(int i=h[u];~i;i=edge[i].nex)
        	    {
        	         int v=edge[i].to;
        	         ru[v]--;
        	         if(!ru[v])
        	         {
        	             if(!a[v])qmain.push(v);
        	             else qco.push(v);
        	         }
        	    }
        	}
        	if(!qco.empty())
        	    ans++;
        	while(!qco.empty())
        	{
        	    int u=qco.front();
        	    qco.pop();
        	    for(int i=h[u];~i;i=edge[i].nex)
        	    {
        	        int v=edge[i].to;
        	         ru[v]--;
        	        if(!ru[v])
        	        {
        	            if(!a[v])qco.push(v);
        	            else qco.push(v);
        	        }
        	    }
        	}

    }

}
int main()
{
    cin>>n>>m;
 memset(h,-1,sizeof(h));
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
  for(int i=1;i<=m;i++)
  {
      int x,y;
     	scanf("%d%d",&y,&x);//注意输入顺序与实际操作要求顺序相反
	add(x+1,y+1);ru[y+1]++;
  }
    tpsort();
    	printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
struct Edge{
    int  to,nex;
}edge[N];
int h[N],idx;
void add(int u,int v)
{
    Edge E={ v,h[u]};
    edge[idx]=E;
    h[u]=idx++;
}int n,m;
queue<int> qmain,qco;
int a[N],ru[N];
int ans=0;
void tpsort()
{
    for(int i=1;i<=n;i++)
    {
        if(!ru[i])
        {
           if(!a[i])qmain.push(i);
           else qco.push(i);
        }
    }
   while((!qmain.empty())||(!qco.empty()))
    {
        	while(!qmain.empty())
        	{
        	    int u=qmain.front();
        	    qmain.pop();
        	    for(int i=h[u];~i;i=edge[i].nex)
        	    {
        	         int v=edge[i].to;
        	         ru[v]--;
        	         if(!ru[v])
        	         {
        	             if(!a[v])qmain.push(v);
        	             else qco.push(v);
        	         }
        	    }
        	}
        	if(!qco.empty())
        	    ans++;
        	while(!qco.empty())
        	{
        	    int u=qco.front();
        	    qco.pop();
        	    for(int i=h[u];~i;i=edge[i].nex)
        	    {
        	        int v=edge[i].to;
        	         ru[v]--;
        	        if(!ru[v])
        	        {
        	            if(!a[v])qmain.push(v);
        	            else qco.push(v);
        	        }
        	    }
        	}

    }

}
int main()
{
    cin>>n>>m;
 memset(h,-1,sizeof(h));
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
  for(int i=1;i<=m;i++)
  {
      int x,y;
     	scanf("%d%d",&y,&x);//注意输入顺序与实际操作要求顺序相反
	add(x+1,y+1);ru[y+1]++;
  }
    tpsort();
    	printf("%d",ans);
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define ll long long
#define vi vector <int>
#define vii vector <pair<int,int> >
#define rep(i,a,b) for (int i=a;i<b;i++)
#define repD(i,a,b) for (int i=a;i>b;i--)
#define pb push_back
#define pii pair <int,int>
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define MOD 1000000007
//#define PI 3.14159265359
typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#define deb(...) {...}
#ifndef local
	#define deb(...) ;
#endif

///----------------------------------------------------------------------------------

int n,m,E[100005],moth[100005];

vi adj[100005], Radj[100005],TS,vis(100005,0),ans(100005,0);

void dfs0(int so)
{
	vis[so]++;
	for (auto x:adj[so])if (!vis[x])dfs0(x);
	TS.pb(so);
}

void dfs1(int so)
{
	vis[so]++;
	for (auto x:adj[so])if (!vis[x])dfs1(x);
}

void dfs2(int so)
{
	vis[so]++;
	for (auto x:adj[so])if (!vis[x])dfs2(x);

	int best=0;
	for (auto x:adj[so])
	{
		if (E[so]==0)
		{
			best=max(best,ans[x]);
		}
		else
		{
			if (E[x])best=max(best,ans[x]);
			else best=max(best,1+ans[x]);
		}
	}
	ans[so]=best;
	if (E[so])ans[so]=max(ans[so],1ll);
}
void solve()
{
	cin>>n>>m;
	int u,v;
	rep(i,0,n)cin>>E[i];
	rep(i,0,m)cin>>u>>v,adj[u].pb(v),Radj[v].pb(u);

	rep(i,0,n)if (!vis[i])dfs0(i);
	reverse(all(TS));

	fill(all(vis),0);
	memset(moth,0,sizeof moth);
	rep(i,0,n)if (!vis[i])dfs1(TS[i]),moth[TS[i]]=true;

	fill(all(vis),0);
	rep(i,0,n)if (moth[i])dfs2(i);

	cout<<*max_element(all(ans));

}

signed main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);

 	#ifdef local
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    clock_t qwertyuiop = clock();
    #endif

 	int t=1;
    //cin>>t;

    rep(a,0,t)
    {
        ///cout<<"Case #"<<a+1<<": ";
        solve();
	}

    #ifdef local
    {qwertyuiop = clock() - qwertyuiop;double time_taken = ((double)qwertyuiop)/CLOCKS_PER_SEC;cerr<<fixed<<setprecision(5)<<"\n\nTime Taken : "<<time_taken<<" seconds\n";}
	#endif
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define ll long long
#define vi vector <int>
#define vii vector <pair<int,int> >
#define rep(i,a,b) for (int i=a;i<b;i++)
#define repD(i,a,b) for (int i=a;i>b;i--)
#define pb push_back
#define pii pair <int,int>
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define MOD 1000000007
//#define PI 3.14159265359
typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#define deb(...) {...}
#ifndef local
	#define deb(...) ;
#endif

///----------------------------------------------------------------------------------

int n,m;
vi adj[100005], TS, vis(100005,0), ans(100005,0), E(100005,0), moth(100005,0);

void dfs0(int so)
{
	vis[so]++;
	for (auto x:adj[so])if (!vis[x])dfs0(x);
	TS.pb(so);
}

void dfs1(int so)
{
	vis[so]++;
	for (auto x:adj[so])if (!vis[x])dfs1(x);
}

void dfs2(int so)
{
	vis[so]++;
	for (auto x:adj[so])if (!vis[x])dfs2(x);

	int best=0;
	for (auto x:adj[so])
	{
		if (E[so]==0)
		{
			best=max(best,ans[x]);
		}
		else
		{
			if (E[x])best=max(best,ans[x]);
			else best=max(best,1+ans[x]);
		}
	}
	ans[so]=best;
	if (E[so])ans[so]=max(ans[so],1ll);
}
void solve()
{
	cin>>n>>m;
	int u,v;
	rep(i,0,n)cin>>E[i];
	rep(i,0,m)cin>>u>>v,adj[u].pb(v);

	rep(i,0,n)if (!vis[i])dfs0(i);
	reverse(all(TS));

	fill(all(vis),0);
	rep(i,0,n)if (!vis[i])dfs1(TS[i]),moth[TS[i]]=true;

	fill(all(vis),0);
	rep(i,0,n)if (!vis[i])dfs2(i);

	cout<<*max_element(all(ans));

}

signed main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);

 	#ifdef local
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    clock_t qwertyuiop = clock();
    #endif

 	int t=1;
    //cin>>t;

    rep(a,0,t)
    {
        ///cout<<"Case #"<<a+1<<": ";
        solve();
	}

    #ifdef local
    {qwertyuiop = clock() - qwertyuiop;double time_taken = ((double)qwertyuiop)/CLOCKS_PER_SEC;cerr<<fixed<<setprecision(5)<<"\n\nTime Taken : "<<time_taken<<" seconds\n";}
	#endif
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define ll long long
#define vi vector <int>
#define vii vector <pair<int,int> >
#define rep(i,a,b) for (int i=a;i<b;i++)
#define repD(i,a,b) for (int i=a;i>b;i--)
#define pb push_back
#define pii pair <int,int>
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define MOD 1000000007
//#define PI 3.14159265359
typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#define deb(...) {...}
#ifndef local
	#define deb(...) ;
#endif

///----------------------------------------------------------------------------------

int n,m;
vi adj[100005], TS, vis(100005,0), ans(100005,0), E(100005,0);

void dfs(int so)
{
	vis[so]++;
	for (auto x:adj[so])if (!vis[x])dfs(x);

	int best=0;
	for (auto x:adj[so])
	{
		if (E[so]==0)
		{
			best=max(best,ans[x]);
		}
		else
		{
			if (E[x])best=max(best,ans[x]);
			else best=max(best,1+ans[x]);
		}
	}
	ans[so]=best;
	if (E[so])ans[so]=max(ans[so],1ll);
}
void solve()
{
	cin>>n>>m;
	int u,v;
	rep(i,0,n)cin>>E[i];
	rep(i,0,m)cin>>u>>v,adj[u].pb(v);

	rep(i,0,n)if (!vis[i])dfs(i);

	cout<<*max_element(all(ans));

}

signed main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);

 	#ifdef local
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    clock_t qwertyuiop = clock();
    #endif

 	int t=1;
    //cin>>t;

    rep(a,0,t)
    {
        ///cout<<"Case #"<<a+1<<": ";
        solve();
	}

    #ifdef local
    {qwertyuiop = clock() - qwertyuiop;double time_taken = ((double)qwertyuiop)/CLOCKS_PER_SEC;cerr<<fixed<<setprecision(5)<<"\n\nTime Taken : "<<time_taken<<" seconds\n";}
	#endif
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr first
#define sc second

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
   // freopen("input.txt","r", stdin);
    //freopen("output.txt","w",stdout);

    int n ,m ; cin>>n>>m ;
    int pro[n] = {} ;
    vector<int> adj[n] = {} ;
    int indeg[n] = {} ;
    int le[n] = {} ;

    for(int i = 0 ;i<n ; i++)
    {
        cin>>pro[i] ;
    }

    for(int i = 0 ; i<m ;i++)
    {
        int a, b ; cin>>a>>b ;
        indeg[a]++ ;
        adj[b].push_back(a) ;
    }

    int ans = 0 ;

    queue<int> q ;

    for(int i = 0 ; i<n ; i++)
    {
        if(indeg[i]) continue ;

        le[i] = pro[i] ;
        q.push(i) ;
    }

    while(!q.empty())
    {
        int b = q.front() ; q.pop() ;
        ans = max(ans,le[b]) ;

        for(auto u : adj[b])
        {
            indeg[u]-- ;
            if(!indeg[u]) q.push(u) ;

            if(pro[u]^pro[b] == 0)
            {
                le[u] = max(le[u],le[b]) ;
                continue ;
            }

            if(pro[u] == 0 and pro[b] == 1 )
            {
                le[u] = max(le[u],le[b]) ;
                continue ;
            }

            le[u] = max(le[u],le[b]+1) ;
        }
    }

    cout<<ans ;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr first
#define sc second

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
   // freopen("input.txt","r", stdin);
    //freopen("output.txt","w",stdout);

    int n ,m ; cin>>n>>m ;
    int pro[n] = {} ;
    vector<int> adj[n] = {} ;
    int indeg[n] = {} ;
    int le[n] = {} ;

    // this code is not copied , i repeat this code is not copied
    for(int i = 0 ;i<n ; i++)
    {
        cin>>pro[i] ;
    }

    for(int i = 0 ; i<m ;i++)
    {
        int a, b ; cin>>a>>b ;
        indeg[a]++ ;
        adj[b].push_back(a) ;
    }

    int ans = 0 ;

    queue<int> q ;

    for(int i = 0 ; i<n ; i++)
    {
        if(indeg[i]) continue ;

        le[i] = pro[i] ;
        q.push(i) ;
    }

    while(!q.empty())
    {
        int b = q.front() ; q.pop() ;
        ans = max(ans,le[b]) ;

        for(auto u : adj[b])
        {
            indeg[u]-- ;
            if(!indeg[u]) q.push(u) ;

            if(pro[u]^pro[b] == 0)
            {
                le[u] = max(le[u],le[b]) ;
                continue ;
            }

            if(pro[u] == 0 and pro[b] == 1 )
            {
                le[u] = max(le[u],le[b]) ;
                continue ;
            }

            le[u] = max(le[u],le[b]+1) ;
        }
    }

    cout<<ans ;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ff first
#define ss second
#define endl "\n"
#define MOD 1000000007
#define MOD1 998244353
#define inf LLONG_MAX

ll GCD(ll x,ll y){if(y==0)return x;return GCD(y,x%y);}
ll LCM(ll x,ll y){return (x*y)/(GCD(x,y));}
ll LOGK(ll x,ll k){if(x>=k)return 1+LOGK(x/k,k);return 0;}
ll MPOW( ll a, ll b, ll m) { if(b==0) return 1; ll x=MPOW(a,b/2,m); x=(x*x)%m; if(b%2==1) x=(x*a)%m; return x;}
ll MINV(ll a,ll m) {return MPOW(a,m-2,m);}

class pnc{
  ll FACT_MAX,MODU;
  vector<ll> fact;
public:
  pnc( ll n, ll m) { FACT_MAX = n; fact.resize(FACT_MAX); MODU = m; MFACT_INIT(MODU);}
  void MFACT_INIT( ll m) { fact[0]=1; for(ll i=1;i<FACT_MAX;++i) fact[i]=(i*fact[i-1])%MODU;}
  ll MFACT( ll n) { return fact[n];}
  ll PERM( ll n, ll r) { return (fact[n]*::MINV(fact[n-r],MODU))%MODU;}
  ll COMB( ll n , ll r) { return (PERM(n,r)*::MINV(fact[r],MODU))%MODU;}
};

ll n,m;
ll ar[100005];
vector<ll> adj[100005];
ll indeg[100005]={0};
bool visited[100005]={false};
ll calls=0;

void dfs(ll node,ll pre)
{
    visited[node]=true;

    if(ar[node]==0 && pre!=0)
    calls++;

    for(auto i:adj[node])
    {
        if(!visited[i])
        dfs(i,ar[node]);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll test=1;
    //cin>>test;
    while(test--)
    {
        cin>>n>>m;

        for(ll i=0;i<n;i++)
        cin>>ar[i];

        for(ll i=0;i<m;i++)
        {
            ll x,y;
            cin>>x>>y;

            adj[x].pb(y);
            indeg[y]++;
        }

        vector<ll> root;

        for(ll i=0;i<n;i++)
        {
            if(indeg[i]==0)
            root.pb(i);
        }

        for(auto i:root)
        {
            dfs(i,1);
        }

        cout<<calls;
    }
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ff first
#define ss second
#define endl "\n"
#define MOD 1000000007
#define MOD1 998244353
#define inf LLONG_MAX

ll GCD(ll x,ll y){if(y==0)return x;return GCD(y,x%y);}
ll LCM(ll x,ll y){return (x*y)/(GCD(x,y));}
ll LOGK(ll x,ll k){if(x>=k)return 1+LOGK(x/k,k);return 0;}
ll MPOW( ll a, ll b, ll m) { if(b==0) return 1; ll x=MPOW(a,b/2,m); x=(x*x)%m; if(b%2==1) x=(x*a)%m; return x;}
ll MINV(ll a,ll m) {return MPOW(a,m-2,m);}

class pnc{
  ll FACT_MAX,MODU;
  vector<ll> fact;
public:
  pnc( ll n, ll m) { FACT_MAX = n; fact.resize(FACT_MAX); MODU = m; MFACT_INIT(MODU);}
  void MFACT_INIT( ll m) { fact[0]=1; for(ll i=1;i<FACT_MAX;++i) fact[i]=(i*fact[i-1])%MODU;}
  ll MFACT( ll n) { return fact[n];}
  ll PERM( ll n, ll r) { return (fact[n]*::MINV(fact[n-r],MODU))%MODU;}
  ll COMB( ll n , ll r) { return (PERM(n,r)*::MINV(fact[r],MODU))%MODU;}
};

ll n,m;
ll ar[100005];
vector<ll> adj[100005];
ll indeg[100005]={0};
bool visited[100005]={false};
ll dp[100005]={0};

void dfs(ll node)
{
    visited[node]=true;

    dp[node]=1-ar[node];

    for(auto i:adj[node])
    {
        if(!visited[i])
        dfs(i);

        if(ar[node]==0)
        {
            dp[node]=max(dp[node],dp[i]+(ar[i]==1));
        }
        else
        {
            dp[node]=max(dp[node],dp[i]);
        }

    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll test=1;
    //cin>>test;
    while(test--)
    {
        cin>>n>>m;

        for(ll i=0;i<n;i++)
        {
            cin>>ar[i];
            ar[i]=1-ar[i];
        }

        for(ll i=0;i<m;i++)
        {
            ll x,y;
            cin>>x>>y;

            adj[x].pb(y);
        }

        for(ll i=0;i<n;i++)
        {
            if(!visited[i])
            dfs(i);
        }

        ll ans=0;

        for(ll i=0;i<n;i++)
        ans=max(ans,dp[i]);

        cout<<ans;

    }
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ff first
#define ss second
#define endl "\n"
#define MOD 1000000007
#define MOD1 998244353
#define inf LLONG_MAX

ll GCD(ll x,ll y){if(y==0)return x;return GCD(y,x%y);}
ll LCM(ll x,ll y){return (x*y)/(GCD(x,y));}
ll LOGK(ll x,ll k){if(x>=k)return 1+LOGK(x/k,k);return 0;}
ll MPOW( ll a, ll b, ll m) { if(b==0) return 1; ll x=MPOW(a,b/2,m); x=(x*x)%m; if(b%2==1) x=(x*a)%m; return x;}
ll MINV(ll a,ll m) {return MPOW(a,m-2,m);}

class pnc{
  ll FACT_MAX,MODU;
  vector<ll> fact;
public:
  pnc( ll n, ll m) { FACT_MAX = n; fact.resize(FACT_MAX); MODU = m; MFACT_INIT(MODU);}
  void MFACT_INIT( ll m) { fact[0]=1; for(ll i=1;i<FACT_MAX;++i) fact[i]=(i*fact[i-1])%MODU;}
  ll MFACT( ll n) { return fact[n];}
  ll PERM( ll n, ll r) { return (fact[n]*::MINV(fact[n-r],MODU))%MODU;}
  ll COMB( ll n , ll r) { return (PERM(n,r)*::MINV(fact[r],MODU))%MODU;}
};

ll n,m;
ll ar[100005];
vector<ll> adj[100005];
ll indeg[100005]={0};
bool visited[100005]={false};
ll dp[100005]={0};

void dfs(ll node)
{
    visited[node]=true;

    if(ar[node]==0)
    dp[node]=1;

    for(auto i:adj[node])
    {
        if(!visited[i])
        dfs(i);

        if(ar[node]==0)
        {
            dp[node]=max(dp[node],dp[i]+(ar[i]==1));
        }
        else
        {
            dp[node]=max(dp[node],dp[i]);
        }

    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll test=1;
    //cin>>test;
    while(test--)
    {
        cin>>n>>m;

        for(ll i=0;i<n;i++)
        cin>>ar[i];

        for(ll i=0;i<m;i++)
        {
            ll x,y;
            cin>>x>>y;

            adj[x].pb(y);
            indeg[y]++;
        }

        vector<ll> root;

        for(ll i=0;i<n;i++)
        {
            if(indeg[i]==0)
            root.pb(i);
        }

        for(auto i:root)
        {
            dfs(i);
        }

        ll ans=0;

        for(ll i=0;i<n;i++)
        ans=max(ans,dp[i]);

        cout<<ans;

    }
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ff first
#define ss second
#define endl "\n"
#define MOD 1000000007
#define MOD1 998244353
#define inf LLONG_MAX

ll GCD(ll x,ll y){if(y==0)return x;return GCD(y,x%y);}
ll LCM(ll x,ll y){return (x*y)/(GCD(x,y));}
ll LOGK(ll x,ll k){if(x>=k)return 1+LOGK(x/k,k);return 0;}
ll MPOW( ll a, ll b, ll m) { if(b==0) return 1; ll x=MPOW(a,b/2,m); x=(x*x)%m; if(b%2==1) x=(x*a)%m; return x;}
ll MINV(ll a,ll m) {return MPOW(a,m-2,m);}

class pnc{
  ll FACT_MAX,MODU;
  vector<ll> fact;
public:
  pnc( ll n, ll m) { FACT_MAX = n; fact.resize(FACT_MAX); MODU = m; MFACT_INIT(MODU);}
  void MFACT_INIT( ll m) { fact[0]=1; for(ll i=1;i<FACT_MAX;++i) fact[i]=(i*fact[i-1])%MODU;}
  ll MFACT( ll n) { return fact[n];}
  ll PERM( ll n, ll r) { return (fact[n]*::MINV(fact[n-r],MODU))%MODU;}
  ll COMB( ll n , ll r) { return (PERM(n,r)*::MINV(fact[r],MODU))%MODU;}
};

ll n,m;
ll ar[100005];
vector<ll> adj[100005];
ll indeg[100005]={0};
bool visited[100005]={false};
ll calls=0;

void dfs(ll node,ll pre)
{
    visited[node]=true;

    if(ar[node]==0 && pre!=0)
    calls++;

    for(auto i:adj[node])
    {
        if(!visited[i])
        dfs(i,ar[node]);
        else
        {
            if(ar[i]==0)
            calls--;
        }

    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll test=1;
    //cin>>test;
    while(test--)
    {
        cin>>n>>m;

        for(ll i=0;i<n;i++)
        cin>>ar[i];

        for(ll i=0;i<m;i++)
        {
            ll x,y;
            cin>>x>>y;

            adj[x].pb(y);
            indeg[y]++;
        }

        vector<ll> root;

        for(ll i=0;i<n;i++)
        {
            if(indeg[i]==0)
            root.pb(i);
        }

        for(auto i:root)
        {
            dfs(i,1);
        }

        cout<<calls;
    }
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ff first
#define ss second
#define endl "\n"
#define MOD 1000000007
#define MOD1 998244353
#define inf LLONG_MAX

ll GCD(ll x,ll y){if(y==0)return x;return GCD(y,x%y);}
ll LCM(ll x,ll y){return (x*y)/(GCD(x,y));}
ll LOGK(ll x,ll k){if(x>=k)return 1+LOGK(x/k,k);return 0;}
ll MPOW( ll a, ll b, ll m) { if(b==0) return 1; ll x=MPOW(a,b/2,m); x=(x*x)%m; if(b%2==1) x=(x*a)%m; return x;}
ll MINV(ll a,ll m) {return MPOW(a,m-2,m);}

class pnc{
  ll FACT_MAX,MODU;
  vector<ll> fact;
public:
  pnc( ll n, ll m) { FACT_MAX = n; fact.resize(FACT_MAX); MODU = m; MFACT_INIT(MODU);}
  void MFACT_INIT( ll m) { fact[0]=1; for(ll i=1;i<FACT_MAX;++i) fact[i]=(i*fact[i-1])%MODU;}
  ll MFACT( ll n) { return fact[n];}
  ll PERM( ll n, ll r) { return (fact[n]*::MINV(fact[n-r],MODU))%MODU;}
  ll COMB( ll n , ll r) { return (PERM(n,r)*::MINV(fact[r],MODU))%MODU;}
};

ll n,m;
ll ar[100005];
vector<ll> adj[100005];
ll indeg[100005]={0};
bool visited[100005]={false};
ll dp[100005]={0};

void dfs(ll node)
{
    visited[node]=true;

    if(ar[node]==0)
    dp[node]=1;

    for(auto i:adj[node])
    {
        if(!visited[i])
        dfs(i);

        if(ar[node]==0)
        {
            dp[node]=max(dp[node],dp[i]+(ar[i]==1));
        }
        else
        {
            dp[node]=max(dp[node],dp[i]);
        }

    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll test=1;
    //cin>>test;
    while(test--)
    {
        cin>>n>>m;

        for(ll i=0;i<n;i++)
        cin>>ar[i];

        for(ll i=0;i<m;i++)
        {
            ll x,y;
            cin>>x>>y;

            adj[x].pb(y);
        }

        for(ll i=0;i<n;i++)
        {
            if(!visited[i])
            dfs(i);
        }

        ll ans=0;

        for(ll i=0;i<n;i++)
        ans=max(ans,dp[i]);

        cout<<ans;

    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;

template <class T> using min_queue = priority_queue<T, vector<T>, greater<T>>;

template<typename Args>
void kill(Args args)
{
    cout << args << "\n";
    exit(0);
}

#define add(x) emplace_back(x)
#define pb(x) push_back(x)
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define fill(a, v) memset(a, v, sizeof a)
#define debug1(x) cerr << "at line " << __LINE__ << ": " << #x << " = " << (x) << endl
#define debug2(x) cerr << #x << " = " << (x) << endl

const double PI = acos(-1);
const ll MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;

const int N=101010;
int arr[N],dp[N],vis[N],out[N];
vector<int>rg[N];
queue<int>cp,mp;
int n,m;
int ans=0;

void solve(){
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>arr[i];
    for(int i=1;i<=m;i++){
        int u,v;
        cin>>u>>v;
        ++u,++v;
        rg[v].add(u);
        out[u]++;
    }
    for(int i=1;i<=n;i++){
        if(!out[i]){
            if(arr[i]){
                cp.push(i);
            }
            else{
                mp.push(i);
            }
        }
    }
    while(cp.size()||mp.size()){
        while(mp.size()){
            int now=mp.front();
            mp.pop();
            for(int prev:rg[now]){
               out[prev]--;
               if(!out[prev]){
                   if(arr[prev]){
                       cp.push(prev);
                   }
                   else mp.push(prev);
               }
            }
        }
        if(cp.size()){
            ans++;
            while(cp.size()){
                int now=cp.front();
                cp.pop();
                for(int prev:rg[now]){
                    out[prev]--;
                    if(!out[prev]){
                        if(arr[prev]){
                            cp.push(prev);
                        }
                        else mp.push(prev);
                    }
                }
            }
        }
    }
    cout<<ans<<endl;
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cout<<fixed<<setprecision(12);
    int testcase=1;
    //cin>>testcase;
    while(testcase--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;

template <class T> using min_queue = priority_queue<T, vector<T>, greater<T>>;

template<typename Args>
void kill(Args args)
{
    cout << args << "\n";
    exit(0);
}

#define add(x) emplace_back(x)
#define pb(x) push_back(x)
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define fill(a, v) memset(a, v, sizeof a)
#define debug1(x) cerr << "at line " << __LINE__ << ": " << #x << " = " << (x) << endl
#define debug2(x) cerr << #x << " = " << (x) << endl

const double PI = acos(-1);
const ll MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;

const int N=101010;
int arr[N],dp[N],vis[N],out[N];
vector<int>rg[N];
queue<int>cp,mp;
int n,m;
int ans=0;

void solve(){
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>arr[i];
    for(int i=1;i<=m;i++){
        int u,v;
        cin>>u>>v;
        ++u,++v;
        rg[v].add(u);
        out[u]++;
    }
    for(int i=1;i<=n;i++){
        if(!out[i]){
            if(arr[i]){
                cp.push(i);
            }
            else{
                mp.push(i);
            }
        }
    }
    while(cp.size()||mp.size()){
        while(mp.size()){
            int now=mp.front();
            mp.pop();
            for(int prev:rg[now]){
               out[prev]--;
               if(!out[prev]){
                   if(arr[prev]){
                       cp.push(prev);
                   }
                   else mp.push(prev);
               }
            }
        }
        if(cp.size()){
            ans++;
            while(cp.size()){
                int now=cp.front();
                cp.pop();
                for(int prev:rg[now]){
                    out[prev]--;
                    if(!out[prev]){
                        if(arr[prev]){
                            cp.push(prev);
                        }
                        else mp.push(prev);
                    }
                }
            }
        }
    }
    cout<<ans<<endl;
}

void precompute(){

}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cout<<fixed<<setprecision(12);
    int testcase=1;
    clock_t zzzx=clock();
    //cin>>testcase;
    precompute();
    while(testcase--){
        solve();
    }
    double elapsed_time=(double)(clock()-zzzx)/CLOCKS_PER_SEC;
    debug2(elapsed_time);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int dp[100009],in[100009];
vector<int>v[100009];
int dfs(int x)
{
if(dp[x]!=-1)return dp[x];
dp[x]=0;
for(int i=0;i<v[x].size();i++)
{
if(in[v[x][i]])dp[x]=max(dp[x],dfs(v[x][i]));
else dp[x]=max(dp[x],dfs(v[x][i])+in[x]);
}
if(v[x].size()==0)dp[x]=in[x];
return dp[x];
}
int n,m;
int main()
{
memset(dp,-1,sizeof(dp));
cin>>n>>m;
for(int i=0;i<n;i++)cin>>in[i];
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
v[y].push_back(x);
}
int mx=0;
for(int i=0;i<n;i++){
mx=max(mx,dfs(i));
}
cout<<mx;
}


#include<bits/stdc++.h>
using namespace std;
int dp[100009],in[100009];
vector<int>v[100009];
int dfs(int x)
{
if(dp[x]!=-1)return dp[x];
dp[x]=0;
for(int i=0;i<v[x].size();i++)
{
dp[x]=max(dp[x],dfs(v[x][i]));
}
if(in[x])dp[x]++;
return dp[x];
}
int n,m;
int main()
{
memset(dp,-1,sizeof(dp));
cin>>n>>m;
for(int i=0;i<n;i++)cin>>in[i];
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
v[y].push_back(x);
}
int mx=0;
for(int i=0;i<n;i++)mx=max(mx,dfs(i));
cout<<mx;
}


#include<bits/stdc++.h>
using namespace std;
int dp[100009],in[100009];
vector<int>v[100009];
int dfs(int x)
{
if(dp[x]!=-1)return dp[x];
dp[x]=0;
for(int i=0;i<v[x].size();i++)
{
if(in[v[x][i]])dp[x]=max(dp[x],dfs(v[x][i])+1);
else dp[x]=max(dp[x],dfs(v[x][i])+in[x]);
}
if(v[x].size()==0)dp[x]=in[x];
return dp[x];
}
int n,m;
int main()
{
memset(dp,-1,sizeof(dp));
cin>>n>>m;
for(int i=0;i<n;i++)cin>>in[i];
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
v[y].push_back(x);
}
int mx=0;
for(int i=0;i<n;i++){
mx=max(mx,dfs(i));
}
cout<<mx;


#include<bits/stdc++.h>
using namespace std;
int dp[100009],in[100009];
vector<int>v[100009];
int dfs(int x)
{
if(dp[x]!=-1)return dp[x];
dp[x]=0;
for(int i=0;i<v[x].size();i++)
{
dp[x]=max(dp[x],dfs(v[x][i]));
}
if(in[x]==0)dp[x]++;
return dp[x];
}
int n,m;
int main()
{
memset(dp,-1,sizeof(dp));
cin>>n>>m;
for(int i=0;i<n;i++)cin>>in[i];
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
v[y].push_back(x);
}
int mx=0;
for(int i=0;i<n;i++){
mx=max(mx,dfs(i));
}
cout<<mx;
}


#include<bits/stdc++.h>
using namespace std;
int dp[100009],in[100009];
vector<int>v[100009];
int dfs(int x)
{
if(dp[x]!=-1)return dp[x];
dp[x]=0;
for(int i=0;i<v[x].size();i++)
{
if(in[v[x][i]])dp[x]=max(dp[x],dfs(v[x][i])+1);
else dp[x]=max(dp[x],dfs(v[x][i])+in[x]);
}
if(v[x].size()==0)dp[x]=in[x];
return dp[x];
}
int n,m;
int main()
{
memset(dp,-1,sizeof(dp));
cin>>n>>m;
for(int i=0;i<n;i++)cin>>in[i];
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
v[y].push_back(x);
}
int mx=0;
for(int i=0;i<n;i++){
mx=max(mx,dfs(i));
}
cout<<mx;
}


#include<bits/stdc++.h>
using namespace std;
int dp[100009],in[100009];
vector<int>v[100009];
int dfs(int x)
{
if(dp[x]!=-1)return dp[x];
dp[x]=0;
for(int i=0;i<v[x].size();i++)
{
   if(in[v[x][i]])dp[x]=max(dp[x],dfs(v[x][i]));
   else dp[x]=max(dp[x],dfs(v[x][i]+in[x]);
}
if(v[x].size()==0)dp[x]=in[x];
return dp[x];
}
int n,m;
int main()
{
memset(dp,-1,sizeof(dp));
cin>>n>>m;
for(int i=0;i<n;i++)cin>>in[i];
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
v[y].push_back(x);
}
int mx=0;
for(int i=0;i<n;i++){
mx=max(mx,dfs(i));
}
cout<<mx;
}


#include<bits/stdc++.h>
using namespace std;
int dp[100009],in[100009];
vector<int>v[100009];
int dfs(int x)
{
if(dp[x]!=-1)return dp[x];
dp[x]=0;
for(int i=0;i<v[x].size();i++)
{
if(in[v[x][i])dp[x]=max(dp[x],dfs(v[x][i])+1);
else dp[x]=max(dp[x],dfs(v[x][i])+in[x]);
}
if(v[x].size()==0)dp[x]=in[x];
return dp[x];
}
int n,m;
int main()
{
memset(dp,-1,sizeof(dp));
cin>>n>>m;
for(int i=0;i<n;i++)cin>>in[i];
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
v[y].push_back(x);
}
int mx=0;
for(int i=0;i<n;i++){
mx=max(mx,dfs(i));
}
cout<<mx;


#include<bits/stdc++.h>
using namespace std;
int dp[100009],in[100009];
vector<int>v[100009];
int dfs(int x)
{
if(dp[x]!=-1)return dp[x];
dp[x]=0;
for(int i=0;i<v[x].size();i++)
{
dp[x]=max(dp[x],dfs(x));
}
if(in[x])dp[x]++;
}
return dp[x];
}
int n,m;
int main()
{
memset(dp,-1,sizeof(dp));
cin>>n>>m;
for(int i=0;i<n;i++)cin>>in[i];
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
v[y].push_back(x);
}
int mx=0;
for(int i=0;i<n;i++)mx=max(mx,dfs(i));
cout<<mx;
}


#include<bits/stdc++.h>
using namespace std;
int dp[100009],in[100009];
vector<int>v[100009];
int dfs(int x)
{
if(dp[x]!=-1)return dp[x];
dp[x]=0;
for(int i=0;i<v[x].size();i++)
{
dp[x]=max(dp[x],dfs(v[x][i]));
}
if(in[x])dp[x]++;
return dp[x];
}
int n,m;
int main()
{
memset(dp,-1,sizeof(dp));
cin>>n>>m;
for(int i=0;i<n;i++)cin>>in[i];
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
v[y].push_back(x);
}
int mx=0;
for(int i=0;i<n;i++){
mx=max(mx,dfs(i));
}
cout<<mx;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long

#define check(c)       cout<<#c<<" : "<<c<<endl;
#define check2(a, b)    cout<<#a<<" : "<<a<<" | "<<#b<<" : "<<b<<endl;
#define check3(a, b, c)  cout<<#a<<" : "<<a<<" | "<<#b<<" : "<<b<<" | "<<#c<<" : "<<c<<endl;

int n, m;
vector<int> graph[100010], order, pro(100010);

void topoSort(){
    vector<int> indeg(n + 1);
    for(int i = 1; i <= n; i++){
        for(auto x : graph[i]){
            ++indeg[x];
        }
    }

    queue<int> q;
    for(int i = 1; i <= n; i++){
        if(!indeg[i]){
            q.push(i);
        }
    }

    while(!q.empty()){
        int frt = q.front();
        order.push_back(frt);
        q.pop();
        for(auto x : graph[frt]){
            --indeg[x];
            if(!indeg[x]){
                q.push(x);
            }
        }
    }

}
void testCase(){
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        cin >> pro[i];
    }

    for(int i = 0; i < m; i++){
        int x, y;
        cin >> x >> y;
        graph[y + 1].push_back(x + 1);
    }

    topoSort();

    int ans = 0;
    for(int i = 0; i < order.size(); i++){
        int j = i;
        if(pro[order[j]]){
            while(pro[order[j]] && j < order.size()){
                j++;
            }
            ans += 1;
            i = j - 1;
        }
    }

    cout << ans << endl;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);

    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif

    int t = 1;
    // int t; cin >> t;
    while(t--)
        testCase();

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long

#define check(c)       cout<<#c<<" : "<<c<<endl;
#define check2(a, b)    cout<<#a<<" : "<<a<<" | "<<#b<<" : "<<b<<endl;
#define check3(a, b, c)  cout<<#a<<" : "<<a<<" | "<<#b<<" : "<<b<<" | "<<#c<<" : "<<c<<endl;

void testCase(){
    int n, m;
    cin >> n >> m;

    vector<int> pro(n);
    vector<vector<int>> graph(n);
    for(int i = 0; i < n; i++){
        cin >> pro[i];
    }

    vector<int> indeg(n);
    for(int i = 0; i < m; i++){
        int x, y;
        cin >> x >> y;
        graph[y].push_back(x);
        indeg[x]++;
    }

    queue<int> Q1, Q2;
    for(int i = 0; i < n; i++){
        if(!indeg[i]){
            if(pro[i]){
                Q1.push(i);
            } else{
                Q2.push(i);
            }
        }
    }

    int ans = 0;
    while(!Q1.empty() || !Q2.empty()){
        int Add = 0;
        while(!Q1.empty()){
            Add = 1;
            int frt = Q1.front();
            Q1.pop();
            for(int x : graph[frt]){
                --indeg[x];
                if(!indeg[x]){
                    if(pro[x]){
                        Q1.push(x);
                    } else{
                        Q2.push(x);
                    }
                }
            }
        }

        while(!Q2.empty()){
            int frt = Q2.front();
            Q2.pop();
            for(int x : graph[frt]){
                --indeg[x];
                if(!indeg[x]){
                    if(pro[x]){
                        Q1.push(x);
                    } else{
                        Q2.push(x);
                    }
                }
            }
        }
        ans += Add;
    }

    cout << ans << endl;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);

    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif

    int t = 1;
    // int t; cin >> t;
    while(t--)
        testCase();

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long

#define check(c)       cout<<#c<<" : "<<c<<endl;
#define check2(a, b)    cout<<#a<<" : "<<a<<" | "<<#b<<" : "<<b<<endl;
#define check3(a, b, c)  cout<<#a<<" : "<<a<<" | "<<#b<<" : "<<b<<" | "<<#c<<" : "<<c<<endl;

void testCase(){
    int n, m;
    cin >> n >> m;

    vector<int> pro(n);
    vector<vector<int>> graph(n);
    for(int i = 0; i < n; i++){
        cin >> pro[i];
    }

    vector<int> indeg(n);
    for(int i = 0; i < m; i++){
        int x, y;
        cin >> x >> y;
        graph[y].push_back(x);
        indeg[x]++;
    }

    queue<int> Q1, Q2;
    for(int i = 0; i < n; i++){
        if(!indeg[i]){
            if(pro[i]){
                Q1.push(i);
            } else{
                Q2.push(i);
            }
        }
    }

    int ans = 0;
    while(!Q1.empty() || !Q2.empty()){
        int Add = 0;
        while(!Q2.empty()){
            int frt = Q2.front();
            Q2.pop();
            for(int x : graph[frt]){
                --indeg[x];
                if(!indeg[x]){
                    if(pro[x]){
                        Q1.push(x);
                    } else{
                        Q2.push(x);
                    }
                }
            }
        }

        while(!Q1.empty()){
            Add = 1;
            int frt = Q1.front();
            Q1.pop();
            for(int x : graph[frt]){
                --indeg[x];
                if(!indeg[x]){
                    if(pro[x]){
                        Q1.push(x);
                    } else{
                        Q2.push(x);
                    }
                }
            }
        }
        ans += Add;
    }

    cout << ans << endl;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);

    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif

    int t = 1;
    // int t; cin >> t;
    while(t--)
        testCase();

    return 0;
}


#include<iostream>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long ll;
#define ip(arr, n) for(int i=0; i<n; i++) cin>>arr[i];
#define op(arr, n) for(int i=0; i<n; i++) cout<<arr[i]<<" ";
#define fstIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define debug(x) cout<<x<<"\n";
#define inf 1e9
#define INF 1e16
#define MOD 1000000007
#define MAX 100005

ll n,m;
vector<ll>adj[MAX], inDegree(MAX, 0);
bool arr[MAX]={false};

void TopologicalSort(){
    queue<ll>q;

    for(ll i=0; i<n; i++){
        if(inDegree[i]==0){
            q.push(i);
        }
    }

    vector<ll>ans;
    while(!q.empty()){
        ll top = q.front();
        q.pop();

        ans.push_back(top);
        for(ll child: adj[top]){
            inDegree[child]--;
            if(inDegree[child]==0){
                q.push(child);
            }
        }
    }

    ll min_processor_req=0;
    for(ll i=0; i<n; ){
        while(i<n && arr[ans[i]]){
            i++;
        }
        min_processor_req++;
        i++;
    }
    cout<<min_processor_req<<endl;
}

int main(){

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    cin>>n>>m;
    ip(arr, n);

    for(ll i=0; i<m; i++){
        ll u,v;
        cin>>u>>v;
        adj[v].push_back(u);
        inDegree[u]++;
    }
    TopologicalSort();
    return 0;
}


#include<iostream>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long ll;
#define ip(arr, n) for(int i=0; i<n; i++) cin>>arr[i];
#define op(arr, n) for(int i=0; i<n; i++) cout<<arr[i]<<" ";
#define fstIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define debug(x) cout<<x<<"\n";
#define inf 1e9
#define INF 1e16
#define MOD 1000000007
#define MAX 100005

/*
Logic: Used Topological Sort

Here we approach greedy means first we finish all independent task that can be run on main processor,
then we interchange our queue means switch to coprocessor and then we run all independent task on this.

This process will continue untill we have considered all task.
*/

ll n,m;
vector<ll>adj[MAX], inDegree(MAX, 0);
bool arr[MAX]={false};

void TopologicalSort(){
    queue<ll>q[2];

    for(ll i=0; i<n; i++){
        if(inDegree[i]==0){
            q[arr[i]].push(i);
        }
    }

    ll min_processor_req=0;

    while(!q[0].empty() || !q[1].empty()){
        while(!q[0].empty()){
            ll front = q[0].front();
            q[0].pop();

            for(ll child: adj[front]){
                inDegree[child]--;
                if(inDegree[child]==0){
                    q[arr[child]].push(child);
                }
            }
        }

        min_processor_req += (!q[1].empty()); // if we swtich to coprocssor then we check if we need any coprocssor
        // or not if yes then increment min_procssor_req

        while(!q[1].empty()){
            ll front = q[1].front();
            q[1].pop();

            for(ll child: adj[front]){
                inDegree[child]--;
                if(inDegree[child]==0){
                    q[arr[child]].push(child);
                }
            }
        }
    }

    cout<<min_processor_req<<endl;
}

int main(){

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    cin>>n>>m;
    ip(arr, n);

    for(ll i=0; i<m; i++){
        ll u,v;
        cin>>u>>v;
        adj[v].push_back(u);
        inDegree[u]++;
    }

    TopologicalSort();
    return 0;
}


#include<iostream>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long ll;
#define ip(arr, n) for(int i=0; i<n; i++) cin>>arr[i];
#define op(arr, n) for(int i=0; i<n; i++) cout<<arr[i]<<" ";
#define fstIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define debug(x) cout<<x<<"\n";
#define inf 1e9
#define INF 1e16
#define MOD 1000000007
#define MAX 100005

ll n,m;
vector<ll>adj[MAX], inDegree(MAX, 0);
bool arr[MAX]={false};

void TopologicalSort(){
    set<ll>q;

    for(ll i=0; i<n; i++){
        if(inDegree[i]==0){
            q.insert(i);
        }
    }

    vector<ll>ans;
    while(!q.empty()){
        ll top = *q.begin();
        q.erase(q.begin());

        ans.push_back(top);
        for(ll child: adj[top]){
            inDegree[child]--;
            if(inDegree[child]==0){
                q.insert(child);
            }
        }
    }

    ll min_processor_req=0;
    for(ll i=0; i<n; ){
        while(i<n && arr[ans[i]]){
            i++;
        }
        min_processor_req++;
        i++;
    }
    cout<<min_processor_req<<endl;
}

int main(){

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    cin>>n>>m;
    ip(arr, n);

    for(ll i=0; i<m; i++){
        ll u,v;
        cin>>u>>v;
        adj[v].push_back(u);
        inDegree[u]++;
    }
    TopologicalSort();
    return 0;
}


#include<iostream>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long ll;
#define ip(arr, n) for(int i=0; i<n; i++) cin>>arr[i];
#define op(arr, n) for(int i=0; i<n; i++) cout<<arr[i]<<" ";
#define fstIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define debug(x) cout<<x<<"\n";
#define inf 1e9
#define INF 1e16
#define MOD 1000000007
#define MAX 100005

ll n,m;
vector<ll>adj[MAX], inDegree(MAX, 0);
bool arr[MAX]={false};

void TopologicalSort(){
    queue<ll>q;

    for(ll i=0; i<n; i++){
        if(inDegree[i]==0){
            q.push(i);
        }
    }

    vector<ll>ans;
    while(!q.empty()){
        ll top = q.front();
        q.pop();

        ans.push_back(top);
        for(ll child: adj[top]){
            inDegree[child]--;
            if(inDegree[child]==0){
                q.push(child);
            }
        }
    }

    ll min_processor_req=0;
    for(ll i=0; i<n; ){
        while(i<n && arr[ans[i]]){
            i++;
        }
        min_processor_req++;
        i++;
    }
    cout<<min_processor_req<<endl;
}

int main(){

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    cin>>n>>m;
    ip(arr, n);

    for(ll i=0; i<m; i++){
        ll u,v;
        cin>>u>>v;
        adj[v].push_back(u);
        inDegree[u]++;
    }

    for(int i=0; i<n; i++){
        sort(adj[i].begin(), adj[i].end(), [&](ll a, ll b){
            return arr[a];
        });
    }
    TopologicalSort();
    return 0;
}


#include <sstream>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<ctime>
#include<cmath>
#include<map>
#include<stack>
#include<random>
#include<queue>
#include<list>
#include<math.h>
#include<bitset>
using namespace std;
#define ll long long int
#define ld long double
#define key pair<ld,ld>
#define ii pair<int,int>
#define si set<int>
#define vii vector<pair<int,int> >
#define vi vector<int>
#define vll vector<ll>
#define vb vector<bool>
#define vvi vector<vector<int> >
#define vs vector<string>
#define all(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define nu 100001
#define mod 1000000007
#define mul(x,y) ((ll)(x)*(y))%mod
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)
#define fastio  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define forn(i, n) for (int i = 0; i < int(n); i++)
const ll INF=9223372036854775807;
//const ll mod=998244353;
int  MOD(int  a, int  b)
{
    if(a>b)
        return a-b;
    else
        return b-a;
}
int  max3(int  a,int  b, int  c)
{
    return max(c,max(a,b));
}
int  min3(int  a,int  b,int  c)
{
    return min(a,min(b,c));
}
ll  power(ll  x,ll  y,ll mod1)
{
    ll res = 1;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x)%mod1;
        y = y>>1;
        x = (x*x)%mod1;
    }
    return res;
}
ll logg(ll  a)
{
    ll  x=0;
    while(a>1)
    {
        x++;
        a/=2;
    }
    return x;
}
ll  gcd(ll  a,ll   b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
ll abso(ll x)
{
    if(x<0)
    {
        return -x;
    }
    return x;
}
ll ceiling (ll  a,ll  b)
{
    if(a%b==0)
    {
        return a/b;
    }
    else
    {
        return a/b+1;
    }
}
ll modinv(ll x)
{
    return power(x,mod-2,mod);
}
int n,m;
vi deg;
vvi adj;
vi p;
vi dp;
vi visited;
void dfs(int i)
{
    visited[i]=1;
    dp[i]=p[i];
    for(int j=0;j<adj[i].size();j++)
    {
        if(!visited[adj[i][j]]){
            dfs(adj[i][j]);}
            if(p[i]==p[adj[i][j]])
            {
                dp[i]=max(dp[i],dp[adj[i][j]]);
            }
            else
            {
                if(p[i]==1)
                {
                    dp[i]=max(dp[i],1+dp[adj[i][j]]);
                }
                else
                {
                    dp[i]=max(dp[i],dp[adj[i][j]]);
                }
            }

    }
}
int main()
{
    fastio;
    cin>>n>>m;
    adj.resize(n+1);
    deg.resize(n+1);
    p.resize(n+1);
    dp.resize(n+1,0);
    for(int i=0;i<n;i++)
    {
        cin>>p[i];
    }
    for(int i=0;i<m;i++)
    {
        int x,y;
        cin>>x>>y;
        adj[y].push_back(x);
        deg[x]++;
    }
    visited.resize(n+1);
    for(int i=0;i<n;i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }
    sort(all(dp));
    cout<<*dp.rbegin();
}


#include <sstream>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<ctime>
#include<cmath>
#include<map>
#include<stack>
#include<random>
#include<queue>
#include<list>
#include<math.h>
#include<bitset>
using namespace std;
#define ll long long int
#define ld long double
#define key pair<ld,ld>
#define ii pair<int,int>
#define si set<int>
#define vii vector<pair<int,int> >
#define vi vector<int>
#define vll vector<ll>
#define vb vector<bool>
#define vvi vector<vector<int> >
#define vs vector<string>
#define all(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define nu 100001
#define mod 1000000007
#define mul(x,y) ((ll)(x)*(y))%mod
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)
#define fastio  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define forn(i, n) for (int i = 0; i < int(n); i++)
const ll INF=9223372036854775807;
//const ll mod=998244353;
int  MOD(int  a, int  b)
{
    if(a>b)
        return a-b;
    else
        return b-a;
}
int  max3(int  a,int  b, int  c)
{
    return max(c,max(a,b));
}
int  min3(int  a,int  b,int  c)
{
    return min(a,min(b,c));
}
ll  power(ll  x,ll  y,ll mod1)
{
    ll res = 1;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x)%mod1;
        y = y>>1;
        x = (x*x)%mod1;
    }
    return res;
}
ll logg(ll  a)
{
    ll  x=0;
    while(a>1)
    {
        x++;
        a/=2;
    }
    return x;
}
ll  gcd(ll  a,ll   b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
ll abso(ll x)
{
    if(x<0)
    {
        return -x;
    }
    return x;
}
ll ceiling (ll  a,ll  b)
{
    if(a%b==0)
    {
        return a/b;
    }
    else
    {
        return a/b+1;
    }
}
ll modinv(ll x)
{
    return power(x,mod-2,mod);
}
int n,m;
vi deg;
vvi adj;
vi p;
vi dp;
vi visited;
void dfs(int i)
{
    visited[i]=1;
    dp[i]=p[i];
    for(int j=0;j<adj[i].size();j++)
    {
        if(!visited[adj[i][j]])
        {
            dfs(adj[i][j]);
            if(p[i]==p[adj[i][j]])
            {
                dp[i]=max(dp[i],dp[adj[i][j]]);
            }
            else
            {
                if(p[i]==1)
                {
                    dp[i]=max(dp[i],1+dp[adj[i][j]]);
                }
                else
                {
                    dp[i]=max(dp[i],dp[adj[i][j]]);
                }
            }
        }
    }
}
int main()
{
    fastio;
    cin>>n>>m;
    adj.resize(n+1);
    deg.resize(n+1);
    p.resize(n+1);
    dp.resize(n+1,0);
    for(int i=0;i<n;i++)
    {
        cin>>p[i];
    }
    for(int i=0;i<m;i++)
    {
        int x,y;
        cin>>x>>y;
        adj[x].push_back(y);
        deg[y]++;
    }
    visited.resize(n+1);
    for(int i=0;i<n;i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }
    sort(all(dp));
    cout<<*dp.rbegin();
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define pb push_back
#define ll long long int
# define pll pair<ll,ll>
#define pii pair<int,int>
# define fio ios_base::sync_with_stdio(false); cin.tie(NULL)
int n,m;
int pro[100009];
bool vis[100009]={false};
vector<int>adj[100009];
vector<int>ord;
int dp[100009];
void dfs(int i)
{
    for(int x :adj[i]) if(!vis[x]) dfs(x);
    ord.pb(i);
}
int main()
{
    cin>>n>>m;
    for(int i=0;i<n;i++) cin>>pro[i];
    for(int i=0;i<m;i++)
    {
        int x,y;
        cin>>x>>y;
        adj[x].pb(y);

    }
    for(int i=0;i<n;i++) if(!vis[i]) dfs(i);
    for(int i=0;i<n;i++) dp[i]=-1;
    for(int i : ord)
    {
        if(adj[i].size()==0) dp[i]=pro[i];
        else{
            if(pro[i]==0)
            {
                for(int x : adj[i]) dp[i]=max(dp[i],dp[x]);

            }
            else{
                for(int x : adj[i])
                {
                    if(pro[x]==0) dp[i]=max(dp[i],dp[x]+1);
                    else dp[i]=max(dp[i],dp[x]);
                }
            }
        }
    }
    int ans=0;
    for(int i=0;i<n;i++) ans=max(ans,dp[i]);
    cout<<ans;

}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define pb push_back
#define ll long long int
# define pll pair<ll,ll>
#define pii pair<int,int>
# define fio ios_base::sync_with_stdio(false); cin.tie(NULL)
int n,m;
int pro[100009];
bool vis[100009]={false};
vector<int>adj[100009];
vector<int>ord;
int dp[100009];
void dfs(int i)
{   vis[i]=true;
    for(int x :adj[i]) if(!vis[x]) dfs(x);
    ord.pb(i);
}
int main()
{
    cin>>n>>m;
    for(int i=0;i<n;i++) cin>>pro[i];
    for(int i=0;i<m;i++)
    {
        int x,y;
        cin>>x>>y;
        adj[x].pb(y);

    }
    for(int i=0;i<n;i++) if(!vis[i]) dfs(i);
    for(int i=0;i<n;i++) dp[i]=-1;
    for(int i : ord)
    {
        if(adj[i].size()==0) dp[i]=pro[i];
        else{
            if(pro[i]==0)
            {
                for(int x : adj[i]) dp[i]=max(dp[i],dp[x]);

            }
            else{
                for(int x : adj[i])
                {
                    if(pro[x]==0) dp[i]=max(dp[i],dp[x]+1);
                    else dp[i]=max(dp[i],dp[x]);
                }
            }
        }
    }
    int ans=0;
    for(int i=0;i<n;i++) ans=max(ans,dp[i]);
    cout<<ans;

}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

vector<set<int> >adj;
int degree[100004];
int col[100005];
set<pair<int,int> >s1,s2;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    if(fopen("INPUT.txt","r"))
    {
    freopen ("INPUT.txt" , "r" , stdin);
    freopen ("OUTPUT.txt" , "w" , stdout);
    }
#endif

// -------------------------------------Code starts here---------------------------------------------------------------------

  int n,m;
  cin>>n>>m;
  adj.resize(n+1);
  for(int i=1;i<=n;i++)
  {
    cin>>col[i];
    col[i]=!col[i];
  }

  int x,y;
  for(int i=0;i<m;i++)
  {
    cin>>x>>y;
    adj[y].insert(x);
    degree[x]++;
  }
  for(int i=1;i<=n;i++)
  {
    if(col[i])
      s2.insert({degree[i],i});
    else
      s1.insert({degree[i],i});
  }
  int cnt=0;
  while (!((s1.empty())&&(s2.empty())))
  {
    while (!s2.empty())
    {
      pair<int,int>pp=*s2.begin();
      s2.erase(s2.begin());
      if(pp.first!=0)
        break;
      for(auto i:adj[pp.second])
      {
        adj[i].erase(pp.second);
        if(col[i])
          s2.erase({degree[i],i});
        else
          s1.erase({degree[i],i});
        degree[i]--;
        if(col[i])
          s2.insert({degree[i],i});
        else
          s1.insert({degree[i],i});
      }
    }
    bool flag=false;
    while (!s1.empty())
    {
      pair<int,int>pp=*s1.begin();
      s1.erase(s1.begin());
      if(pp.first!=0)
        break;
      flag=true;
      for(auto i:adj[pp.second])
      {
        adj[i].erase(pp.second);
        if(col[i])
          s2.erase({degree[i],i});
        else
          s1.erase({degree[i],i});
        degree[i]--;
        if(col[i])
          s2.insert({degree[i],i});
        else
          s1.insert({degree[i],i});
      }
    }
    cnt+=flag;
  }
  cout<<cnt;

// -------------------------------------Code ends here------------------------------------------------------------------
    clock_t clk;
  clk = clock();

    clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << "\n";
  return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

vector<set<int> >adj;
int degree[100004];
int col[100005];
set<pair<int,int> >s1,s2;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    if(fopen("INPUT.txt","r"))
    {
    freopen ("INPUT.txt" , "r" , stdin);
    freopen ("OUTPUT.txt" , "w" , stdout);
    }
#endif

// -------------------------------------Code starts here---------------------------------------------------------------------

  int n,m;
  cin>>n>>m;
  adj.resize(n+1);
  for(int i=1;i<=n;i++)
  {
    cin>>col[i];
    col[i]=!col[i];
  }

  int x,y;
  for(int i=0;i<m;i++)
  {
    cin>>x>>y;
    x++;
    y++;
    adj[y].insert(x);
    degree[x]++;
  }
  for(int i=1;i<=n;i++)
  {
    if(col[i])
      s2.insert({degree[i],i});
    else
      s1.insert({degree[i],i});
  }
  int cnt=0;
  while (!((s1.empty())&&(s2.empty())))
  {
    while (!s2.empty())
    {
      pair<int,int>pp=*s2.begin();
      s2.erase(s2.begin());
      if(pp.first!=0)
        break;
      for(auto i:adj[pp.second])
      {
        adj[i].erase(pp.second);
        if(col[i])
          s2.erase({degree[i],i});
        else
          s1.erase({degree[i],i});
        degree[i]--;
        if(col[i])
          s2.insert({degree[i],i});
        else
          s1.insert({degree[i],i});
      }
    }
    bool flag=false;
    while (!s1.empty())
    {
      pair<int,int>pp=*s1.begin();
      s1.erase(s1.begin());
      if(pp.first!=0)
        break;
      flag=true;
      for(auto i:adj[pp.second])
      {
        adj[i].erase(pp.second);
        if(col[i])
          s2.erase({degree[i],i});
        else
          s1.erase({degree[i],i});
        degree[i]--;
        if(col[i])
          s2.insert({degree[i],i});
        else
          s1.insert({degree[i],i});
      }
    }
    cnt+=flag;
  }
  cout<<cnt;

// -------------------------------------Code ends here------------------------------------------------------------------
    clock_t clk;
  clk = clock();

    clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << "\n";
  return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

vector<set<int> >adj;
int degree[100004];
int col[100005];
set<pair<int,int> >s1,s2;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    if(fopen("INPUT.txt","r"))
    {
    freopen ("INPUT.txt" , "r" , stdin);
    freopen ("OUTPUT.txt" , "w" , stdout);
    }
#endif

// -------------------------------------Code starts here---------------------------------------------------------------------

  int n,m;
  cin>>n>>m;
  adj.resize(n+1);
  for(int i=1;i<=n;i++)
    cin>>col[i];

  int x,y;
  for(int i=0;i<m;i++)
  {
    cin>>x>>y;
    adj[y].insert(x);
    degree[x]++;
  }
  for(int i=1;i<=n;i++)
  {
    if(col[i])
      s2.insert({degree[i],i});
    else
      s1.insert({degree[i],i});
  }
  int cnt=0;
  while (!((s1.empty())&&(s2.empty())))
  {
    while (!s2.empty())
    {
      pair<int,int>pp=*s2.begin();
      s2.erase(s2.begin());
      if(pp.first!=0)
        break;
      for(auto i:adj[pp.second])
      {
        adj[i].erase(pp.second);
        if(col[i])
          s2.erase({degree[i],i});
        else
          s1.erase({degree[i],i});
        degree[i]--;
        if(col[i])
          s2.insert({degree[i],i});
        else
          s1.insert({degree[i],i});
      }
    }
    bool flag=false;
    while (!s1.empty())
    {
      pair<int,int>pp=*s1.begin();
      s1.erase(s1.begin());
      if(pp.first!=0)
        break;
      flag=true;
      for(auto i:adj[pp.second])
      {
        adj[i].erase(pp.second);
        if(col[i])
          s2.erase({degree[i],i});
        else
          s1.erase({degree[i],i});
        degree[i]--;
        if(col[i])
          s2.insert({degree[i],i});
        else
          s1.insert({degree[i],i});
      }
    }
    cnt+=flag;
  }
  cout<<cnt;

// -------------------------------------Code ends here------------------------------------------------------------------
    clock_t clk;
  clk = clock();

    clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << "\n";
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5,M=1e5+5;
int n,m,e[N];
int E=0,nxt[M],pnt[M],head[N],in[N];
void add(int u,int v){
	E++;
	in[v]++;
	pnt[E]=v;
	nxt[E]=head[u];
	head[u]=E;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)scanf("%d",&e[i]);
	memset(head,-1,sizeof(head));
	for(int u,v,i=0;i<m;i++){
		scanf("%d%d",&u,&v);
		add(v,u);
	}
	queue<int> Q0,Q1;  //0 -> 主   1->副
	for(int i=0;i<n;i++){
		if(in[i]==0){
			if(e[i]==0) Q0.push(i);
			if(e[i]==1) Q1.push(i);
		}
	}
	int cnt=0;
	while(!Q0.empty() || !Q1.empty()){
		while(!Q0.empty()){
			int x=Q0.front();
			Q0.pop();
			for(int i=head[x];i!=-1;i=nxt[i]){
				in[pnt[i]]--;
				if(!in[pnt[i]]){
					if(e[pnt[i]]==0) Q0.push(pnt[i]);
					else Q1.push(pnt[i]);
				}
			}
		}
		if(!Q1.empty()) cnt++;
		while(!Q1.empty()){
			int x=Q1.front();
			Q1.pop();
			for(int i=head[x];i!=-1;i=nxt[i]){
				in[pnt[i]]--;
				if(!in[pnt[i]]){
					if(e[pnt[i]]==0) Q0.push(pnt[i]);
					else Q1.push(pnt[i]);
				}
			}
		}
	}
	cout<<cnt;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<bool> vb;

const int MAXN = 1e5+1;

vi g[MAXN];
bool v[MAXN];
pii pr[MAXN];

int main(){
    ios::sync_with_stdio(false); cin.tie(0);

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

    memset(v, 0, sizeof(v));
    for(int i=0; i<n; i++)
        cin >> pr[i].first;

    for(int i=0; i<m; i++){
        int t, f; cin >> f >> t;
        g[t].push_back(f);
        pr[f].second++;
    }

    queue<int> q[2];
    for(int i=1; i<=n; i++){
        if(!pr[i].second)
            q[pr[i].first].push(i);
    }

    int ans=0;
    while(!q[0].empty() || !q[1].empty()){
        while(!q[0].empty()){
            int f = q[0].front();
            v[f] = 1;
            q[0].pop();
            for(int i:g[f]){
                if(v[i]) continue;
                pr[i].second--;
                if(!pr[i].second)
                    q[pr[i].first].push(i);
            }
        }
        if(!q[1].empty())
            ans++;
        while(!q[1].empty()){
            int f = q[1].front();
            v[f] = 1;
            q[1].pop();
            for(int i:g[f]){
                if(v[i]) continue;
                pr[i].second--;
                if(!pr[i].second)
                    q[pr[i].first].push(i);
            }
        }
    }

    cout << ans << '\n';

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<bool> vb;

const int MAXN = 1e5+1;

vi g[MAXN];
bool v[MAXN];
pii pr[MAXN];

int main(){
    ios::sync_with_stdio(false); cin.tie(0);

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

    memset(v, 0, sizeof(v));
    for(int i=0; i<n; i++)
        cin >> pr[i].first;

    for(int i=0; i<m; i++){
        int t, f; cin >> f >> t;
        g[t].push_back(f);
        pr[f].second++;
    }

    queue<int> q[2];
    for(int i=0; i<n; i++){
        if(!pr[i].second)
            q[pr[i].first].push(i);
    }

    int ans=0;
    while(!q[0].empty() || !q[1].empty()){
        while(!q[0].empty()){
            int f = q[0].front();
            v[f] = 1;
            q[0].pop();
            for(int i:g[f]){
                if(v[i]) continue;
                pr[i].second--;
                if(!pr[i].second)
                    q[pr[i].first].push(i);
            }
        }
        if(!q[1].empty())
            ans++;
        while(!q[1].empty()){
            int f = q[1].front();
            v[f] = 1;
            q[1].pop();
            for(int i:g[f]){
                if(v[i]) continue;
                pr[i].second--;
                if(!pr[i].second)
                    q[pr[i].first].push(i);
            }
        }
    }

    cout << ans << '\n';

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<vector<int>>in;
vector<int>e,vis,dp;
void dfs(int v){
    vis[v]=1;
    dp[v]=e[v];
    for(auto x:in[v]){
        if(vis[x])continue;
        dfs(x);
    }
   // cout<<dp[v]<<" dp[v] "<<v
    for(auto x:in[v]){
        int s=0;
        if(e[v] && !e[x])
        s=1;
        dp[v]=max(dp[v],dp[x]+s);
    }
}

main(){
    int n,m;
    cin>>n>>m;
    in=vector<vector<int>>(n);
    e=vis=dp=vector<int>(n,0);
    vector<int>ind(n,0);
    for(int i=0;i<n;i++)
    cin>>e[i];
    for(int i=0;i<m;i++){
        int x,y;
        cin>>x>>y;
        ind[y]++;
        in[x].push_back(y);
    }
    int root=0;
    for(int i=0;i<n;i++)
    if(ind[i]==0)root=i;
    dfs(root);
    int ans=0;
    for(int i=0;i<n;i++)
    ans=max(ans,dp[i]);
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
vector<vector<int>>in;
vector<int>e,vis,dp;
int dfs(int v){
    if(vis[v])
    return dp[v];
    vis[v]=1;
    dp[v]=e[v];
    for(auto x:in[v]){
        if(vis[x])continue;
        dfs(x);
    }
    for(auto x:in[v]){
        int s=0;
        if(e[v] && !e[x])
        s=1;
        dp[v]=max(dp[v],dp[x]+s);
    }
    return dp[v];
}

main(){
    int n,m;
    cin>>n>>m;
    in=vector<vector<int>>(n);
    e=vis=dp=vector<int>(n,0);
    for(int i=0;i<n;i++)
    cin>>e[i];
    for(int i=0;i<m;i++){
        int x,y;
        cin>>x>>y;
        in[x].push_back(y);
    }
    int ans=0;
    for(int i=0;i<n;i++)
    ans=max(ans,dfs(i));
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
vector<vector<int>>in;
vector<int>e,vis,dp;
int dfs(int v){
    if(dp[v]!=-1)
    return dp[v];
    vis[v]=1;
    dp[v]=e[v];
    for(auto x:in[v]){
        if(vis[x])continue;
        dfs(x);
    }
    for(auto x:in[v]){
        int s=0;
        if(e[v] && !e[x])
        s=1;
        dp[v]=max(dp[v],dp[x]+s);
    }
    return dp[v];
}

main(){
    int n,m;
    cin>>n>>m;
    in=vector<vector<int>>(n);
    e=vis=dp=vector<int>(n,-1);
    for(int i=0;i<n;i++)
    cin>>e[i];
    for(int i=0;i<m;i++){
        int x,y;
        cin>>x>>y;
        in[x].push_back(y);
    }
    int ans=0;
    for(int i=0;i<n;i++)
    ans=max(ans,dfs(i));
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
vector<vector<int>>in;
vector<int>e,vis,dp;
void dfs(int v){
    vis[v]=1;
    dp[v]=e[v];
    for(auto x:in[v]){
        if(vis[x])continue;
        dfs(x);
    }
   // cout<<dp[v]<<" dp[v] "<<v
    for(auto x:in[v]){
        int s=0;
        if(e[v] && !e[x])
        s=1;
        dp[v]=max(dp[v],dp[x]+s);
    }
}

main(){
    int n,m;
    cin>>n>>m;
    in=vector<vector<int>>(n);
    e=vis=dp=vector<int>(n,0);
    vector<int>ind(n,0);
    for(int i=0;i<n;i++)
    cin>>e[i];
    for(int i=0;i<m;i++){
        int x,y;
        cin>>x>>y;
        ind[y]++;
        in[x].push_back(y);
    }
    int root=0;
    for(int i=0;i<n;i++)
    if(ind[i]==0)root=0;
    dfs(root);
    int ans=0;
    for(int i=0;i<n;i++)
    ans=max(ans,dp[i]);
    cout<<ans<<endl;
}


#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(_i,_a,_n) for(int _i=_a;_i<=_n;++_i)
#define PER(_i,_a,_n) for(int _i=_n;_i>=_a;--_i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(_a) ({REP(_i,1,n) cout<<_a[_i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head

#ifdef __APPLE__
const int N = 1e2+50;
#else
const int N = 1e6+50;
#endif

int n,m,a[N],deg[N];
vector<int> g[N];
queue<int> q[2];

int main() {
	scanf("%d%d",&n,&m);
	REP(i,0,n-1) scanf("%d",a+i);
	REP(i,1,m) {
		int u,v;
		scanf("%d%d",&u,&v);
		g[u].pb(v);
		++deg[v];
	}
	REP(i,0,n-1) if (!deg[i]) q[a[i]].push(i);
	int ans = 0;
	while (q[0].size()+q[1].size()) {
		while (q[0].size()) {
			int u = q[0].front(); q[0].pop();
			for (int v:g[u]) if (!--deg[v]) q[a[v]].push(v);
		}
		ans += !q[1].empty();
		while (q[1].size()) {
			int u = q[1].front(); q[1].pop();
			for (int v:g[u]) if (!--deg[v]) q[a[v]].push(v);
		}
	}
	printf("%d\n", ans);
}


#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(_i,_a,_n) for(int _i=_a;_i<=_n;++_i)
#define PER(_i,_a,_n) for(int _i=_n;_i>=_a;--_i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(_a) ({REP(_i,1,n) cout<<_a[_i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head

#ifdef __APPLE__
const int N = 1e2+50;
#else
const int N = 1e6+50;
#endif

int n,m,a[N],deg[N];
vector<int> g[N];
queue<int> q[2];

int main() {
	scanf("%d%d",&n,&m);
	REP(i,0,n-1) scanf("%d",a+i);
	REP(i,1,m) {
		int u,v;
		scanf("%d%d",&u,&v);
		g[u].pb(v);
		++deg[v];
	}
	REP(i,0,n-1) if (!deg[i]) q[a[i]].push(i);
	int ans = 0, tot = 0;
	while (1) {
		while (q[1].size()) {
			int u = q[1].front();; q[1].pop();
			for (int v:g[u]) if (!--deg[v]) q[a[v]].push(v);
		}
		ans += !q[0].empty();
		while (q[0].size()) {
			int u = q[0].front(); q[0].pop();
			for (int v:g[u]) if (!--deg[v]) q[a[v]].push(v);
		}
	}
	printf("%d\n", ans);
}


#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(_i,_a,_n) for(int _i=_a;_i<=_n;++_i)
#define PER(_i,_a,_n) for(int _i=_n;_i>=_a;--_i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(_a) ({REP(_i,1,n) cout<<_a[_i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head

#ifdef __APPLE__
const int N = 1e2+50;
#else
const int N = 1e6+50;
#endif

int n,m,a[N],deg[N];
vector<int> g[N];
queue<int> q[2];

int main() {
	scanf("%d%d",&n,&m);
	REP(i,0,n-1) scanf("%d",a+i);
	REP(i,1,m) {
		int u,v;
		scanf("%d%d",&u,&v);
		g[u].pb(v);
		++deg[v];
	}
	REP(i,0,n-1) if (!deg[i]) q[a[i]].push(i);
	int ans = 0, tot = 0;
	while (1) {
		while (q[1].size()) {
			++tot;
			int u = q[1].front();; q[1].pop();
			for (int v:g[u]) if (!--deg[v]) q[a[v]].push(v);
		}
		if (tot==n) break;
		++ans;
		while (q[0].size()) {
			++tot;
			int u = q[0].front(); q[0].pop();
			for (int v:g[u]) if (!--deg[v]) q[a[v]].push(v);
		}
	}
	printf("%d\n", ans);
}


#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(_i,_a,_n) for(int _i=_a;_i<=_n;++_i)
#define PER(_i,_a,_n) for(int _i=_n;_i>=_a;--_i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(_a) ({REP(_i,1,n) cout<<_a[_i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head

#ifdef __APPLE__
const int N = 1e2+50;
#else
const int N = 1e6+50;
#endif

int n,m,a[N],deg[N];
vector<int> g[N];
queue<int> q[2];

int main() {
	scanf("%d%d",&n,&m);
	REP(i,0,n-1) scanf("%d",a+i);
	REP(i,1,m) {
		int u,v;
		scanf("%d%d",&u,&v);
		g[u].pb(v);
		++deg[v];
	}
	REP(i,0,n-1) if (!deg[i]) q[a[i]].push(i);
	int ans = 0, tot = 0,t=3;
	while (t--) {
		while (q[1].size()) {
			++tot;
			int u = q[1].front();; q[1].pop();
			for (int v:g[u]) if (!--deg[v]) q[a[v]].push(v);
		}
		if (tot==n) break;
		++ans;
		while (q[0].size()) {
			++tot;
			int u = q[0].front(); q[0].pop();
			for (int v:g[u]) if (!--deg[v]) q[a[v]].push(v);
		}
	}
	printf("%d\n", ans);
}


#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(_i,_a,_n) for(int _i=_a;_i<=_n;++_i)
#define PER(_i,_a,_n) for(int _i=_n;_i>=_a;--_i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(_a) ({REP(_i,1,n) cout<<_a[_i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head

#ifdef __APPLE__
const int N = 1e2+50;
#else
const int N = 1e6+50;
#endif

int n,m,a[N],deg[N];
vector<int> g[N];
queue<int> q[2];

int main() {
	scanf("%d%d",&n,&m);
	REP(i,0,n-1) scanf("%d",a+i);
	REP(i,1,m) {
		int u,v;
		scanf("%d%d",&u,&v);
		g[u].pb(v);
		++deg[v];
	}
	REP(i,0,n-1) if (!deg[i]) q[a[i]].push(i);
	int ans = 0, tot = 0;
	while (q[0].size()+q[1].size()) {
		while (q[1].size()) {
			int u = q[1].front();; q[1].pop();
			for (int v:g[u]) if (!--deg[v]) q[a[v]].push(v);
		}
		ans += !q[0].empty();
		while (q[0].size()) {
			int u = q[0].front(); q[0].pop();
			for (int v:g[u]) if (!--deg[v]) q[a[v]].push(v);
		}
	}
	printf("%d\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
#define R(i,b,a) for(int i=(int)(b);i>=(int)(a);i--)
#define endl "\n"
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define out(x) cout<<x<<endl,exit(0);
#define pii pair<int,int>
#define pb push_back
#define all(v) v.begin(),v.end()
#define I first
#define S second

const int N=1e5+9;
int a[N],deg[N],vis[N],tol,ans;
vector <int> v[N];

void slv(queue<int> &cop,queue<int> &man,int f){
    while(!cop.empty()){
        int p=cop.front();
        cop.pop();
        vis[p]=1;
        tol++;

        for(auto it:v[p]){
            if(vis[it]) continue;
            deg[it]--;
            if(deg[it]==0){
                if((a[it] && f)||(a[it]==0 && f==0)) cop.push(it);
                else man.push(it);
            }
        }
    }
}

int32_t main(){
    ios;
    int n,m;
    cin>>n>>m;

    F(i,0,n-1) cin>>a[i];
    F(i,1,m){
        int x,y;
        cin>>x>>y;
        v[y].pb(x);
        deg[x]++;
    }

    queue <int> cop,man;
    F(i,0,n-1){
        if(deg[i]==0) {
            if(a[i]) cop.push(i);
            else man.push(i);
        }
    }

    while(tol<n){
        slv(man,cop,0);
        if(cop.size()){
            slv(cop,man,1);
            ans++;
        }
    }

    cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
#define R(i,b,a) for(int i=(int)(b);i>=(int)(a);i--)
#define endl "\n"
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define out(x) cout<<x<<endl,exit(0);
#define pii pair<int,int>
#define pb push_back
#define all(v) v.begin(),v.end()
#define I first
#define S second

const int N=1e5+9;
int a[N],deg[N],vis[N],tol;
vector <int> v[N];

int32_t main(){
    ios;
    int n,m;
    cin>>n>>m;

    F(i,0,n-1) cin>>a[i];
    F(i,1,m){
        int x,y;
        cin>>x>>y;
        v[y].pb(x);
        deg[x]++;
    }

    vector <int> p,q;
    F(i,0,n-1){
        if(deg[i]==0) {
            q.pb(i);
            tol++;
        }
    }

    int f=0,ans=0;

    while(tol<n){
        int ff=0;
        for(auto it:q) if(a[it]==1) ff=1;
        if(ff && f==0) f=1,ans++;
        else if(ff==0 && f) f=0;

        for(auto it:q){
            vis[it]=1;
            for(auto j:v[it]){
                if(vis[j]) continue;
                deg[j]--;
                if(deg[j]==0) p.pb(j),tol++;
            }
        }
        q=p;
        p.clear();
    }

    cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
#define R(i,b,a) for(int i=(int)(b);i>=(int)(a);i--)
#define endl "\n"
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define out(x) cout<<x<<endl,exit(0);
#define pii pair<int,int>
#define pb push_back
#define all(v) v.begin(),v.end()
#define I first
#define S second

const int N=1e5+9;
int a[N],deg[N],vis[N],tol,ans;
vector <int> v[N];

void slv(queue<int> &cop,queue<int> &man){
    while(!cop.empty()){
        int p=cop.front();
        cop.pop();
        vis[p]=1;
        tol++;

        for(auto it:v[p]){
            if(vis[it]) continue;
            deg[it]--;
            if(deg[it]==0){
                if(a[it]) cop.push(it);
                else man.push(it);
            }
        }
    }
}

int32_t main(){
    ios;
    int n,m;
    cin>>n>>m;

    F(i,0,n-1) cin>>a[i];
    F(i,1,m){
        int x,y;
        cin>>x>>y;
        v[y].pb(x);
        deg[x]++;
    }

    queue <int> cop,man;
    F(i,0,n-1){
        if(deg[i]==0) {
            if(a[i]) cop.push(i);
            else man.push(i);
        }
    }

    while(tol<n){
        slv(man,cop);
        if(cop.size()){
            slv(cop,man);
            ans++;
        }
    }

    cout<<ans;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
using ll = long long;
const char E = '\n';
const int N = 100005;
const ll mod = 1e9+7;

int n, m, dp[N], ans;
bool p[N], used[N];
vector <int> g[N];

void dfs (int v) {
    used[v] = 1;
    int cnt = 0;
    for (auto i:g[v]) {
        if (!used[i]) {
            dfs (i);
            cnt ++;
        }
        if (!p[i] && p[v]) dp[v] = max(dp[v], dp[i]+1);
        else dp[v] = max(dp[v], dp[i]);
    }
    if (!cnt && p[v]) dp[v] = 1;
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for (int i=0; i<n; i++) {
        cin >> p[i];
	}
	for (int i=1; i<=m; i++) {
        int x, y;
        cin >> x >> y;
        g[x].pb (y);
	}
	for (int i=0; i<n; i++) {
        if (!used[i]) {
            dfs (i);
            ans = max(ans, dp[i]);
        }
	}
	cout << ans;
    return 0;
}