"""
NTC here
"""
from sys import setcheckinterval,stdin,setrecursionlimit
setcheckinterval(1000)
setrecursionlimit(10**6)

iin=lambda :int(stdin.readline())
lin=lambda :list(map(int,stdin.readline().split()))


class Disjoint_set:
    class node:
        def __init__(self,a):
            self.value=a
            self.p=self
            self.rank=0
            self.ans=[a]
    def __init__(self,a):
        self.data={i:self.make_set(i) for i in range(1,a+1)}

    def make_set(self,val):
        return self.node(val)
    
    def union(self,x,y):
        self.link(self.find_set(self.data[x]),self.find_set(self.data[y]))
    
    def link(self,val1,val2):
        if val1.rank>val2.rank:
            val2.p=val1
            val1.ans.extend(val2.ans)
            val1.rank+=1
        else:
            val1.p=val2
            val2.ans.extend(val1.ans)
            val2.rank+=1
    def find_set(self,val):
        if val.p.value!=val.value:
            val.p=self.find_set(val.p)
        return val.p
    def get_sol(self,val,n):
        mx=n
        for i in self.data:
            if self.find_set(self.data[i])==val:
                #print('inner',mx,i)
                mx=min(mx,pos[i])
        return mx

n,m=lin()
a1=lin()
queue=set()
for _ in range(m):
    i,j=lin()
    if (i,j) not in queue and (j,i) not in queue:queue.add((i,j))
pos={j:i for i,j in enumerate(a1)}
da=Disjoint_set(n)
for i,j in queue:
    da.union(i,j)
ans=da.find_set(da.data[a1[-1]])
sol=da.get_sol(ans,n)
#print('i',sol,ans,pos)
print(n-sol-1)