#include<bits/stdc++.h>
using namespace std;
#define mp(x,y)(make_pair(x,y))
const int N=1e5+1;
struct List{
	int head[N],tail[N],next[N];;
	void combine(int x,int y){
		if (head[y]){
			if (!head[x])head[x]=head[y],tail[x]=tail[y];
			else{
			    next[tail[x]]=head[y];
			    tail[x]=tail[y];
			}
			head[y]=tail[y]=0;
		}
	}
	void add(int x,int y){
		if (!head[x])head[x]=tail[x]=y;
		else
		    next[tail[x]]=y,tail[x]=y;
	}
	void clear(int x){
	    head[x]=tail[x]=0;
	}
};
struct Tarjan{
    vector<pair<int,int>>g[N];
    int n,m,tot;
    bool instack[N];
	List in_edge,stack;
    int dfn[N],low[N];
    vector<int>edge_on_simple_circle,d;
    void read(){
        cin>>n>>m;
        for (int i=1;i<=m;i++){
            int x,y;
            cin>>x>>y;
            g[x].push_back(mp(y,i));
            g[y].push_back(mp(x,i));
        }
    }
    void dfs(int x,int lastedge){
        dfn[x]=low[x]=++tot;
        instack[x]=1;
        for (auto y:g[x]){
            if (y.second==lastedge)continue;
            if (!dfn[y.first]){
                dfs(y.first,y.second);
                if (dfn[x]<low[y.first]){
                    //bridge
                    continue;
                }else{
                    in_edge.add(low[y.first],y.second);
                }
                if (low[y.first]==dfn[x]){
                    int calc=1;
                    int sig=dfn[x],p=stack.head[sig];
                    while (p){
                        instack[p]=0;
                        ++calc;
                        p=stack.next[p];
                    }
                    stack.clear(sig);
                    d.clear();
                    instack[x]=1;
                    int e=in_edge.head[sig];
                    while (e)d.push_back(e),e=in_edge.next[e];
                    in_edge.head[sig]=in_edge.tail[sig]=0;
                    if (d.size()==calc){
                        for (auto e:d)
                            edge_on_simple_circle.push_back(e);
                    }
                }else{
                    if (low[y.first]<low[x]){
                        in_edge.combine(low[y.first],low[x]);
                        stack.combine(low[y.first],low[x]);
                    }else
                        if (low[y.first]>low[x]){
                            in_edge.combine(low[x],low[y.first]);
                            stack.combine(low[x],low[y.first]);
                        }
                    low[x]=min(low[x],low[y.first]);
                }
            }else{
                if (instack[y.first]){
                    in_edge.add(dfn[y.first],y.second);
                    if (low[x]>dfn[y.first]){
                        in_edge.combine(dfn[y.first],low[x]);
                        stack.combine(low[y.first],low[x]);
                        low[x]=dfn[y.first];
                    }else
                    if (low[x]<dfn[y.first]){
                        in_edge.combine(low[x],dfn[y.first]);
                        stack.combine(low[x],low[y.first]);
                    }
                }
            }
        }
        if (low[x]!=dfn[x])
            stack.add(low[x],x);
        else
            instack[x]=0;
    }
    void build(){
        for (int i=1;i<=n;i++)
            if (!dfn[i])
                tot=0,dfs(i,0);
    }
    void output(){
        sort(edge_on_simple_circle.begin(),edge_on_simple_circle.end());
        cout<<edge_on_simple_circle.size()<<endl;
        for (auto e:edge_on_simple_circle)
            cout<<e<<' ';
    }
}f;
int main(){
    ios::sync_with_stdio(false);
    f.read();
    f.build();
    f.output();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mp(x,y)(make_pair(x,y))
const int N=1e5+1;
struct Tarjan{
    vector<pair<int,int>>g[N],edge;
    int n,m,tot;
    bool instack[N];
    vector<int> in_edge[N],in_edge1[N];
    int dfn[N],low[N];
    vector<int>edge_on_simple_circle,d,stack[N];
    void read(){
        cin>>n>>m;
        edge.clear();
        edge.push_back(mp(0,0));
        for (int i=1;i<=m;i++){
            int x,y;
            cin>>x>>y;
            edge.push_back(mp(x,y));
            g[x].push_back(mp(y,i));
            g[y].push_back(mp(x,i));
        }
    }
    inline bool edge_out(int x){
        return !instack[edge[x].first]&&!instack[edge[x].second];
    }
    void dfs(int x,int lastedge){
        dfn[x]=low[x]=++tot;
        instack[x]=1;
        for (auto y:g[x]){
            if (y.second==lastedge)continue;
            if (!dfn[y.first]){
                dfs(y.first,y.second);
                in_edge[low[y.first]].push_back(y.second);
                if (dfn[x]<low[y.first]){
                    //bridge
                   in_edge[low[y.first]].pop_back();
                }
                int calc=1;
                int sig=dfn[x];
                while (stack[sig].size()){
                    instack[*stack[sig].rbegin()]=0;
                    stack[sig].pop_back();
                    ++calc;
                }
                d.clear();
                int tail=in_edge[sig].size()-1;
                instack[x]=0;
                while (tail>=0&&edge_out(in_edge[sig][tail]))d.push_back(in_edge[sig][tail]),in_edge[sig].pop_back(),tail--;
                instack[x]=1;
                if (d.size()==calc)
                    for (auto e:d)
                        edge_on_simple_circle.push_back(e);
                low[x]=min(low[x],low[y.first]);
            }else{
                if (instack[y.first]){
                    in_edge[dfn[y.first]].push_back(y.second);
                //    in_edge1[x].push_back(y.second);
                    if (low[x]>=dfn[y.first])
                        low[x]=dfn[y.first];
                }
            }
        }
        /*for (auto y:in_edge1[x])
            in_edge[low[x]].push_back(y);
        in_edge1[x].clear();*/
        if (low[x]!=dfn[x])
            stack[low[x]].push_back(x);
        else
            instack[x]=0;
    }
    void build(){
        for (int i=1;i<=n;i++)
            if (!dfn[i])
                tot=0,dfs(i,0);
    }
    void output(){
        sort(edge_on_simple_circle.begin(),edge_on_simple_circle.end());
        cout<<edge_on_simple_circle.size()<<endl;
        for (auto e:edge_on_simple_circle)
            cout<<e<<' ';
    }
}f;
int main(){
    ios::sync_with_stdio(false);
    f.read();
    f.build();
    f.output();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mp(x,y)(make_pair(x,y))
const int N=1e5+1;
struct List{
	int head[N],tail[N],next[N];;
	void combine(int x,int y){
		if (head[y]){
			next[tail[x]]=head[y];
			tail[x]=tail[y];
			if (!head[x])head[x]=head[y];
			head[y]=tail[y]=0;
		}
	}
	void add(int x,int y){
		next[tail[x]]=y;
		if (!head[x])head[x]=tail[x]=y;
		else
			tail[x]=y;
	}
	void clear(int x){
	    head[x]=tail[x]=0;
	}
};
struct Tarjan{
    vector<pair<int,int>>g[N];
    int n,m,tot;
    bool instack[N];
	List in_edge,stack;
    int dfn[N],low[N];
    vector<int>edge_on_simple_circle,d;
    void read(){
        cin>>n>>m;
        for (int i=1;i<=m;i++){
            int x,y;
            cin>>x>>y;
            g[x].push_back(mp(y,i));
            g[y].push_back(mp(x,i));
        }
    }
    void dfs(int x,int lastedge){
        dfn[x]=low[x]=++tot;
        instack[x]=1;
        for (auto y:g[x]){
            if (y.second==lastedge)continue;
            if (!dfn[y.first]){
                dfs(y.first,y.second);
                if (dfn[x]<low[y.first]){
                    //bridge
                    continue;
                }else{
                    in_edge.add(low[y.first],y.second);
                }
                if (low[y.first]==dfn[x]){
                    int calc=1;
                    int sig=dfn[x],p=stack.head[sig];
                    while (p){
                        instack[p]=0;
                        ++calc;
                        p=stack.next[p];
                    }
                    stack.clear(sig);
                    d.clear();
                    instack[x]=1;
                    int e=in_edge.head[sig];
                    while (e)d.push_back(e),e=in_edge.next[e];
                    in_edge.head[sig]=in_edge.tail[sig]=0;
                    if (d.size()==calc){
                        for (auto e:d)
                            edge_on_simple_circle.push_back(e);
                    }
                }else{
                    if (low[y.first]<low[x]){
                        in_edge.combine(low[y.first],low[x]);
                        stack.combine(low[y.first],low[x]);
                    }else
                        if (low[y.first]>low[x]){
                            in_edge.combine(low[x],low[y.first]);
                            stack.combine(low[x],low[y.first]);
                        }
                    low[x]=min(low[x],low[y.first]);
                }
            }else{
                if (instack[y.first]){
                    in_edge.add(dfn[y.first],y.second);
                    if (low[x]>dfn[y.first]){
                        in_edge.combine(dfn[y.first],low[x]);
                        stack.combine(low[y.first],low[x]);
                        low[x]=dfn[y.first];
                    }else
                    if (low[x]<dfn[y.first]){
                        in_edge.combine(low[x],dfn[y.first]);
                        stack.combine(low[x],low[y.first]);
                    }
                }
            }
        }
        if (low[x]!=dfn[x])
            stack.add(low[x],x);
        else
            instack[x]=0;
    }
    void build(){
        for (int i=1;i<=n;i++)
            if (!dfn[i])
                tot=0,dfs(i,0);
    }
    void output(){
        sort(edge_on_simple_circle.begin(),edge_on_simple_circle.end());
        cout<<edge_on_simple_circle.size()<<endl;
        for (auto e:edge_on_simple_circle)
            cout<<e<<' ';
    }
}f;
int main(){
    ios::sync_with_stdio(false);
    f.read();
    f.build();
    f.output();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mp(x,y)(make_pair(x,y))
const int N=2e5+1;
struct List{
	int head[N],tail[N],next[N];;
	void combine(int x,int y){
		if (head[y]){
			if (!head[x])head[x]=head[y],tail[x]=tail[y];
			else{
			    next[tail[x]]=head[y];
			    tail[x]=tail[y];
			}
			head[y]=tail[y]=0;
		}
	}
	void add(int x,int y){
		if (!head[x])head[x]=tail[x]=y;
		else
		    next[tail[x]]=y,tail[x]=y;
	}
	void clear(int x){
	    head[x]=tail[x]=0;
	}
};
struct Tarjan{
    vector<pair<int,int>>g[N];
    int n,m,tot;
    bool instack[N];
	List in_edge,stack;
    int dfn[N],low[N];
    vector<int>edge_on_simple_circle,d;
    void read(){
        cin>>n>>m;
        for (int i=1;i<=m;i++){
            int x,y;
            cin>>x>>y;
            g[x].push_back(mp(y,i));
            g[y].push_back(mp(x,i));
        }
    }
    void dfs(int x,int lastedge){
        dfn[x]=low[x]=++tot;
        instack[x]=1;
        for (auto y:g[x]){
            if (y.second==lastedge)continue;
            if (!dfn[y.first]){
                dfs(y.first,y.second);
                if (dfn[x]<low[y.first]){
                    //bridge
                    continue;
                }else{
                    in_edge.add(low[y.first],y.second);
                }
                if (low[y.first]==dfn[x]){
                    int calc=1;
                    int sig=dfn[x],p=stack.head[sig];
                    while (p){
                        instack[p]=0;
                        ++calc;
                        p=stack.next[p];
                    }
                    stack.clear(sig);
                    d.clear();
                    instack[x]=1;
                    int e=in_edge.head[sig];
                    while (e)d.push_back(e),e=in_edge.next[e];
                    in_edge.head[sig]=in_edge.tail[sig]=0;
                    if (d.size()==calc){
                        for (auto e:d)
                            edge_on_simple_circle.push_back(e);
                    }
                }else{
                    if (low[y.first]<low[x]){
                        in_edge.combine(low[y.first],low[x]);
                        stack.combine(low[y.first],low[x]);
                    }else
                        if (low[y.first]>low[x]){
                            in_edge.combine(low[x],low[y.first]);
                            stack.combine(low[x],low[y.first]);
                        }
                    low[x]=min(low[x],low[y.first]);
                }
            }else{
                if (instack[y.first]){
                    if (low[x]>=dfn[y.first])
                        in_edge.add(dfn[y.first],y.second);
                    if (low[x]>dfn[y.first]){
                        in_edge.combine(dfn[y.first],low[x]);
                        stack.combine(dfn[y.first],low[x]);
                        low[x]=dfn[y.first];
                    }else
                    if (low[x]<dfn[y.first]){
                        in_edge.combine(low[x],dfn[y.first]);
                        stack.combine(low[x],dfn[y.first]);
                    }
                }
            }
        }
        stack.clear(dfn[x]);
        in_edge.clear(dfn[x]);
        if (low[x]!=dfn[x])
            stack.add(low[x],x);
        else
            instack[x]=0;
    }
    void build(){
        for (int i=1;i<=n;i++)
            if (!dfn[i])
                tot=0,dfs(i,0);
    }
    void output(){
        sort(edge_on_simple_circle.begin(),edge_on_simple_circle.end());
        cout<<edge_on_simple_circle.size()<<endl;
        for (auto e:edge_on_simple_circle)
            cout<<e<<' ';
    }
}f;
int main(){
   // freopen("a.in","r",stdin);
    ios::sync_with_stdio(false);
    f.read();
    f.build();
    f.output();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mp(x,y)(make_pair(x,y))
const int N=1e5+1;
struct List{
	int head[N],tail[N],next[N];;
	void combine(int x,int y){
		if (head[y]){
			next[tail[x]]=head[y];
			tail[x]=tail[y];
			head[y]=tail[y]=0;
		}
	}
	void add(int x,int y){
		next[tail[x]]=y;
		if (!head[x])head[x]=tail[x]=y;
		else
			tail[x]=y;
	}
	void clear(int x){
	    head[x]=tail[x]=0;
	}
};
struct Tarjan{
    vector<pair<int,int>>g[N];
    int n,m,tot;
    bool instack[N];
	List in_edge,stack;
    int dfn[N],low[N];
    vector<int>edge_on_simple_circle,d;
    void read(){
        cin>>n>>m;
        for (int i=1;i<=m;i++){
            int x,y;
            cin>>x>>y;
            g[x].push_back(mp(y,i));
            g[y].push_back(mp(x,i));
        }
    }
    void dfs(int x,int lastedge){
        dfn[x]=low[x]=++tot;
        instack[x]=1;
        for (auto y:g[x]){
            if (y.second==lastedge)continue;
            if (!dfn[y.first]){
                dfs(y.first,y.second);
                if (dfn[x]<low[y.first]){
                    //bridge
                    continue;
                }else{
                    in_edge.add(low[y.first],y.second);
                }
                if (low[y.first]==dfn[x]){
                int calc=1;
                int sig=dfn[x],p=stack.head[sig];
                while (p){
                    instack[p]=0;
                    ++calc;
                    p=stack.next[p];
                }
                stack.clear(sig);
                d.clear();
                instack[x]=1;
                int e=in_edge.head[sig];
                while (e)d.push_back(e),e=in_edge.next[e];
                in_edge.head[sig]=in_edge.tail[sig]=0;
                if (d.size()==calc)
                    for (auto e:d)
                        edge_on_simple_circle.push_back(e);
                }else{
                    if (low[y.first]<low[x]){
                        in_edge.combine(low[y.first],low[x]);
                        stack.combine(low[y.first],low[x]);
                    }else
                        if (low[y.first]>low[x]){
                            in_edge.combine(low[x],low[y.first]);
                            stack.combine(low[x],low[y.first]);
                        }
                    low[x]=min(low[x],low[y.first]);
                }
            }else{
                if (instack[y.first]){
                    in_edge.add(dfn[y.first],y.second);
                    if (low[x]>dfn[y.first]){
                        in_edge.combine(dfn[y.first],low[x]);
                        stack.combine(low[y.first],low[x]);
                        low[x]=dfn[y.first];
                    }else
                    if (low[x]<dfn[y.first]){
                        in_edge.combine(low[x],dfn[y.first]);
                        stack.combine(low[x],low[y.first]);
                    }
                }
            }
        }
        if (low[x]!=dfn[x])
            stack.add(low[x],x);
        else
            instack[x]=0;
    }
    void build(){
        for (int i=1;i<=n;i++)
            if (!dfn[i])
                tot=0,dfs(i,0);
    }
    void output(){
        sort(edge_on_simple_circle.begin(),edge_on_simple_circle.end());
        cout<<edge_on_simple_circle.size()<<endl;
        for (auto e:edge_on_simple_circle)
            cout<<e<<' ';
    }
}f;
int main(){
    ios::sync_with_stdio(false);
    f.read();
    f.build();
    f.output();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mp(x,y)(make_pair(x,y))
const int N=1e5+1;
struct Tarjan{
    vector<pair<int,int>>g[N],edge;
    int n,m,tot;
    bool instack[N];
    vector<int> in_edge;
    int dfn[N],low[N];
    vector<int>edge_on_simple_circle,d,stack[N];
    void read(){
        cin>>n>>m;
        edge.clear();
        edge.push_back(mp(0,0));
        for (int i=1;i<=m;i++){
            int x,y;
            cin>>x>>y;
            edge.push_back(mp(x,y));
            g[x].push_back(mp(y,i));
            g[y].push_back(mp(x,i));
        }
    }
    inline bool edge_out(int x){
        return !instack[edge[x].first]&&!instack[edge[x].second];
    }
    void dfs(int x,int lastedge){
        dfn[x]=low[x]=++tot;
        instack[x]=1;
        for (auto y:g[x]){
            if (y.second==lastedge)continue;
            if (!dfn[y.first]){
                in_edge.push_back(y.second);
                dfs(y.first,y.second);
                if (dfn[x]<low[y.first]){
                    //bridge
                    in_edge.pop_back();
                }
                int calc=1;
                int sig=dfn[x];
                while (stack[sig].size()){
                    instack[*stack[sig].rbegin()]=0;
                    stack[sig].pop_back();
                    ++calc;
                }
                d.clear();
                int tail=in_edge.size()-1;
                instack[x]=0;
                while (tail>=0&&edge_out(in_edge[tail]))d.push_back(in_edge[tail]),in_edge.pop_back(),tail--;
                instack[x]=1;
                if (d.size()==calc)
                    for (auto e:d)
                        edge_on_simple_circle.push_back(e);
                low[x]=min(low[x],low[y.first]);
            }else{
                if (instack[y.first]){
                    in_edge.push_back(y.second);
                    if (low[x]>=dfn[y.first])
                        low[x]=dfn[y.first];
                }
            }
        }
        if (low[x]!=dfn[x])
            stack[low[x]].push_back(x);
        else
            instack[x]=0;
    }
    void build(){
        tot=0;
        for (int i=1;i<=n;i++)
            if (!dfn[i])
                tot=0,dfs(i,0);
    }
    void output(){
        sort(edge_on_simple_circle.begin(),edge_on_simple_circle.end());
        cout<<edge_on_simple_circle.size()<<endl;
        for (auto e:edge_on_simple_circle)
            cout<<e<<' ';
    }
}f;
int main(){
    ios::sync_with_stdio(false);
    f.read();
    f.build();
    f.output();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mp(x,y)(make_pair(x,y))
const int N=2e5+1;
struct List{
	int head[N],tail[N],next[N];;
	void combine(int x,int y){
		if (head[y]){
			if (!head[x])head[x]=head[y],tail[x]=tail[y];
			else{
			    next[tail[x]]=head[y];
			    tail[x]=tail[y];
			}
			head[y]=tail[y]=0;
		}
	}
	void add(int x,int y){
		if (!head[x])head[x]=tail[x]=y;
		else
		    next[tail[x]]=y,tail[x]=y;
	}
	void clear(int x){
	    head[x]=tail[x]=0;
	}
};
struct Tarjan{
    vector<pair<int,int>>g[N];
    int n,m,tot;
    bool instack[N];
	List in_edge,stack;
    int dfn[N],low[N];
    vector<int>edge_on_simple_circle,d;
    void read(){
        cin>>n>>m;
        for (int i=1;i<=m;i++){
            int x,y;
            cin>>x>>y;
            g[x].push_back(mp(y,i));
            g[y].push_back(mp(x,i));
        }
    }
    void dfs(int x,int lastedge){
        dfn[x]=low[x]=++tot;
        instack[x]=1;
        for (auto y:g[x]){
            if (y.second==lastedge)continue;
            if (!dfn[y.first]){
                dfs(y.first,y.second);
                if (dfn[x]<low[y.first]){
                    //bridge
                    continue;
                }else{
                    in_edge.add(low[y.first],y.second);
                }
                if (low[y.first]==dfn[x]){
                    int calc=1;
                    int sig=dfn[x],p=stack.head[sig];
                    while (p){
                        instack[p]=0;
                        ++calc;
                        p=stack.next[p];
                    }
                    stack.clear(sig);
                    d.clear();
                    instack[x]=1;
                    int e=in_edge.head[sig];
                    while (e)d.push_back(e),e=in_edge.next[e];
                    in_edge.head[sig]=in_edge.tail[sig]=0;
                    if (d.size()==calc){
                        for (auto e:d)
                            edge_on_simple_circle.push_back(e);
                    }
                }else{
                    if (low[y.first]<low[x]){
                        in_edge.combine(low[y.first],low[x]);
                        stack.combine(low[y.first],low[x]);
                    }else
                        if (low[y.first]>low[x]){
                            in_edge.combine(low[x],low[y.first]);
                            stack.combine(low[x],low[y.first]);
                        }
                    low[x]=min(low[x],low[y.first]);
                }
            }else{
                if (instack[y.first]){
                    if (low[x]>dfn[y.first]){
                        in_edge.add(dfn[y.first],y.second);
                        in_edge.combine(dfn[y.first],low[x]);
                        stack.combine(dfn[y.first],low[x]);
                        low[x]=dfn[y.first];
                    }else
                    if (low[x]<dfn[y.first]){
                        in_edge.combine(low[x],dfn[y.first]);
                        stack.combine(low[x],dfn[y.first]);
                    }
                }
            }
        }
        stack.clear(dfn[x]);
        in_edge.clear(dfn[x]);
        if (low[x]!=dfn[x])
            stack.add(low[x],x);
        else
            instack[x]=0;
    }
    void build(){
        for (int i=1;i<=n;i++)
            if (!dfn[i])
                tot=0,dfs(i,0);
    }
    void output(){
        sort(edge_on_simple_circle.begin(),edge_on_simple_circle.end());
        cout<<edge_on_simple_circle.size()<<endl;
        for (auto e:edge_on_simple_circle)
            cout<<e<<' ';
    }
}f;
int main(){
   // freopen("a.in","r",stdin);
    ios::sync_with_stdio(false);
    f.read();
    f.build();
    f.output();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mp(x,y)(make_pair(x,y))
const int N=1e5+1;
struct List{
	int head[N],tail[N],next[N];;
	void combine(int x,int y){
		if (head[y]){
			if (!head[x])head[x]=head[y],tail[x]=tail[y];
			else{
			    next[tail[x]]=head[y];
			    tail[x]=tail[y];
			}
			head[y]=tail[y]=0;
		}
	}
	void add(int x,int y){
		if (!head[x])head[x]=tail[x]=y;
		else
		    next[tail[x]]=y,tail[x]=y;
	}
	void clear(int x){
	    head[x]=tail[x]=0;
	}
};
struct Tarjan{
    vector<pair<int,int>>g[N];
    int n,m,tot;
    bool instack[N];
	List in_edge,stack;
    int dfn[N],low[N];
    vector<int>edge_on_simple_circle,d;
    void read(){
        cin>>n>>m;
        for (int i=1;i<=m;i++){
            int x,y;
            cin>>x>>y;
            g[x].push_back(mp(y,i));
            g[y].push_back(mp(x,i));
        }
    }
    void dfs(int x,int lastedge){
        dfn[x]=low[x]=++tot;
        instack[x]=1;
        for (auto y:g[x]){
            if (y.second==lastedge)continue;
            if (!dfn[y.first]){
                dfs(y.first,y.second);
                if (dfn[x]<low[y.first]){
                    //bridge
                    continue;
                }else{
                    in_edge.add(low[y.first],y.second);
                }
                if (low[y.first]==dfn[x]){
                    int calc=1;
                    int sig=dfn[x],p=stack.head[sig];
                    while (p){
                        instack[p]=0;
                        ++calc;
                        p=stack.next[p];
                    }
                    stack.clear(sig);
                    d.clear();
                    instack[x]=1;
                    int e=in_edge.head[sig];
                    while (e)d.push_back(e),e=in_edge.next[e];
                    in_edge.head[sig]=in_edge.tail[sig]=0;
                    if (d.size()==calc){
                        for (auto e:d)
                            edge_on_simple_circle.push_back(e);
                    }
                }else{
                    if (low[y.first]<low[x]){
                        in_edge.combine(low[y.first],low[x]);
                        stack.combine(low[y.first],low[x]);
                    }else
                        if (low[y.first]>low[x]){
                            in_edge.combine(low[x],low[y.first]);
                            stack.combine(low[x],low[y.first]);
                        }
                    low[x]=min(low[x],low[y.first]);
                }
            }else{
                if (instack[y.first]){
                    in_edge.add(dfn[y.first],y.second);
                    if (low[x]>dfn[y.first]){
                        in_edge.combine(dfn[y.first],low[x]);
                        stack.combine(dfn[y.first],low[x]);
                        low[x]=dfn[y.first];
                    }else
                    if (low[x]<dfn[y.first]){
                        in_edge.combine(low[x],dfn[y.first]);
                        stack.combine(low[x],dfn[y.first]);
                    }
                }
            }
        }
        if (low[x]!=dfn[x])
            stack.add(low[x],x);
        else
            instack[x]=0;
    }
    void build(){
        for (int i=1;i<=n;i++)
            if (!dfn[i])
                dfs(i,0);
    }
    void output(){
        sort(edge_on_simple_circle.begin(),edge_on_simple_circle.end());
        cout<<edge_on_simple_circle.size()<<endl;
        for (auto e:edge_on_simple_circle)
            cout<<e<<' ';
    }
}f;
int main(){
    ios::sync_with_stdio(false);
    f.read();
    f.build();
    f.output();
    return 0;
}


#include <iostream>
#include <vector>

int main(void) {

    int n, m;

    scanf("%d %d", &n, &m);

    int pupils[n];
    std::vector<int> v[m];
    int p[300001] = {-1};

    for (int i = 0; i < n; i++)
        scanf("%d", &pupils[i]);

    int a, b;
    while (m-- && scanf("%d %d", &a, &b))
        v[b].push_back(a);

    int counter = 0;

    for (int i = n - 1; i >= 0; i--)
		if(p[pupils[i]] == n - 1 - i - counter && i != n - 1)
			counter++;
		else
			for (int f: v[pupils[i]])
				p[f]++;

    printf("%d\n", counter);

    return 0;
}


#include <iostream>
// #include <vector>
#include <stdbool.h>

// struct pair {
//     int front;
//     int rear;
// };

int main(void) {

    int n, m;

    scanf("%d %d", &n, &m);

    int pupils[n];
    int pairs[m][2];

    for (int i = 0; i < n; i++)
        scanf("%d", &pupils[i]);

    for (int i = 0; i < m; i++)
        scanf("%d %d", &pairs[i][0], &pairs[i][1]);

    int counter = 0;
    int npos = n - 1;
    bool changed = false;
    // std::vector<struct pair> vp;

    do {
        changed = false;
        for (int j = 0; j < n - 1; j++) {
            for (int i = 0; i < m; i++) {

                // bool present = false;
                // for (int v = 0; v < vp.size() && !present; v++)
                //     if ((vp[v].front == pairs[i][0] && vp[v].rear == pairs[i][1]) ||
                //         (vp[v].front == pairs[i][1] && vp[v].rear == pairs[i][0]))
                //         present = true;
                //
                // if (present)
                //     continue;

                if (pupils[j] == pairs[i][0] && pupils[j + 1] == pairs[i][1]) {

                    if (j == npos)
                        continue;

                    int tmp = pupils[j];
                    pupils[j] = pupils[j + 1];
                    pupils[j + 1] = tmp;

                    if (j + 1 == npos) {
                        counter++;
                        npos--;
                    }

                    // struct pair p = { .front = pairs[i][0], .rear = pairs[i][1] };
                    // vp.push_back(p);

                    for (int k = i + 1; k < m; k++)
                        if (pairs[k][1] == pairs[i][0] && pairs[k][0] == pairs[i][1])
                            pairs[k][0] = pairs[k][1] = -1;

                    pairs[i][0] = pairs[i][1] = -1;

                    changed = true;
                }
            }
        }
    } while (changed);

    printf("%d\n", counter);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef tuple<int,int,int> piii;
typedef pair<int,ll> pil;
typedef pair<ll,ll> pll;
typedef tuple<ll,ll,ll> plll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pil> vil;
typedef vector<pll> vll;
typedef vector<piii> viii;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef int (*uniopi) (int);
typedef ll (*uniopl) (ll);
typedef int (*binopi) (int, int);
typedef ll (*binopl) (ll, ll);
typedef int (*tupopi) (int, int, int);
typedef ll (*tupopl) (ll, ll, ll);

const ld PI = acos(0)*2;
const int INF = 0x3f2f1f0f;
const ll LINF = 1ll*INF*INF;
int X4[] = {-1, 0, 1, 0}, Y4[] = {0, -1, 0, 1};
int X8[] = {-1, -1, -1, 0, 1, 1, 1, 0}, Y8[] = {-1, 0, 1, 1, 1, 0, -1, -1};

#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SZ(x) ((int) (x).size())
#define NTH(v, x) ((int) (lower_bound(ALL(v), (x)) - (v).begin()))
#define SAME(v, x) ((int) (upper_bound(ALL(v), (x)) - lower_bound(ALL(v), (x))))
#define UNIQUE(v) v.erase(unique(ALL(v)), (v).end())
#define WWW << ' ' <<

#define pb push_back
#define eb emplace_back
#define INP(t, x) t x; cin >> x

#define CPPIO ios::sync_with_stdio(0); cin.tie(nullptr); cout << std::setprecision(10); cout << fixed
#define forn(x, n) for (int x=0; x<(n); x++)
#define for1(x, n) for (int x=1; x<=(n); x++)
#define ford(x, n) for (int x=(n)-1; x>=0; x--)
#define ford1(x, n) for (int x=(n); x>=1; x--)
#define forr(x, n, m) for (int x=(n); x<(m); x++)
#define forrd(x, n, m) for (int x=(m)-1; x>(n)-1; x--)

template<typename T> inline bool uax(T &a, T b) { return a<b? (a=b, 1) : 0; }
template<typename T> inline bool uin(T &a, T b) { return a>b? (a=b, 1) : 0; }
template<typename T1, typename T2> istream& operator>>(istream& is, pair<T1,T2>& p) {
	return is >> p.first >> p.second;
}
template<typename T1, typename T2> ostream& operator<<(ostream& os, const pair<T1,T2>& p) {
	return os << '(' << p.first << ", " << p.second << ")";
}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
	for (int i=0; i<(int)v.size(); i++) { os << v[i]; if (i+1<(int)v.size()) os << ' '; } return os;
}

//////// ---- Woosung Song's Source Code ---- ////////

vi Pos;

int main() {
	CPPIO;

	//////// ---- Woosung Song's Source Code ---- ////////

	int n, m; cin >> n >> m;
	map<pii,bool> pr;
	vvi adj(n+1, vi());
	vi pos(n+1), p(n+1);

	for (int i=1; i<=n; i++) {
		int pi; cin >> pi;
		p[i] = pi;
		pos[pi] = i;
	}
	Pos = pos;

	forn(__i, m) {
		int a, b; cin >> a >> b;
		pr[{a,b}] = 1;
		adj[b].pb(a);
	}

	int l = p[n];
	vi Al, passed(n+1, 0), rrrr(n+2, 0), llll(n+2, 0);
	for (auto &x : adj[l]) Al.pb(x);
	for (int i=0; i<=n+1; i++) rrrr[i] = i+1, llll[i] = i-1;
	sort(ALL(Al), [](const int &i, const int &j) { return Pos[i] < Pos[j]; });
	reverse(ALL(Al));
	//cout << Al << endl;
	int nAl = SZ(Al);
	int k = 0, cnt = 0;
	for (; k<nAl; k++) {
		int can = 1;
		int ik = Al[k], pik = pos[ik];
		//cout << "ik: " << ik << endl;
		for (int j=rrrr[pik]; j<=n-1; j=rrrr[j]) {
			//cout << ik WWW p[j] WWW pr[{ik, p[j]}] << endl;
			if (passed[p[j]]) continue;
			if (!pr[{ik, p[j]}]) {
				can = 0;
				break;
			}
		}
		if (can) {
			cnt++, passed[ik] = 1;
			rrrr[llll[pik]] = rrrr[pik];
			llll[rrrr[pik]] = llll[pik];
		}
	}
	cout << cnt << endl;

#ifdef LOCAL_DEFINE
	cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\n";
#endif
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef tuple<int,int,int> piii;
typedef pair<int,ll> pil;
typedef pair<ll,ll> pll;
typedef tuple<ll,ll,ll> plll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pil> vil;
typedef vector<pll> vll;
typedef vector<piii> viii;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef int (*uniopi) (int);
typedef ll (*uniopl) (ll);
typedef int (*binopi) (int, int);
typedef ll (*binopl) (ll, ll);
typedef int (*tupopi) (int, int, int);
typedef ll (*tupopl) (ll, ll, ll);

const ld PI = acos(0)*2;
const int INF = 0x3f2f1f0f;
const ll LINF = 1ll*INF*INF;
int X4[] = {-1, 0, 1, 0}, Y4[] = {0, -1, 0, 1};
int X8[] = {-1, -1, -1, 0, 1, 1, 1, 0}, Y8[] = {-1, 0, 1, 1, 1, 0, -1, -1};

#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SZ(x) ((int) (x).size())
#define NTH(v, x) ((int) (lower_bound(ALL(v), (x)) - (v).begin()))
#define SAME(v, x) ((int) (upper_bound(ALL(v), (x)) - lower_bound(ALL(v), (x))))
#define UNIQUE(v) v.erase(unique(ALL(v)), (v).end())
#define WWW << ' ' <<

#define pb push_back
#define eb emplace_back
#define INP(t, x) t x; cin >> x

#define CPPIO ios::sync_with_stdio(0); cin.tie(nullptr); cout << std::setprecision(10); cout << fixed
#define forn(x, n) for (int x=0; x<(n); x++)
#define for1(x, n) for (int x=1; x<=(n); x++)
#define ford(x, n) for (int x=(n)-1; x>=0; x--)
#define ford1(x, n) for (int x=(n); x>=1; x--)
#define forr(x, n, m) for (int x=(n); x<(m); x++)
#define forrd(x, n, m) for (int x=(m)-1; x>(n)-1; x--)

template<typename T> inline bool uax(T &a, T b) { return a<b? (a=b, 1) : 0; }
template<typename T> inline bool uin(T &a, T b) { return a>b? (a=b, 1) : 0; }
template<typename T1, typename T2> istream& operator>>(istream& is, pair<T1,T2>& p) {
	return is >> p.first >> p.second;
}
template<typename T1, typename T2> ostream& operator<<(ostream& os, const pair<T1,T2>& p) {
	return os << '(' << p.first << ", " << p.second << ")";
}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
	for (int i=0; i<(int)v.size(); i++) { os << v[i]; if (i+1<(int)v.size()) os << ' '; } return os;
}

//////// ---- Woosung Song's Source Code ---- ////////

vi Pos;

int main() {
	CPPIO;

	//////// ---- Woosung Song's Source Code ---- ////////

	int n, m; cin >> n >> m;
	map<pii,bool> pr;
	vvi adj(n+1, vi());
	vi pos(n+1), p(n+1);

	for (int i=1; i<=n; i++) {
		int pi; cin >> pi;
		p[i] = pi;
		pos[pi] = i;
	}
	Pos = pos;

	forn(__i, m) {
		int a, b; cin >> a >> b;
		pr[{a,b}] = 1;
		adj[b].pb(a);
	}

	int l = p[n];
	vi Al, inAl(n+1, 0);
	for (auto &x : adj[l]) Al.pb(x), inAl[x] = 1;
	sort(ALL(Al), [](const int &i, const int &j) { return Pos[i] < Pos[j]; });
	reverse(ALL(Al));
	int nAl = SZ(Al);
	int k = 0;
	for (; k<nAl; k++) {
		int can = 1;
		int ik = Al[k], pik = pos[ik];
		//cout << "ik: " << ik << endl;
		for (int j=pik+1; j<=n-1; j++) {
			//cout << ik WWW p[j] WWW pr[{ik, p[j]}] << endl;
			if (inAl[p[j]]) continue;
			if (!pr[{ik, p[j]}]) {
				can = 0;
				break;
			}
		}
		if (!can) break;
	}
	cout << k << endl;

#ifdef LOCAL_DEFINE
	cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\n";
#endif
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef tuple<int,int,int> piii;
typedef pair<int,ll> pil;
typedef pair<ll,ll> pll;
typedef tuple<ll,ll,ll> plll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pil> vil;
typedef vector<pll> vll;
typedef vector<piii> viii;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef int (*uniopi) (int);
typedef ll (*uniopl) (ll);
typedef int (*binopi) (int, int);
typedef ll (*binopl) (ll, ll);
typedef int (*tupopi) (int, int, int);
typedef ll (*tupopl) (ll, ll, ll);

const ld PI = acos(0)*2;
const int INF = 0x3f2f1f0f;
const ll LINF = 1ll*INF*INF;
int X4[] = {-1, 0, 1, 0}, Y4[] = {0, -1, 0, 1};
int X8[] = {-1, -1, -1, 0, 1, 1, 1, 0}, Y8[] = {-1, 0, 1, 1, 1, 0, -1, -1};

#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SZ(x) ((int) (x).size())
#define NTH(v, x) ((int) (lower_bound(ALL(v), (x)) - (v).begin()))
#define SAME(v, x) ((int) (upper_bound(ALL(v), (x)) - lower_bound(ALL(v), (x))))
#define UNIQUE(v) v.erase(unique(ALL(v)), (v).end())
#define WWW << ' ' <<

#define pb push_back
#define eb emplace_back
#define INP(t, x) t x; cin >> x

#define CPPIO ios::sync_with_stdio(0); cin.tie(nullptr); cout << std::setprecision(10); cout << fixed
#define forn(x, n) for (int x=0; x<(n); x++)
#define for1(x, n) for (int x=1; x<=(n); x++)
#define ford(x, n) for (int x=(n)-1; x>=0; x--)
#define ford1(x, n) for (int x=(n); x>=1; x--)
#define forr(x, n, m) for (int x=(n); x<(m); x++)
#define forrd(x, n, m) for (int x=(m)-1; x>(n)-1; x--)

template<typename T> inline bool uax(T &a, T b) { return a<b? (a=b, 1) : 0; }
template<typename T> inline bool uin(T &a, T b) { return a>b? (a=b, 1) : 0; }
template<typename T1, typename T2> istream& operator>>(istream& is, pair<T1,T2>& p) {
	return is >> p.first >> p.second;
}
template<typename T1, typename T2> ostream& operator<<(ostream& os, const pair<T1,T2>& p) {
	return os << '(' << p.first << ", " << p.second << ")";
}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
	for (int i=0; i<(int)v.size(); i++) { os << v[i]; if (i+1<(int)v.size()) os << ' '; } return os;
}

//////// ---- Woosung Song's Source Code ---- ////////

vi Pos;

int main() {
	CPPIO;

	//////// ---- Woosung Song's Source Code ---- ////////

	int n, m; cin >> n >> m;
	map<pii,bool> pr;
	vvi adj(n+1, vi());
	vi pos(n+1), p(n+1);

	for (int i=1; i<=n; i++) {
		int pi; cin >> pi;
		p[i] = pi;
		pos[pi] = i;
	}
	Pos = pos;

	forn(__i, m) {
		int a, b; cin >> a >> b;
		pr[{a,b}] = 1;
		adj[b].pb(a);
	}

	int l = p[n];
	vi Al, passed(n+1, 0);
	for (auto &x : adj[l]) Al.pb(x);
	sort(ALL(Al), [](const int &i, const int &j) { return Pos[i] < Pos[j]; });
	reverse(ALL(Al));
	//cout << Al << endl;
	int nAl = SZ(Al);
	int k = 0, cnt = 0;
	for (; k<nAl; k++) {
		int can = 1;
		int ik = Al[k], pik = pos[ik];
		//cout << "ik: " << ik << endl;
		for (int j=pik+1; j<=n-1; j++) {
			//cout << ik WWW p[j] WWW pr[{ik, p[j]}] << endl;
			if (passed[p[j]]) continue;
			if (!pr[{ik, p[j]}]) {
				can = 0;
				break;
			}
		}
		if (can) cnt++, passed[ik] = 1;
	}
	cout << cnt << endl;

#ifdef LOCAL_DEFINE
	cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\n";
#endif
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef tuple<int,int,int> piii;
typedef pair<int,ll> pil;
typedef pair<ll,ll> pll;
typedef tuple<ll,ll,ll> plll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pil> vil;
typedef vector<pll> vll;
typedef vector<piii> viii;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef int (*uniopi) (int);
typedef ll (*uniopl) (ll);
typedef int (*binopi) (int, int);
typedef ll (*binopl) (ll, ll);
typedef int (*tupopi) (int, int, int);
typedef ll (*tupopl) (ll, ll, ll);

const ld PI = acos(0)*2;
const int INF = 0x3f2f1f0f;
const ll LINF = 1ll*INF*INF;
int X4[] = {-1, 0, 1, 0}, Y4[] = {0, -1, 0, 1};
int X8[] = {-1, -1, -1, 0, 1, 1, 1, 0}, Y8[] = {-1, 0, 1, 1, 1, 0, -1, -1};

#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SZ(x) ((int) (x).size())
#define NTH(v, x) ((int) (lower_bound(ALL(v), (x)) - (v).begin()))
#define SAME(v, x) ((int) (upper_bound(ALL(v), (x)) - lower_bound(ALL(v), (x))))
#define UNIQUE(v) v.erase(unique(ALL(v)), (v).end())
#define WWW << ' ' <<

#define pb push_back
#define eb emplace_back
#define INP(t, x) t x; cin >> x

#define CPPIO ios::sync_with_stdio(0); cin.tie(nullptr); cout << std::setprecision(10); cout << fixed
#define forn(x, n) for (int x=0; x<(n); x++)
#define for1(x, n) for (int x=1; x<=(n); x++)
#define ford(x, n) for (int x=(n)-1; x>=0; x--)
#define ford1(x, n) for (int x=(n); x>=1; x--)
#define forr(x, n, m) for (int x=(n); x<(m); x++)
#define forrd(x, n, m) for (int x=(m)-1; x>(n)-1; x--)

template<typename T> inline bool uax(T &a, T b) { return a<b? (a=b, 1) : 0; }
template<typename T> inline bool uin(T &a, T b) { return a>b? (a=b, 1) : 0; }
template<typename T1, typename T2> istream& operator>>(istream& is, pair<T1,T2>& p) {
	return is >> p.first >> p.second;
}
template<typename T1, typename T2> ostream& operator<<(ostream& os, const pair<T1,T2>& p) {
	return os << '(' << p.first << ", " << p.second << ")";
}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
	for (int i=0; i<(int)v.size(); i++) { os << v[i]; if (i+1<(int)v.size()) os << ' '; } return os;
}

//////// ---- Woosung Song's Source Code ---- ////////

vi Pos;

int main() {
	CPPIO;

	//////// ---- Woosung Song's Source Code ---- ////////

	int n, m; cin >> n >> m;
	map<pii,bool> pr;
	vvi adj(n+1, vi());
	vi pos(n+1), p(n+1);

	for (int i=1; i<=n; i++) {
		int pi; cin >> pi;
		p[i] = pi;
		pos[pi] = i;
	}
	Pos = pos;

	forn(__i, m) {
		int a, b; cin >> a >> b;
		pr[{a,b}] = pr[{b,a}] = 1;
		adj[a].pb(b); adj[b].pb(a);
	}

	int l = p[n];
	vi Al;
	for (auto &x : adj[l]) Al.pb(x);
	sort(ALL(Al), [](const int &i, const int &j) { return Pos[i] < Pos[j]; });
	reverse(ALL(Al));
	int nAl = SZ(Al);
	int k = 0;
	for (; k<nAl; k++) {
		int can = 1;
		int ik = Al[k], pik = pos[ik];
		//cout << "ik: " << ik << endl;
		for (int j=pik+1; j<=n-1-k; j++) {
			//cout << ik WWW p[j] WWW pr[{ik, p[j]}] << endl;
			if (!pr[{ik, p[j]}]) {
				can = 0;
				break;
			}
		}
		if (!can) break;
	}
	cout << k << endl;

#ifdef LOCAL_DEFINE
	cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\n";
#endif
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef tuple<int,int,int> piii;
typedef pair<int,ll> pil;
typedef pair<ll,ll> pll;
typedef tuple<ll,ll,ll> plll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pil> vil;
typedef vector<pll> vll;
typedef vector<piii> viii;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef int (*uniopi) (int);
typedef ll (*uniopl) (ll);
typedef int (*binopi) (int, int);
typedef ll (*binopl) (ll, ll);
typedef int (*tupopi) (int, int, int);
typedef ll (*tupopl) (ll, ll, ll);

const ld PI = acos(0)*2;
const int INF = 0x3f2f1f0f;
const ll LINF = 1ll*INF*INF;
int X4[] = {-1, 0, 1, 0}, Y4[] = {0, -1, 0, 1};
int X8[] = {-1, -1, -1, 0, 1, 1, 1, 0}, Y8[] = {-1, 0, 1, 1, 1, 0, -1, -1};

#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SZ(x) ((int) (x).size())
#define NTH(v, x) ((int) (lower_bound(ALL(v), (x)) - (v).begin()))
#define SAME(v, x) ((int) (upper_bound(ALL(v), (x)) - lower_bound(ALL(v), (x))))
#define UNIQUE(v) v.erase(unique(ALL(v)), (v).end())
#define WWW << ' ' <<

#define pb push_back
#define eb emplace_back
#define INP(t, x) t x; cin >> x

#define CPPIO ios::sync_with_stdio(0); cin.tie(nullptr); cout << std::setprecision(10); cout << fixed
#define forn(x, n) for (int x=0; x<(n); x++)
#define for1(x, n) for (int x=1; x<=(n); x++)
#define ford(x, n) for (int x=(n)-1; x>=0; x--)
#define ford1(x, n) for (int x=(n); x>=1; x--)
#define forr(x, n, m) for (int x=(n); x<(m); x++)
#define forrd(x, n, m) for (int x=(m)-1; x>(n)-1; x--)

template<typename T> inline bool uax(T &a, T b) { return a<b? (a=b, 1) : 0; }
template<typename T> inline bool uin(T &a, T b) { return a>b? (a=b, 1) : 0; }
template<typename T1, typename T2> istream& operator>>(istream& is, pair<T1,T2>& p) {
	return is >> p.first >> p.second;
}
template<typename T1, typename T2> ostream& operator<<(ostream& os, const pair<T1,T2>& p) {
	return os << '(' << p.first << ", " << p.second << ")";
}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
	for (int i=0; i<(int)v.size(); i++) { os << v[i]; if (i+1<(int)v.size()) os << ' '; } return os;
}

//////// ---- Woosung Song's Source Code ---- ////////

vi Pos;

int main() {
	CPPIO;

	//////// ---- Woosung Song's Source Code ---- ////////

	int n, m; cin >> n >> m;
	map<pii,bool> pr;
	vvi adj(n+1, vi());
	vi pos(n+1), p(n+1);

	for (int i=1; i<=n; i++) {
		int pi; cin >> pi;
		p[i] = pi;
		pos[pi] = i;
	}
	Pos = pos;

	forn(__i, m) {
		int a, b; cin >> a >> b;
		pr[{a,b}] = pr[{b,a}] = 1;
		adj[a].pb(b); adj[b].pb(a);
	}

	int l = p[n];
	vi Al, inAl(n+1, 0);
	for (auto &x : adj[l]) Al.pb(x), inAl[x] = 1;
	sort(ALL(Al), [](const int &i, const int &j) { return Pos[i] < Pos[j]; });
	reverse(ALL(Al));
	int nAl = SZ(Al);
	int k = 0;
	for (; k<nAl; k++) {
		int can = 1;
		int ik = Al[k], pik = pos[ik];
		//cout << "ik: " << ik << endl;
		for (int j=pik+1; j<=n-1; j++) {
			//cout << ik WWW p[j] WWW pr[{ik, p[j]}] << endl;
			if (inAl[p[j]]) continue;
			if (!pr[{ik, p[j]}]) {
				can = 0;
				break;
			}
		}
		if (!can) break;
	}
	cout << k << endl;

#ifdef LOCAL_DEFINE
	cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\n";
#endif
	return 0;
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define MOD ((int)1e9+7)
#define endl "\n"

#define yn(f) ((f)?"Yes":"No")
#define YN(f) ((f)?"YES":"NO")

#define MAX 310000

vector<int> p, used, temp[MAX];
int temp2[MAX];

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	int n, m;
	int u, v;
	int ans = 0, con;

	cin>>n>>m;

	p.resize(n+1);
	used.resize(n+1);

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

	used[p[n]] = true;

	for(int i = 0; i < m; i++){
		cin>>u>>v;
		if(v == p[n]) used[u] = true;
		temp[u].push_back(v);
	}

	con = n;

	for(int i = n; i >= 1; i--,con--){
		// cout<<" i = "<<i<<" "<<used[p[i]]<<" "<<p[i]<<endl;
		if(used[p[i]] == false){
			break;
		}
	}

	ans = n-con-1;

	// cout<<"ans ->"<<ans<<" "<<n<<" "<<con<<" - "<<endl;

	for(int i = 1; i <= n; i++){
		if(i == p[n]) continue;
		if(used[i] == false) continue;
		for(int j = 0; j < temp[i].size(); j++){
			temp2[temp[i][j]] = i;
		}
		int temp3 = con;
		bool flag = false;
		for(int j = con; j >= 1; j--,temp3--){
			// cout<<"i j "<<i<<" "<<j<<" "<<temp3<<endl;

			if(p[j] == i){
				flag = true;
				break;
			}
			if(temp2[p[j]] != i){
				break;
			}
		}
		if(flag)ans = max(ans,  n-con);
	}

	cout<<ans<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define MOD ((int)1e9+7)
#define endl "\n"

#define yn(f) ((f)?"Yes":"No")
#define YN(f) ((f)?"YES":"NO")

#define MAX 310000

vector<int> p, used, temp[MAX];
int temp2[MAX];

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	int n, m;
	int u, v;
	int ans = 0, con;

	cin>>n>>m;

	p.resize(n+1);
	used.resize(n+1);

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

	for(int i = 0; i < m; i++){
		cin>>u>>v;
		if(v == p[n]) used[u] = true;
		temp[u].push_back(v);
	}

	con = n-1;

	for(int i = n-1; i >= 0; i--,con--){
		// cout<<" i = "<<i<<" "<<used[p[i]]<<" "<<p[i]<<endl;
		if(used[p[i]] == false){
			break;
		}
	}

	ans = n-con-1;

	// cout<<"ans ->"<<ans<<" "<<n<<" "<<con<<" - "<<1<<endl;

	for(int i = 1; i <= n; i++){
		if(used[i] == false) continue;
		for(int j = 0; j < temp[i].size(); j++){
			temp2[temp[i][j]] = i;
		}
		int temp3 = con;
		for(int j = con; j >= 0; j--,temp3--){
			// cout<<"i j "<<i<<" "<<j<<" "<<temp3<<endl;
			if(temp2[p[j]] != i){
				break;
			}
		}
		ans = max(ans, n-temp3-1);
	}

	cout<<ans<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define MOD ((int)1e9+7)
#define endl "\n"

#define yn(f) ((f)?"Yes":"No")
#define YN(f) ((f)?"YES":"NO")

#define MAX 310000

vector<int> p, used, temp[MAX];
int temp2[MAX];

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	int n, m;
	int u, v;
	int ans = 0, con;

	cin>>n>>m;

	p.resize(n+1);
	used.resize(n+1);

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

	used[p[n]] = true;

	for(int i = 0; i < m; i++){
		cin>>u>>v;
		if(v == p[n]) used[u] = true;
		temp[u].push_back(v);
	}

	con = n;

	for(int i = n; i >= 1; i--,con--){
		// cout<<" i = "<<i<<" "<<used[p[i]]<<" "<<p[i]<<endl;
		if(used[p[i]] == false){
			break;
		}
	}

	ans = n-con-1;

	// cout<<"ans ->"<<ans<<" "<<n<<" "<<con<<" - "<<endl;

	for(int i = 1; i <= n; i++){
		if(i == p[n]) continue;
		if(used[i] == false) continue;
		for(int j = 0; j < temp[i].size(); j++){
			temp2[temp[i][j]] = i;
		}
		int temp3 = con;
		bool flag = false;
		for(int j = con; j >= 1; j--,temp3--){
			// cout<<"i j "<<i<<" "<<j<<" "<<temp3<<endl;
			if(temp2[p[j]] != i){
				break;
			}
			if(p[j] == i){
				flag = true;
				break;
			}
		}
		ans = max(ans, n-temp3-1);
	}

	cout<<ans<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define MOD ((int)1e9+7)
#define endl "\n"

#define yn(f) ((f)?"Yes":"No")
#define YN(f) ((f)?"YES":"NO")

#define MAX 310000

vector<int> p, used, temp[MAX];
int temp2[MAX];

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	int n, m, E;
	int u, v;
	int ans = 0, con;

	cin>>n>>m;

	p.resize(n+1);
	// P.resize(n+1);
	used.resize(n+1);

	for(int i = 1; i <= n; i++){
		cin>>p[i];
		// P[i] = p[i];
	}
	E = p[n];

	used[E] = true;

	for(int i = 0; i < m; i++){
		cin>>u>>v;
		if(v == E) used[u] = true;
		temp[u].push_back(v);
	}

	for(int i = n-1; i >= 1; i--){
		if(used[p[i]] == false) continue;

		for(int j = 0; j < temp[p[i]].size(); j++){
			temp2[temp[p[i]][j]] = i;
			// cout<<i<<" "<<j<<" "<<temp[p[i]][j]<<endl;
		}
		bool flag = true;
		for(int j = i+1; j <= n; j++){//cout<<" j "<<j<<endl;
			// cout<<temp2[p[j]]<<" "<<i<<endl;
			if(temp2[p[j]] != i){
				flag = false;
				break;
			}
			swap(p[j-1],p[j]);

			if(E == p[j-1])break;

		}
	}

	for(int i = n; i >= 1; i--, ans++){
		if(p[i] == E) break;
		// cout<<i<<" "<<p[i]<<endl;
	}

	cout<<ans<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define MOD ((int)1e9+7)
#define endl "\n"

#define yn(f) ((f)?"Yes":"No")
#define YN(f) ((f)?"YES":"NO")

#define MAX 310000

vector<int> p, used, temp[MAX];
int temp2[MAX];

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	int n, m;
	int u, v;
	int ans = 0, con;

	cin>>n>>m;

	p.resize(n+1);
	used.resize(n+1);

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

	used[p[n]] = true;

	for(int i = 0; i < m; i++){
		cin>>u>>v;
		if(v == p[n]) used[u] = true;
		temp[u].push_back(v);
	}

	con = n;

	for(int i = n; i >= 1; i--,con--){
		// cout<<" i = "<<i<<" "<<used[p[i]]<<" "<<p[i]<<endl;
		if(used[p[i]] == false){
			break;
		}
	}

	ans = n-con-1;

	// cout<<"ans ->"<<ans<<" "<<n<<" "<<con<<" - "<<endl;

	for(int i = 1; i <= n; i++){
		if(i == p[n]) continue;
		if(used[i] == false) continue;
		for(int j = 0; j < temp[i].size(); j++){
			temp2[temp[i][j]] = i;
		}
		int temp3 = con;
		for(int j = con; j >= 1; j--,temp3--){
			// cout<<"i j "<<i<<" "<<j<<" "<<temp3<<endl;
			if(temp2[p[j]] != i){
				break;
			}
		}
		ans = max(ans, n-temp3-1);
	}

	cout<<ans<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define MOD ((int)1e9+7)
#define endl "\n"

#define yn(f) ((f)?"Yes":"No")
#define YN(f) ((f)?"YES":"NO")

#define MAX 310000

vector<int> p, used, temp[MAX];
int temp2[MAX];

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	int n, m;
	int u, v;
	int ans = 0, con;

	cin>>n>>m;

	p.resize(n+1);
	used.resize(n+1);

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

	for(int i = 0; i < m; i++){
		cin>>u>>v;
		if(v == p[n]) used[u] = true;
		temp[u].push_back(v);
	}

	con = n-1;

	for(int i = n-1; i >= 0; i--,con--){
		// cout<<" i = "<<i<<" "<<used[p[i]]<<" "<<p[i]<<endl;
		if(used[p[i]] == false){
			break;
		}
	}

	ans = n-con-1;

	// cout<<"ans ->"<<ans<<" "<<n<<" "<<con<<" - "<<1<<endl;

	for(int i = 1; i <= n; i++){
		if(used[i] == false) continue;
		for(int j = 0; j < temp[i].size(); j++){
			temp2[temp[i][j]] = i;
		}
		int temp3 = con;
		for(int j = con; j >= 1; j--,temp3--){
			// cout<<"i j "<<i<<" "<<j<<" "<<temp3<<endl;
			if(temp2[p[j]] != i){
				break;
			}
		}
		ans = max(ans, n-temp3-1);
	}

	cout<<ans<<endl;

	return 0;
}


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

int n,m;
int p[400006];
int idx[500005];
int me;

bool gd[400006];

int cnt[500005];

unordered_set<int>st[300005];
pair<int,int> pr;

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",p+i);
        idx[p[i]]=i;
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d",&pr.first,&pr.second);
        if(pr.second==p[n])gd[pr.first]=1;
        st[pr.first].insert(pr.second);
        if(idx[pr.first]<idx[pr.second])
        cnt[pr.second]++;
    }
    me=n;

    bool flag=1;
    while(flag){
        flag=0;
        while(me){
            if(st[p[me-1]].count(p[me]))me--;
            else break;
        }
        for(int i=1;i<me;i++){
            if(gd[p[i]]){
                if(st[p[i]].count(p[i+1])){
                    flag=1;
                    swap(p[i],p[i+1]);
                    if(i+1==me){me--;}
                }
            }
            else{
                if(i>1&&st[p[i-1]].count(p[i])){
                    flag=1;
                    swap(p[i],p[i-1]);
                }
            }
        }
    }
    cout<<n-me;
    return 0;
}


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

int n,m;
int p[400006];
int me;

int cnt[400006];

unordered_set<int>st[300005];
pair<int,int> pr;

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",p+i);
        st[0].insert(i);
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d",&pr.first,&pr.second);
        cnt[pr.first]++;
        cnt[pr.second]++;
        st[pr.first].insert(pr.second);
    }
    me=p[n];

    int ans=0;
    for(int i=n-1;i>=1;i--){
        if(st[p[i]].count(me)==0)continue;

        bool ok=1;
        for(int j=i+1;j<n;j++){
            if(!st[p[i]].count(p[j])){ok=0;break;}
        }
        if(ok){ans++;p[i]=0;}
    }
    cout<<ans;
    return 0;
}


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

int n,m;
int p[400006];
int me;

int cnt[400006];

unordered_set<int>st[300005];
pair<int,int> pr;

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",p+i);
        st[0].insert(i);
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d",&pr.first,&pr.second);
        cnt[pr.first]++;
        cnt[pr.second]++;
        st[pr.first].insert(pr.second);
    }
    me=p[n];

    int ans=0;
    for(int i=n-1;i>=1;i--){
        if(st[p[i]].count(me)==0)continue;

        bool ok=1;
        for(int j=i+1;j<n;j++){
            if(!st[p[j]].count(p[i])){ok=0;break;}
        }
        if(ok){ans++;p[i]=0;}
    }
    cout<<ans;
    return 0;
}


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

int n,m;
int p[400006];
int me;

bool gd[400006];

unordered_set<int>st[300005];
pair<int,int> pr;

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",p+i);
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d",&pr.first,&pr.second);
        if(pr.second==p[n])gd[pr.first]=1;
        st[pr.first].insert(pr.second);
    }
    me=n;

    bool flag=1;
    while(flag){
        flag=0;
        for(int i=1;i<me;i++){
            if(gd[p[i]]){
                if(st[p[i]].count(p[i+1])){
                    flag=1;
                    swap(p[i],p[i+1]);
                    if(i+1==me){me--;}
                }
            }
            else{
                if(i>1&&st[p[i-1]].count(p[i])){
                    flag=1;
                    swap(p[i],p[i-1]);
                }
            }
        }
    }
    cout<<n-me;
    return 0;
}


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

int n,m;
int p[400006];
int me;

set<int>st[300005];
pair<int,int> pr;

int main(){
    scanf("%d%d",&n,&m);
    vector<int> vec(n);
    for(int i=1;i<=n;i++){
        scanf("%d",p+i);
        vec[i-1]=p[i];
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d",&pr.first,&pr.second);
        st[pr.first].insert(pr.second);
    }
    me=p[n];
    vec.pop_back();
    for(int i=(int)vec.size()-1;i>=0;i--){
        int now=vec[i];
        for(int j=i+1;j<vec.size();j++){
            if(st[now].count(vec[j])){
                swap(vec[j],vec[j-1]);
            }
            else break;
        }
        if(st[vec.back()].count(me)){
            vec.pop_back();
        }
    }
    cout<<n-vec.size()-1;
    return 0;
}


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

int n,m;
int p[400006];
int me;

set<int>st[300005];
pair<int,int> pr;

int main(){
    scanf("%d%d",&n,&m);
    vector<int> vec(n);
    for(int i=1;i<=n;i++){
        scanf("%d",p+i);
        vec[i-1]=p[i];
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d",&pr.first,&pr.second);
        st[pr.first].insert(pr.second);
    }
    me=p[n];
    vec.pop_back();
    for(int i=(int)vec.size()-1;i>=0;i--){
        int now=vec[i];
        for(int j=i+1;j<vec.size();j++){
            if(st[now].count(vec[j])){
                swap(vec[j],vec[j-1]);
            }
        }
        if(st[vec.back()].count(me)){
            vec.pop_back();
        }
    }
    cout<<n-vec.size();
    return 0;
}


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

int n,m;
int p[400006];
int me;

bool gd[400006];

unordered_set<int>st[300005];
pair<int,int> pr;

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",p+i);
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d",&pr.first,&pr.second);
        if(pr.second==p[n])gd[pr.first]=1;
        st[pr.first].insert(pr.second);
    }
    me=n;

    bool flag=1;
    while(flag){
        flag=0;
        while(me){
            if(st[p[me-1]].count(p[me]))me--;
            else break;
        }
        for(int i=1;i<me;i++){
            if(gd[p[i]]){
                if(st[p[i]].count(p[i+1])){
                    flag=1;
                    swap(p[i],p[i+1]);
                    if(i+1==me){me--;}
                }
            }
            else{
                if(i>1&&st[p[i-1]].count(p[i])){
                    flag=1;
                    swap(p[i],p[i-1]);
                }
            }
        }
    }
    cout<<n-me;
    return 0;
}


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

int n,m;
int p[400006];
int me;

set<int>st[300005];
pair<int,int> pr;

int main(){
    scanf("%d%d",&n,&m);
    vector<int> vec(n);
    for(int i=1;i<=n;i++){
        scanf("%d",p+i);
        vec[i-1]=p[i];
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d",&pr.first,&pr.second);
        st[pr.first].insert(pr.second);
    }
    me=p[n];
    vec.pop_back();
    for(int i=(int)vec.size()-1;i>=0;i--){
        int now=vec[i];
        for(int j=i+1;j<vec.size();j++){
            if(st[now].count(vec[j])){
                swap(vec[j],vec[j-1]);
            }
        }
        if(st[vec.back()].count(me)){
            vec.pop_back();
        }
    }
    cout<<n-vec.size()-1;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

vector<int> p;
vector<set<int> > swaps;

int main(){
    int n, m;
    cin >> n >> m;
    p.resize(n);
    swaps.resize(n+1);
    for(int& i: p) cin >> i;
    for(int i = 0; i < m; i++){
        int u, v;
        cin >> u >> v;
        swaps[v].insert(u);
    }
    set<int> swappables;
    for(int i: swaps[p.back()]) swappables.insert(i);
    int res = 0;
    for(int i = n-2; i >= 0; i--){
        if(swappables.find(p[i]) != swappables.end()) ++res;
        else{
            for(auto it = swappables.begin(); it != swappables.end(); ){
                if(swaps[p[i]].find(*it) != swaps[p[i]].end()) ++it;
                else it = swappables.erase(it);
            }
        }
    }
    cout << res << endl;
}


#include<bits/stdc++.h>

using namespace std;

vector<int> p;
set<pair<int, int> > swaps;

bool swappable(int id, set<int>& toSwap){
    for(int i: toSwap){
        if(swaps.find(make_pair(id, i)) == swaps.end()) return false;
        swaps.erase(make_pair(id, i));
    }
    return true;
}

int main(){
    srand(time(0));
    int n, m;
    cin >> n >> m;
    p.resize(n);
    for(int& i: p) cin >> i;
    for(int i = 0; i < m; i++){
        int u, v;
        cin >> u >> v;
        swaps.emplace(u, v);
    }
    set<int> toSwap;
    toSwap.insert(p.back());
    int res = 0;
    for(int i = n-2; i >= 0; i--){
        if(swappable(p[i], toSwap)) ++res;
        else toSwap.insert(p[i]);
    }
    cout << res << endl;
}


#include<bits/stdc++.h>

using namespace std;

vector<int> p;
set<pair<int, int> > swaps;

bool swappable(int id, set<int>& toSwap){
    for(int i: toSwap){
        if(swaps.find(make_pair(id, i)) == swaps.end()) return false;
        swaps.erase(make_pair(id, i));
    }
    return true;
}

int main(){
    srand(time(0));
    int n, m;
    cin >> n >> m;
    p.resize(n);
    for(int& i: p) cin >> i;
    for(int i = 0; i < m; i++){
        int u, v;
        cin >> u >> v;
        swaps.emplace(u, v);
    }
    set<int> toSwap;
    toSwap.insert(p.bacK());
    int res = 0;
    for(int i = n-2; i >= 0; i--){
        if(swappable(p[i], toSwap)) ++res;
        else toSwap.insert(p[i]);
    }
    cout << res << endl;
}


#include<bits/stdc++.h>

using namespace std;

vector<int> p;
vector<set<int> > swaps;

int main(){
    srand(time(0));
    int n, m;
    cin >> n >> m;
    p.resize(n);
    swaps.resize(n+1);
    for(int& i: p) cin >> i;
    for(int i = 0; i < m; i++){
        int u, v;
        cin >> u >> v;
        swaps[v].insert(u);
    }
    set<int> swappables;
    for(int i: swaps[p.back()]) swappables.insert(i);
    int res = 0;
    for(int i = n-2; i >= 0; i--){
        if(swappables.find(p[i]) != swappables.end()) ++res;
        else{
            for(auto it = swappables.begin(); it != swappables.end(); ){
                if(swaps[p[i]].find(*it) != swaps[i].end()) ++it;
                else it = swappables.erase(it);
            }
        }
    }
    cout << res << endl;
}


#include<bits/stdc++.h>

using namespace std;

vector<int> p;
vector<set<int> > swaps;

int main(){
    int n, m;
    cin >> n >> m;
    p.resize(n);
    swaps.resize(n+1);
    for(int& i: p) cin >> i;
    for(int i = 0; i < m; i++){
        int u, v;
        cin >> u >> v;
        swaps[v].insert(u);
    }
    set<int> swappables;
    for(int i: swaps[p.back()]) swappables.insert(i);
    int res = 0;
    for(int i = n-2; i >= 0 && !swappables.empty(); i--){
        if(swappables.find(p[i]) != swappables.end()) ++res;
        else{
            for(auto it = swappables.begin(); it != swappables.end(); ){
                if(swaps[p[i]].find(*it) != swaps[p[i]].end()) ++it;
                else it = swappables.erase(it);
            }
        }
    }
    cout << res << endl;
}


#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ld INF = (ld)10000000000000;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
map<P, bool> mp;
int p[300001];
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	rep(i, n) {
		cin >> p[i];
	}
	rep(i, m) {
		int u, v; cin >> u >> v;
		mp[{u, v}] = true;
	}
	int k = p[n-1];
	int ans = 0;
	per(i, n-1) {
		if (mp[{p[i], k}]) {
			ans++;
		}
		else {
			k = p[i];
		}
	}
	cout << ans << endl;
	//stop
	return 0;
}


#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ld INF = (ld)10000000000000;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
map<P, bool> mp;
int p[300001];
int invp[300001];
vector<int> loc[300001];
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	rep(i, n) {
		cin >> p[i]; invp[p[i]] = i;
	}
	rep(i, m) {
		int u, v; cin >> u >> v;
		loc[invp[u]].push_back(invp[v]);
		mp[{u, v}] = true;
	}
	int k = p[n-1];
	int ans = 0;
	per(i, n-1) {
		if (mp[{p[i], k}]) {
			ans++;
		}
		else {
			bool exi = false;
			rep(j, i) {
				if (!mp[{p[j], k}])continue;
				sort(loc[j].begin(), loc[j].end());
				int sta = lower_bound(loc[j].begin(), loc[j].end(), j + 1) - loc[j].begin();
				bool valid = true;
				Rep1(k, j + 1, i) {
					if (sta == loc[j].size() || loc[j][sta] != k) {
						valid = false; break;
					}
					else {
						sta++;
					}
				}
				if (valid)exi = true;
			}
			if(exi)ans++;
			break;
		}
	}
	cout << ans << endl;
	//stop
	return 0;
}


#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ld INF = (ld)10000000000000;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
map<P, bool> mp;
int p[300001];
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	rep(i, n) {
		cin >> p[i];
	}
	rep(i, m) {
		int u, v; cin >> u >> v;
		mp[{u, v}] = true;
	}
	int k = p[n-1];
	int ans = 0;
	per(i, n-1) {
		if (mp[{p[i], k}]) {
			ans++;
		}
		else {
			int id = mod;
			per(j, i) {
				if (mp[{p[j], k}]) {
					id = j; break;
				}
			}
			if (id == mod)break;
			int k2 = p[id];
			bool f = true;
			Rep(j, id+1, i) {
				if (!mp[{k2, p[j]}]) {
					f = false; break;
				}
			}
			if(f)ans++;
			break;
		}
	}
	cout << ans << endl;
	//stop
	return 0;
}


#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ld INF = (ld)10000000000000;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
map<P, bool> mp;
int p[300001];
int invp[300001];
vector<int> loc[300001];
bool cansk[300001];
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	rep(i, n) {
		cin >> p[i]; invp[p[i]] = i;
	}
	rep(i, m) {
		int u, v; cin >> u >> v;
		loc[invp[u]].push_back(invp[v]);
		mp[{u, v}] = true;
	}
	int k = p[n - 1];
	int ans = 0;
	per(i, n - 1) {
		if (mp[{p[i], k}]) {
			ans++;
		}
		else {
			bool exi = false;
			per(j, i) {
				if (!mp[{p[j], k}])continue;
				sort(loc[j].begin(), loc[j].end());
				int sta = lower_bound(loc[j].begin(), loc[j].end(), j + 1) - loc[j].begin();
				bool valid = true;
				Rep1(k, j + 1, i) {
					if (cansk[k])continue;
					while (sta < loc[j].size() && loc[j][sta] < k)sta++;
					if (sta == loc[j].size() || loc[j][sta] != k) {
						valid = false; break;
					}
				}
				if (valid) {
					ans++; cansk[j] = true;
				}
			}
			break;
		}
	}
	cout << ans << endl;
	//stop
	return 0;
}


#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ld INF = (ld)10000000000000;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
map<P, bool> mp;
int p[300001];
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	rep(i, n) {
		cin >> p[i];
	}
	rep(i, m) {
		int u, v; cin >> u >> v;
		mp[{u, v}] = true;
	}
	int k = p[n-1];
	int ans = 0;
	per(i, n-1) {
		if (mp[{p[i], k}]) {
			ans++;
		}
		else {
			int id = mod;
			per(j, i) {
				if (mp[{p[j], k}]) {
					id = j; break;
				}
			}
			if (id == mod)break;
			int k2 = p[id];
			Rep(j, id+1, i) {
				if (!mp[{k2, p[j]}])break;
			}
			ans++; break;
			break;
		}
	}
	cout << ans << endl;
	//stop
	return 0;
}


#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ld INF = (ld)10000000000000;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
map<P, bool> mp;
int p[300001];
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	rep(i, n) {
		cin >> p[i];
	}
	rep(i, m) {
		int u, v; cin >> u >> v;
		mp[{u, v}] = true;
	}
	int k = p[n-1];
	int ans = 0;
	per(i, n-1) {
		if (mp[{p[i], k}]) {
			ans++;
		}
		else {
			int id = mod;
			per(j, i) {
				if (mp[{p[j], k}]) {
					id = j; break;
				}
			}
			if (id == mod)break;
			int k2 = p[id];
			bool f = true;
			Rep1(j, id+1, i) {
				if (!mp[{k2, p[j]}]) {
					f = false; break;
				}
			}
			if(f)ans++;
			break;
		}
	}
	cout << ans << endl;
	//stop
	return 0;
}


#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ld INF = (ld)10000000000000;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
map<P, bool> mp;
int p[300001];
int invp[300001];
vector<int> loc[300001];
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	rep(i, n) {
		cin >> p[i]; invp[p[i]] = i;
	}
	rep(i, m) {
		int u, v; cin >> u >> v;
		loc[invp[u]].push_back(invp[v]);
		mp[{u, v}] = true;
	}
	int k = p[n - 1];
	int ans = 0;
	per(i, n - 1) {
		if (mp[{p[i], k}]) {
			ans++;
		}
		else {
			bool exi = false;
			rep(j, i) {
				if (!mp[{p[j], k}])continue;
				sort(loc[j].begin(), loc[j].end());
				int sta = lower_bound(loc[j].begin(), loc[j].end(), j + 1) - loc[j].begin();
				bool valid = true;
				Rep1(k, j + 1, i) {
					if (sta == loc[j].size() || loc[j][sta] != k) {
						valid = false; break;
					}
				}
				if (valid)ans++;
			}
			break;
		}
	}
	cout << ans << endl;
	//stop
	return 0;
}


#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ld INF = (ld)10000000000000;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
map<P, bool> mp;
int p[300001];
int invp[300001];
vector<int> loc[300001];
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	rep(i, n) {
		cin >> p[i]; invp[p[i]] = i;
	}
	rep(i, m) {
		int u, v; cin >> u >> v;
		loc[invp[u]].push_back(invp[v]);
		mp[{u, v}] = true;
	}
	int k = p[n-1];
	int ans = 0;
	per(i, n-1) {
		if (mp[{p[i], k}]) {
			ans++;
		}
		else {
			bool exi = false;
			rep(j, i) {
				if (!mp[{p[j], k}])continue;
				sort(loc[j].begin(), loc[j].end());
				int sta = lower_bound(loc[j].begin(), loc[j].end(), j + 1) - loc[j].begin();
				bool valid = true;
				Rep1(k, j + 1, i) {
					if (sta == loc[j].size() || loc[j][sta] != k) {
						valid = false; break;
					}
				}
				if (valid)exi = true;
			}
			if(exi)ans++;
			break;
		}
	}
	cout << ans << endl;
	//stop
	return 0;
}


#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<algorithm>
#include<ctime>
#include<cstdarg>
#include<assert.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
using namespace std;
int n,k,a,b,ans,x;
vector<int>v;
map<pair<int,int>,int>flag;
pair<int,int>p;
int main()
{
    cin>>n>>k;
    for (int i=0; i<n; ++i)
    {
        cin>>x;
        v.push_back(x);
    }
    for (int i=0; i<k; ++i)
    {
        cin>>a>>b;

        p.first=a;
        p.second=b;
        flag[p]=1;

        p.first=b;
        p.second=a;
        flag[p]=1;
    }

    int i=n-1,j;
    while (i>=0&&j>=0)
    {
        //for (int j=0;j<v.size();++j)
        //    cout<<v[j]<<" ";
        //cout<<endl;
        for (j=i-1; j>=0; --j)
        {
            p.first=v[j];
            p.second=v[v.size()-1];
            if (flag[p])
            {
                bool f=true;
                for (int k=j+1; k<v.size()-1; ++k)
                {
                    p.second=v[k];
                    if (!flag[p])
                    {
                        f=false;
                        break;
                    }
                }
                if (f)
                {
                    v.erase(v.begin()+j);
                    i=j-1;
                }
                else i--;
            }
        }
        p.first=v[i-1];
        p.second=v[i];
    }
    cout<<n-v.size();
}


#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<algorithm>
#include<ctime>
#include<cstdarg>
#include<assert.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
using namespace std;
int n,k,a,b,sum[300005],ans,x;
vector<int>f;
vector<int>v[300005];
int main()
{
    cin>>n>>k;
    for (int i=0; i<n; ++i)
    {
        cin>>x;
        f.push_back(x);
    }
    for (int i=0; i<k; ++i)
    {
        cin>>a>>b;
        v[a].push_back(b);
    }
    for (int i=1; i<=n; ++i)
        sort(v[i].begin(),v[i].end());

    for (int i=0; i<n; ++i)
        sum[i]=v[f[i]].size();

    int i=n-2;
    while (i>=0)
    {

        if (f.size()-1-i<=sum[i])
        {
            for (int j=i-1; j>=0; --j)
                if (binary_search(v[f[j]].begin(),v[f[j]].end(),f[i]))
                    sum[j]--;
            f.erase(f.begin()+i);
        }
        i--;
    }
    cout<<n-f.size();
}


#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<algorithm>
#include<ctime>
#include<cstdarg>
#include<assert.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
using namespace std;
int n,k,a,b,ans,x;
vector<int>v;
map<pair<int,int>,int>flag;
pair<int,int>p;
int main()
{
    cin>>n>>k;
    for (int i=0; i<n; ++i)
    {
        cin>>x;
        v.push_back(x);
    }
    for (int i=0; i<k; ++i)
    {
        cin>>a>>b;

        p.first=a;
        p.second=b;
        flag[p]=1;

        p.first=b;
        p.second=a;
        flag[p]=1;
    }

    int i=n-1,j;
    while (i>=0&&j>=0)
    {
        //for (int j=0;j<v.size();++j)
        //    cout<<v[j]<<" ";
        //cout<<endl;
        for (j=i-1; j>=0; --j)
        {
            p.first=v[j];
            p.second=v.back();
            if (flag[p])
            {
                bool f=true;
                for (int k=j+1; k<v.size()-1; ++k)
                {
                    p.second=v[k];
                    if (!flag[p])
                    {
                        f=false;
                        break;
                    }
                }
                if (f)
                {
                    v.erase(v.begin()+j);
                    i=j-1;
                }
                else i--;
            }
        }
        p.first=v[i-1];
        p.second=v[i];
    }
    cout<<n-v.size();
}


#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<algorithm>
#include<ctime>
#include<cstdarg>
#include<assert.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
using namespace std;
int n,k,f[300005],sum[300005],a,b,ans;
vector<int>v[300005];
map<int,int>m;
map<pair<int,int>,int>flag;
pair<int,int>p;
int main()
{
    cin>>n>>k;
    for (int i=0; i<n; ++i)
    {
        cin>>f[i];
        m[f[i]]=i;
    }
    for (int i=0; i<k; ++i)
    {
        cin>>a>>b;
        if (m[a]<m[b])
        {
            p.first=a;
            p.second=b;
            if (!flag[p])
            {
                v[a].push_back(b);
                p.first=a;
                p.second=b;
                flag[p]=1;
            }
        }
        else
        {
            p.first=b;
            p.second=a;
            if (!flag[p])
            {
                v[b].push_back(a);
                p.first=b;
                p.second=a;
                flag[p]=1;
            }
        }
    }
    for (int i=0; i<n; ++i)
    {
        sum[f[i]]=v[f[i]].size();
        //cout<<sum[f[i]];
    }

    for (int i=n-2; i>=0; --i)
    {
        //for (int k=0; k<n; ++k)
        //    cout<<sum[f[k]]<<" ";
        //cout<<endl;

        if (n-1-i<=sum[f[i]])
        {
            ans++;
        for (int j=i-1; j>=0; --j)
        {
            p.first=f[j];
            p.second=f[i];
            if (!flag[p])
                sum[f[j]]++;
        }
        }
        //else break;
    }
    cout<<ans;
}


#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<algorithm>
#include<ctime>
#include<cstdarg>
#include<assert.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
using namespace std;
int n,k,a,b,ans,x;
vector<int>v;
map<pair<int,int>,int>flag;
pair<int,int>p;
int main()
{
    cin>>n>>k;
    for (int i=0; i<n; ++i)
    {
        cin>>x;
        v.push_back(x);
    }
    for (int i=0; i<k; ++i)
    {
        cin>>a>>b;

        p.first=a;
        p.second=b;
        flag[p]=1;

        p.first=b;
        p.second=a;
        flag[p]=1;
    }

    x=n-2;
    for (int i=n-1; x>=0; --i)
    {
        //for (int j=0;j<v.size();++j)
        //    cout<<v[j]<<" ";
        //cout<<endl;
        for (int j=x; j>=0; --j)
        {
            p.first=v[j];
            p.second=v[i];
            if (flag[p])
            {
                bool f=true;
                for (int k=j+1; k<i; ++k)
                {
                    p.second=v[k];
                    if (!flag[p])
                    {
                        f=false;
                        break;
                    }
                }
                if (f)
                {
                    v.erase(v.begin()+j);
                    x=j-1;
                }
            }
        }
        p.first=v[i-1];
        p.second=v[i];
    }
    cout<<n-v.size();
}


#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<algorithm>
#include<ctime>
#include<cstdarg>
#include<assert.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
using namespace std;
int n,k,a,b,ans,x;
vector<int>v;
map<pair<int,int>,int>flag;
pair<int,int>p;
int main()
{
    cin>>n>>k;
    for (int i=0; i<n; ++i)
    {
        cin>>x;
        v.push_back(x);
    }
    for (int i=0; i<k; ++i)
    {
        cin>>a>>b;

        p.first=a;
        p.second=b;
        flag[p]=1;

        p.first=b;
        p.second=a;
        flag[p]=1;
    }

    x=n-2;
    for (int i=n-1; i>=0; --i)
    {
        //for (int j=0;j<v.size();++j)
        //    cout<<v[j]<<" ";
        //cout<<endl;
        for (int j=x; j>=0; --j)
        {
            p.first=v[j];
            p.second=v[i];
            if (flag[p])
            {
                bool f=true;
                for (int k=j+1; k<i; ++k)
                {
                    p.second=v[k];
                    if (!flag[p])
                    {
                        f=false;
                        break;
                    }
                }
                if (f)
                {
                    v.erase(v.begin()+j);
                    x=j-1;
                }
            }
        }
        p.first=v[i-1];
        p.second=v[i];
    }
    cout<<n-v.size();
}


#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<algorithm>
#include<ctime>
#include<cstdarg>
#include<assert.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
using namespace std;
int n,k,a,b,ans,x;
vector<int>v;
map<pair<int,int>,int>flag;
pair<int,int>p;
int main()
{
    cin>>n>>k;
    for (int i=0; i<n; ++i)
    {
        cin>>x;
        v.push_back(x);
    }
    for (int i=0; i<k; ++i)
    {
        cin>>a>>b;

        p.first=a;
        p.second=b;
        flag[p]=1;

        p.first=b;
        p.second=a;
        flag[p]=1;
    }

    int i=n-2;
    while (i>=0)
    {
        //for (int j=0; j<v.size(); ++j)
        //    cout<<v[j]<<" ";
        //cout<<endl;

        p.first=v[i];
        p.second=v.back();
        if (flag[p])
        {
            bool f=true;
            for (int j=i+1; j<v.size()-1; ++j)
            {
                p.second=v[j];
                if (!flag[p])
                {
                    f=false;
                    break;
                }
            }
            if (f)v.erase(v.begin()+i);
        }
        i--;
    }
    cout<<n-v.size();
}


#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<algorithm>
#include<ctime>
#include<cstdarg>
#include<assert.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
using namespace std;
int n,k,a,b,ans,x;
vector<int>v;
map<pair<int,int>,int>flag;
pair<int,int>p;
int main()
{
    cin>>n>>k;
    for (int i=0; i<n; ++i)
    {
        cin>>x;
        v.push_back(x);
    }
    for (int i=0; i<k; ++i)
    {
        cin>>a>>b;

        p.first=a;
        p.second=b;
        flag[p]=1;

        p.first=b;
        p.second=a;
        flag[p]=1;
    }

    int i=n-1,j;
    while (i>=0&&j>=0)
    {
        for (int j=0;j<v.size();++j)
            cout<<v[j]<<" ";
        cout<<endl;
        for (j=i-1; j>=0; --j)
        {
            p.first=v[j];
            p.second=v[v.size()-1];
            if (flag[p])
            {
                bool f=true;
                for (int k=j+1; k<v.size()-1; ++k)
                {
                    p.second=v[k];
                    if (!flag[p])
                    {
                        f=false;
                        break;
                    }
                }
                if (f)
                {
                    v.erase(v.begin()+j);
                    i=j-1;
                }
            }
        }
        p.first=v[i-1];
        p.second=v[i];
    }
    cout<<n-v.size();
}


#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<algorithm>
#include<ctime>
#include<cstdarg>
#include<assert.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
using namespace std;
int n,k,f[300005],sum[300005],a,b,ans;
vector<int>v[300005];
map<int,int>m;
map<pair<int,int>,int>flag;
pair<int,int>p;
int main()
{
    cin>>n>>k;
    for (int i=0; i<n; ++i)
    {
        cin>>f[i];
        m[f[i]]=i;
    }
    for (int i=0; i<k; ++i)
    {
        cin>>a>>b;
        if (m[a]<m[b])
        {
            v[a].push_back(b);
            p.first=a;
            p.second=b;
            flag[p]=1;
        }
        else
        {
            v[b].push_back(a);
            p.first=b;
            p.second=a;
            flag[p]=1;
        }
    }
    for (int i=0; i<n; ++i)
    {
        sum[f[i]]=v[f[i]].size();
        //cout<<sum[f[i]];
    }

    for (int i=n-2; i>=0; --i)
    {
        //for (int k=0; k<n; ++k)
        //    cout<<sum[f[k]]<<" ";
        //cout<<endl;

        if (n-1-i==sum[f[i]])
        {
            ans++;
        }
        for (int j=i-1; j>=0; --j)
        {
            p.first=f[j];
            p.second=f[i];
            if (!flag[p])
                sum[f[j]]++;
        }
        //else break;
    }
    cout<<ans;
}


#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<algorithm>
#include<ctime>
#include<cstdarg>
#include<assert.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
using namespace std;
int n,k,a,b,ans,x;
vector<int>v;
vector<int>f[500005];
int main()
{
    cin>>n>>k;
    for (int i=0; i<n; ++i)
    {
        cin>>x;
        v.push_back(x);
    }
    for (int i=0; i<k; ++i)
    {
        cin>>a>>b;
        f[a].push_back(b);
    }
    for (int i=1; i<=n; ++i)
        sort(f[i].begin(),f[i].end());

    int i=n-2;
    while (i>=0)
    {
        //for (int j=0; j<v.size(); ++j)
        //    cout<<v[j]<<" ";
        //cout<<endl;

        if (binary_search(f[v[i]].begin(),f[v[i]].end(),v.back()))
        {
            bool flag=true;
            for (int j=i+1; j<v.size()-1; ++j)
            {
                if (!binary_search(f[v[i]].begin(),f[v[i]].end(),v[j]))
                {
                    flag=false;
                    break;
                }
            }
            if (flag)v.erase(v.begin()+i);
        }
        i--;
    }
    cout<<n-v.size();
}


#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<algorithm>
#include<ctime>
#include<cstdarg>
#include<assert.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
using namespace std;
int n,k,f[300005],sum[300005],a,b,ans;
vector<int>v[300005];
map<int,int>m;
map<pair<int,int>,int>flag;
pair<int,int>p;
int main()
{
    cin>>n>>k;
    for (int i=0; i<n; ++i)
    {
        cin>>f[i];
        m[f[i]]=i;
    }
    for (int i=0; i<k; ++i)
    {
        cin>>a>>b;
        if (m[a]<m[b])
        {
            p.first=a;
            p.second=b;
            if (!flag[p])
            {
                v[a].push_back(b);
                p.first=a;
                p.second=b;
                flag[p]=1;
            }
        }
        else
        {
            p.first=a;
            p.second=b;
            if (!flag[p])
            {
                v[b].push_back(a);
                p.first=b;
                p.second=a;
                flag[p]=1;
            }
        }
    }
    for (int i=0; i<n; ++i)
    {
        sum[f[i]]=v[f[i]].size();
        //cout<<sum[f[i]];
    }

    for (int i=n-2; i>=0; --i)
    {
        //for (int k=0; k<n; ++k)
        //    cout<<sum[f[k]]<<" ";
        //cout<<endl;

        if (n-1-i<=sum[f[i]])
        {
            ans++;
        }
        for (int j=i-1; j>=0; --j)
        {
            p.first=f[j];
            p.second=f[i];
            if (!flag[p])
                sum[f[j]]++;
        }
        //else break;
    }
    cout<<ans;
}


#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<algorithm>
#include<ctime>
#include<cstdarg>
#include<assert.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
using namespace std;
int n,k,a,b,ans,x;
vector<int>v;
map<pair<int,int>,int>flag;
pair<int,int>p;
int main()
{
    cin>>n>>k;
    for (int i=0; i<n; ++i)
    {
        cin>>x;
        v.push_back(x);
    }
    for (int i=0; i<k; ++i)
    {
        cin>>a>>b;

        p.first=a;
        p.second=b;
        flag[p]=1;

        p.first=b;
        p.second=a;
        flag[p]=1;
    }

    int i=n-1,j;
    while (i>=0&&j>=0)
    {
        for (int j=0;j<v.size();++j)
            cout<<v[j]<<" ";
        cout<<endl;

        for (j=i-1; j>=0; --j)
        {
            p.first=v[j];
            p.second=v.back();
            if (flag[p])
            {
                bool f=true;
                for (int k=j+1; k<v.size()-1; ++k)
                {
                    p.second=v[k];
                    if (!flag[p])
                    {
                        f=false;
                        break;
                    }
                }
                if (f)
                {
                    v.erase(v.begin()+j);
                    i=j-1;
                }
                else i--;
            }
        }
    }
    cout<<n-v.size();
}


#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<algorithm>
#include<ctime>
#include<cstdarg>
#include<assert.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
using namespace std;
int n,k,a,b,ans,x;
vector<int>v;
map<pair<int,int>,int>flag;
pair<int,int>p;
int main()
{
    cin>>n>>k;
    for (int i=0; i<n; ++i)
    {
        cin>>x;
        v.push_back(x);
    }
    for (int i=0; i<k; ++i)
    {
        cin>>a>>b;

        p.first=a;
        p.second=b;
        flag[p]=1;

        p.first=b;
        p.second=a;
        flag[p]=1;
    }

    int i=n-1,j;
    while (i>=0&&j>=0)
    {
        //for (int j=0;j<v.size();++j)
        //    cout<<v[j]<<" ";
        //cout<<endl;

        for (j=i-1; j>=0; --j)
        {
            p.first=v[j];
            p.second=v.back();
            if (flag[p])
            {
                bool f=true;
                for (int k=j+1; k<v.size()-1; ++k)
                {
                    p.second=v[k];
                    if (!flag[p])
                    {
                        f=false;
                        break;
                    }
                }
                if (f)
                {
                    v.erase(v.begin()+j);
                    i=j-1;
                }
                else i--;
            }
        }
    }
    cout<<n-v.size();
}


#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<algorithm>
#include<ctime>
#include<cstdarg>
#include<assert.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
using namespace std;
int n,k,a,b,ans,x;
vector<int>v;
map<pair<int,int>,int>flag;
pair<int,int>p;
int main()
{
    cin>>n>>k;
    for (int i=0; i<n; ++i)
    {
        cin>>x;
        v.push_back(x);
    }
    for (int i=0; i<k; ++i)
    {
        cin>>a>>b;

        p.first=a;
        p.second=b;
        flag[p]=1;

        p.first=b;
        p.second=a;
        flag[p]=1;
    }

    int i=n-1,j;
    while (i>=0&&j>=0)
    {
        //for (int j=0;j<v.size();++j)
        //    cout<<v[j]<<" ";
        //cout<<endl;
        for (j=i-1; j>=0; --j)
        {
            p.first=v[j];
            p.second=v[v.size()-1];
            if (flag[p])
            {
                bool f=true;
                for (int k=j+1; k<v.size()-1; ++k)
                {
                    p.second=v[k];
                    if (!flag[p])
                    {
                        f=false;
                        break;
                    }
                }
                if (f)
                {
                    v.erase(v.begin()+j);
                    i=j-1;
                }
            }
        }
        p.first=v[i-1];
        p.second=v[i];
    }
    cout<<n-v.size();
}