At the big break Nastya came to the school dining room. There are $n$ pupils in the school, numbered from $1$ to $n$. Unfortunately, Nastya came pretty late, so that all pupils had already stood in the queue, i.e. Nastya took the last place in the queue. Of course, it's a little bit sad for Nastya, but she is not going to despond because some pupils in the queue can agree to change places with some other pupils.

Formally, there are some pairs $u$, $v$ such that if the pupil with number $u$ stands directly in front of the pupil with number $v$, Nastya can ask them and they will change places. 

Nastya asks you to find the maximal number of places in queue she can move forward. 


-----Input-----

The first line contains two integers $n$ and $m$ ($1 \leq n \leq 3 \cdot 10^{5}$, $0 \leq m \leq 5 \cdot 10^{5}$) — the number of pupils in the queue and number of pairs of pupils such that the first one agrees to change places with the second one if the first is directly in front of the second.

The second line contains $n$ integers $p_1$, $p_2$, ..., $p_n$ — the initial arrangement of pupils in the queue, from the queue start to its end ($1 \leq p_i \leq n$, $p$ is a permutation of integers from $1$ to $n$). In other words, $p_i$ is the number of the pupil who stands on the $i$-th position in the queue.

The $i$-th of the following $m$ lines contains two integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), denoting that the pupil with number $u_i$ agrees to change places with the pupil with number $v_i$ if $u_i$ is directly in front of $v_i$. It is guaranteed that if $i \neq j$, than $v_i \neq v_j$ or $u_i \neq u_j$. Note that it is possible that in some pairs both pupils agree to change places with each other.

Nastya is the last person in the queue, i.e. the pupil with number $p_n$.


-----Output-----

Print a single integer — the number of places in queue she can move forward.


-----Examples-----
Input
2 1
1 2
1 2

Output
1
Input
3 3
3 1 2
1 2
3 1
3 2

Output
2
Input
5 2
3 1 5 4 2
5 2
5 4

Output
1


-----Note-----

In the first example Nastya can just change places with the first pupil in the queue.

Optimal sequence of changes in the second example is   change places for pupils with numbers $1$ and $3$.  change places for pupils with numbers $3$ and $2$.  change places for pupils with numbers $1$ and $2$. 

The queue looks like $[3, 1, 2]$, then $[1, 3, 2]$, then $[1, 2, 3]$, and finally $[2, 1, 3]$ after these operations.

Difficulty: interview

Starter code:

Solutions:
n, m = list(map(int, input().split()))
line = list(map(int, input().split()))

pairs = set()
for i in range(m):
    a, b = list(map(int, input().split()))
    pairs.add((a,b))

req = [line.pop()]

out = 0
while line != []:
    nex = line.pop()
    works = True
    for pers in req:
        if not (nex, pers) in pairs:
            works = False
            break
    if works:
        out += 1
    else:
        req.append(nex)

print(out)
        


import sys
input = sys.stdin.readline

n,m=list(map(int,input().split()))
P=list(map(int,input().split()))
PAIR=[list(map(int,input().split())) for i in range(m)]

NAT=P[-1]

LIST=[[] for i in range(n+1)]

for x,y in PAIR:
    LIST[x].append(y)

for i in range(n+1):
    LIST[i]=set(LIST[i])

FLIST=[NAT]
i=n-2
ANS=0
while i>=0:

    for j in FLIST:
        if j in LIST[P[i]]:
            continue
        else:
            FLIST.append(P[i])
            break

    else:
        ANS+=1

    i-=1
        
print(ANS)
        
    
    


n, m = list(map(int, input().split()))
p = tuple(map(int, input().split()))
r = [{i} for i in range(n + 1)]
for _ in range(m):
    u, v = list(map(int, input().split()))
    r[u].add(v)
first = True
nr = set()
res = 0
for i in p[::-1]:
    if first:
        cur = 1
        first = False
    else:
        cur = len(nr)
        for ri in r[i]:
            if ri in nr:
                cur -= 1
    if cur:
        nr.add(i)
    else:
        res += 1
print(res)


n, m = list(map(int, input().split()))
p = list(map(int, input().split()))

allp = set()
for i in range(m):
	u, v = list(map(int, input().split()))
	allp.add((u ,v))

currentList = [p.pop()]
suma = 0

while len(p) > 0:
	nextc = p.pop()
	check = True
	for i in currentList:
		if not ((nextc, i) in allp):
			check = False
			break
	
	if check:
		suma += 1
	else:
		currentList.append(nextc)

print(suma)	



# -*- coding: utf-8 -*-
# @Date    : 2019-03-12 15:49:02
# @Author  : raj lath (oorja.halt@gmail.com)
# @Link    : link
# @Version : 1.0.0

import sys
sys.setrecursionlimit(10**5+1)

inf     =  int(10 ** 20)
max_val =  inf
min_val = -inf

RW  = lambda : sys.stdin.readline().strip()
RI  = lambda : int(RW())
RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda : [x for x in sys.stdin.readline().strip().split()]


N, M = RMI()
P = RMI()
G = [[] for x in range(N+6)]
C = [0 for x in range(N+6)]
ans = 0
for _ in range(M):
    x,  y = RMI()
    G[y].append(x)
for i in range(N-1, -1, -1):
    if C[P[i]] == N - 1 - i - ans and i != N - 1:
        ans += 1
    else:
        for F in G[P[i]]:
            C[F] += 1
print(ans)




n, m = [int(x) for x in input().split()]
q = [int(x) for x in input().split()]
helpers = [[] for i in range(n + 5)]
helping = [0] * 300005
ans, removed = 0, 0
for i in range(m):
    u, v = [int(c) for c in input().split()]
    helpers[v].append(u)

for i in helpers[q[-1]]:
    helping[i] += 1

for i in range(n - 2, -1, -1):
    remaining = n - 1 - i - removed
    if(helping[q[i]] == remaining):
        removed += 1
        ans += 1
    else:
        for k in helpers[q[i]]:
            helping[k] += 1
        
print(ans)

    


# -*- coding: utf-8 -*-
# @Time    : 2019/3/13 11:57
# @Author  : LunaFire
# @Email   : gilgemesh2012@gmail.com
# @File    : D. Nastya Is Buying Lunch.py

import sys

input = sys.stdin.readline

def main():
    n, m = list(map(int, input().split()))
    p = list(map(int, input().split()))
    edge_set = set()
    for _ in range(m):
        u, v = list(map(int, input().split()))
        edge_set.add((u, v))

    ret = 0
    curr_list = [p.pop()]
    while p:
        u = p.pop()
        flag = True
        for v in curr_list:
            if (u, v) not in edge_set:
                flag = False
                break
        if flag:
            ret += 1
        else:
            curr_list.append(u)
    print(ret)


def __starting_point():
    main()

__starting_point()

n,m=map(int,input().split())
l=[int(x) for x in input().split()]
pairs=[[] for i in range(n+1)]
ans=0
N=[0 for i in range(n+1)]
for i in range(m):
    a,b=map(int,input().split())
    pairs[b].append(a)
for i in range(n-1,-1,-1):
    if N[l[i]]==n-1-i-ans and i!=n-1:
        ans+=1
    else:
        for I in pairs[l[i]]:
            N[I]+=1
print(ans)

n, m = list(map(int, input().split()))
p = list(map(int, input().split()))
gr = [set() for i in range(n)]
for i in range(m):
    a, b = list(map(int, input().split()))
    a -= 1
    b -= 1
    gr[a].add(b)

cant = {p[-1] - 1}
for i in range(n - 2, -1, -1):
    cnt = 0
    for j in gr[p[i] - 1]:
        if j in cant:
            cnt += 1
    # print(p[i], gr[p[i] - 1], cnt)
    if cnt != len(cant):
        cant.add(p[i] - 1)
# print(cant)
print(n - len(cant))


import math
from collections import deque, defaultdict
from sys import stdin, stdout
input = stdin.readline
# print = stdout.write
listin = lambda : list(map(int, input().split()))
mapin = lambda : map(int, input().split())
n, m = mapin()
a = listin()
s = set([])
for _ in range(m):
    s.add(tuple(mapin()))
z = [a.pop()]
count = 0
while a:
    last = a.pop()
    flag = True
    for i in z:
        if (last, i) not in s:
            flag = False
            break
    if flag:
        count+=1
    else:
        z.append(last)
print(count)

import math
from collections import deque, defaultdict
from sys import stdin, stdout
input = stdin.readline
# print = stdout.write
listin = lambda : list(map(int, input().split()))
mapin = lambda : map(int, input().split())
n, m = mapin()
a = listin()
s = set([])
for _ in range(m):
    s.add(tuple(mapin()))
z = [a.pop()]
count = 0
while a:
    last = a.pop()
    flag = True
    for i in z:
        if (last, i) not in s:
            flag = False
            break
    if flag:
        count+=1
    else:
        z.append(last)
print(count)

n, m = map(int, input().split())

p = list([int(s)-1 for s in input().split()])
nastya = p[-1]

good = [False] * n

swaps = [[] for _ in range(n)]
counts = [0] * n
for _ in range(m):
	u, v  = map(int, input().split())
	u-=1
	v-=1
	if v==nastya:
		good[u] = True
	else:
		swaps[v].append(u)

total = 0
bad_count = 0
for i, pupil in enumerate(p[-2::-1]):
	if good[pupil] and counts[pupil] >= bad_count:
		total += 1
	else:
		bad_count += 1
		for u in swaps[pupil]:
			counts[u] += 1
print(total)

import sys
import heapq
import math
from enum import Enum

lines = sys.stdin.read().splitlines()
n, m = list(map(int, lines[0].split(' ')))
positionToNumber = [int(x)-1 for x in lines[1].split(' ')]

numberToPosition = [0]*n
for i in range(0, len(positionToNumber)):
    numberToPosition[positionToNumber[i]] = i

# print('numberToOrder: ' + str(numberToOrder))
letsInFront = []
for i in range(0, n):
    letsInFront.append(set())
for i in range(0, m):
    a, b = list(map(int, lines[i+2].split(' ')))
    # a lets b in front
    indexA = a-1
    indexB = b-1
    letsInFront[numberToPosition[indexA]].add(numberToPosition[indexB])

# for i in range(0, n):
#     letsInFront[i].sort()
# print(bitmaps)
count = 0
let = set()
let.add(n-1)
# print(letsInFront)
for i in reversed(list(range(0, n-1))):
    found = True
    if len(let) > len(letsInFront[i]):
        found = False
    else:
        for j in let:
            if j not in letsInFront[i]:
                found = False
                break
    if not found:
        let.add(i)
    else:
        count += 1
print(count)


n,m = list(map(int, input().split()))
p = list(map(int, input().split()))
p.insert(0, 0)

vis = [0] * (n+7)
G = [[0] for i in range(n+7)]
for i in range(m):
    u, v = list(map(int, input().split()))
    G[v].append(u)

for x in G[p[n]]:
    vis[x] += 1

ans = 0
cnt = 1
for i in range(n - 1, 0, -1):
    now = p[i]
    if vis[now] == cnt:
        ans += 1
    else:
        cnt += 1
        for y in G[now]:
            vis[y] += 1

print(ans)
    


from sys import stdin
n,m=list(map(int,stdin.readline().strip().split()))
s=list(map(int,stdin.readline().strip().split()))
nas=s[-1]
g=set()
st=set()
for i in range(m):
    a,b=list(map(int,stdin.readline().strip().split()))
    if b==nas:
        g.add(a)
    else:
        st.add((a,b))
x=n-2
while x>=0:
    if s[x] in g and s[x+1] not in g and (s[x],s[x+1]) in st:
        st.remove((s[x],s[x+1]))
        aux=s[x]
        s[x]=s[x+1]
        s[x+1]=aux
        x=min(n-2,x+1)
        
    elif s[x] in g and s[x+1] in g and (s[x],s[x+1]) in st:
        st.remove((s[x],s[x+1]))   
        aux=s[x]
        s[x]=s[x+1]
        s[x+1]=aux
        x=min(n-2,x+1)
         
    else:
        x-=1
ans=0
for i in range(n-2,-1,-1):
    if s[i] in g:
        ans+=1
    else:
        break
print(ans)


n,m = [int(x) for x in input().split()]

p = [int(x)-1 for x in input().split()]

e = [[] for _ in range(n)]

for _ in range(m):
    u,v = [int(x) for x in input().split()]
    e[u-1].append(v-1)

t = [p[n-1]]

ans = 0
for i in range(n-2,-1,-1):
    u = p[i]
    if len(e[u]) >= len(t):
        f = set(e[u])
        can = True
        for x in t:
            if x not in f:
                can = False
                break
        if can : ans += 1
        else: t.append(u)
    else:
        t.append(u)


print(ans)


n, m = list(map(int, input().split()))

q = list(map(int, input().split()))

G = [set() for _ in range(n + 1)]
vn = []
for i in range(m):
  u, v = list(map(int, input().split()))
  G[u].add(v)

P = set([q[-1]])
for i in range(n - 2, -1, -1):
  p = q[i]
  if len(G[p]) < len(P) or not P.issubset(G[p]):
    P.add(p)
print(n - len(P))


def __starting_point():
    n, m = (int(x) for x in input().split())
    queue = [int(x) - 1 for x in input().split()]
    id___passables = [set() for _ in range(n)]
    for _ in range(m):
        id1, id2 = (int(x) - 1 for x in input().split())
        id___passables[id2].add(id1)
    train_passables = id___passables[queue[-1]].copy()
    ans = 0
    for person in reversed(queue[:-1]):
        if person in train_passables:
            ans += 1
        else:
            train_passables &= id___passables[person]
    print(ans)
    # swapable = [[int(x) for x in input().split()] for _ in range(m)]

__starting_point()