import sys
input = sys.stdin.readline
n,m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = {}
res = 0
for i in range(m):
    c,d = list(map(int, input().split()))
    if d in b:
        b[d].append(c)
    else:
        b[d] = [c]
# a3 = deepcopy(a)
# a = deepcopy(a3)
t = a[-1]
from copy import deepcopy
def move(index,x,a2):
    if index < 0 or index > len(a2)-1:
        return False
    if a2[index] == x:
        a2[index],a2[index+1] = a2[index+1],a2[index]
        return True
    if a2[index] in b:
        for y in b[a2[index]]:
            if move(index-1,y,a2):
                return True
for i in range(n-2,-1,-1):
    flag = True
    if a[i+1] in b:
        for y in b[a[i+1]]:
            a2 = deepcopy(a)
            if move(i,y, a2):
                a = deepcopy(a2)
                #a[i+1],a[i] = a[i],a[i+1]
                flag = False
                break
        if not flag:
            continue
    
            
    if flag:
        break
print(n-a.index(t)-1)