n,m = map(int,input().split())
a = list(map(int,input().split()))
b = []
c = []
d = []
for i in range(m):
    q = list(map(int,input().split()))
    b.append(q[0])
    c.append(q[1])
    d.append(q)
i = 0
z = a[len(a) - 1]
while i < len(a) - 1:
    qq = [a[i],a[i + 1]]
    if qq in d:
        a[i],a[i + 1] = a[i + 1],a[i]
    i = i + 1
for i in range(len(a) - 1):
    if a[i + 1] == 2:
        qq = [a[i],a[i + 1]]
        if qq in d:
            a[i],a[i + 1] = a[i + 1],a[i]
print(len(a) - a.index(z) - 1)