You are given a permutation p of length n. Also you are given m foe pairs (a_{i}, b_{i}) (1 ≤ a_{i}, b_{i} ≤ n, a_{i} ≠ b_{i}). 

Your task is to count the number of different intervals (x, y) (1 ≤ x ≤ y ≤ n) that do not contain any foe pairs. So you shouldn't count intervals (x, y) that contain at least one foe pair in it (the positions and order of the values from the foe pair are not important).

Consider some example: p = [1, 3, 2, 4] and foe pairs are {(3, 2), (4, 2)}. The interval (1, 3) is incorrect because it contains a foe pair (3, 2). The interval (1, 4) is also incorrect because it contains two foe pairs (3, 2) and (4, 2). But the interval (1, 2) is correct because it doesn't contain any foe pair.


-----Input-----

The first line contains two integers n and m (1 ≤ n, m ≤ 3·10^5) — the length of the permutation p and the number of foe pairs.

The second line contains n distinct integers p_{i} (1 ≤ p_{i} ≤ n) — the elements of the permutation p.

Each of the next m lines contains two integers (a_{i}, b_{i}) (1 ≤ a_{i}, b_{i} ≤ n, a_{i} ≠ b_{i}) — the i-th foe pair. Note a foe pair can appear multiple times in the given list.


-----Output-----

Print the only integer c — the number of different intervals (x, y) that does not contain any foe pairs.

Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.


-----Examples-----
Input
4 2
1 3 2 4
3 2
2 4

Output
5

Input
9 5
9 7 2 3 1 4 6 5 8
1 6
4 5
2 7
7 2
2 7

Output
20



-----Note-----

In the first example the intervals from the answer are (1, 1), (1, 2), (2, 2), (3, 3) and (4, 4).

Difficulty: MEDIUM_HARD
Tags: Sorting

Combinatorics

Amortized analysis
Skill types: Sorting

Amortized analysis

Starter code:

Solutions:
import sys

def FoePairs():
	(n, m) = sys.stdin.readline().split()
	n = int(n)
	m = int(m)
	s = n + 1
	p = [0] * s
	pos_p = [0] * s
	closest_pos = [0] * s
	i = 1
	line = sys.stdin.readline().split()
	while i < s:
		t = int(line[i - 1])
		p[i] = t
		pos_p[t] = i
		i += 1
	for x in range(0, m):
		(start, finish) = sys.stdin.readline().split()
		start = int(start)
		finish = int(finish)
		p_finish = pos_p[finish]
		p_start = pos_p[start]
		if p_finish > p_start:
			closest_pos[finish] = max(closest_pos[finish], p_start)
		else:
			closest_pos[start] = max(closest_pos[start], p_finish)
	i = 1
	respuesta = 0
	current_closest_pos = 0
	while i < s:
		current_closest_pos = max(current_closest_pos, closest_pos[p[i]])
		respuesta += i - current_closest_pos
		i += 1
	print(respuesta)
FoePairs()


def main():
	from sys import stdin
	(n, m) = list(map(int, input().split()))
	n += 1
	(aa, pos, duo) = ([0] * n, [0] * n, [0] * n)
	for (i, a) in enumerate(map(int, input().split()), 1):
		aa[i] = a
		pos[a] = i
	for s in stdin.read().splitlines():
		(x, y) = list(map(int, s.split()))
		(px, py) = (pos[x], pos[y])
		if px > py:
			if duo[x] < py:
				duo[x] = py
		elif duo[y] < px:
			duo[y] = px
	res = mx = 0
	for (i, a) in enumerate(aa):
		if mx < duo[a]:
			mx = duo[a]
		res += i - mx
	print(res)

def __starting_point():
	main()
__starting_point()


import sys
(n, m) = map(int, input().split())
pos = [None] * (n + 1)
for (i, a) in enumerate(map(int, input().split())):
	pos[a] = i
z = [300005] * (n + 1)
for pr in sys.stdin.read().splitlines():
	(x, y) = map(int, pr.split())
	if pos[x] > pos[y]:
		(x, y) = (y, x)
	z[pos[x]] = min(z[pos[x]], pos[y])
(lf, rg) = (n - 1, n - 1)
ans = 0
while lf >= 0:
	if z[lf] is not None:
		rg = min(rg, z[lf] - 1)
	ans += rg - lf + 1
	lf -= 1
print(ans)


from __future__ import division, print_function
import sys

def solver():
	import re
	blob = re.split('[ \n]', sys.stdin.read())
	it = map(int, blob)
	(n, m) = (next(it), next(it))
	revp = [0] * (n + 1)
	for i in range(n):
		revp[next(it)] = i
	pairs = [-1] * (n + 1)
	pairs[-1] = n - 1
	for _ in range(m):
		(a, b) = (next(it), next(it))
		(a, b) = (revp[a], revp[b])
		if b > a:
			(a, b) = (b, a)
		pairs[a] = max(pairs[a], b)
	res = 0
	pos = 0
	for (right, left) in enumerate(pairs):
		if left == -1:
			continue
		for i in range(pos, left + 1):
			res = res + right - i
		pos = max(pos, left + 1)
	return res

def main():
	res = solver()
	sys.stdout.write('{}\n'.format(res))
	return 0
main()


import sys
from bisect import bisect_right as rb
from collections import deque
from queue import PriorityQueue
from math import *
input_ = lambda : sys.stdin.readline().strip('\r\n')
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
ip = lambda : input_()
fi = lambda : float(input_())
ap = lambda ab, bc, cd: ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x: print(x)
prinT = lambda x: print(x)
f = lambda : sys.stdout.flush()
mod = 998244353
(n, m) = il()
a = il()
b = [0 for i in range(n + 10)]
c = [0 for i in range(n + 10)]
for i in range(1, n + 1):
	x = a[i - 1]
	b[x] = i
	c[x] = n + 1
for i in range(1, m + 1):
	(x, y) = il()
	x = b[x]
	y = b[y]
	if x > y:
		(x, y) = (y, x)
	c[x] = min(c[x], y)
for i in range(n - 1, 0, -1):
	c[i] = min(c[i], c[i + 1])
ans = 0
for i in range(1, n + 1):
	ans += c[i] - i
print(ans)


def solver():
	import sys
	it = (int(x) for line in sys.stdin.read().splitlines() for x in line.split(' '))
	(n, m) = (next(it), next(it))
	revp = [0] * (n + 1)
	for i in range(n):
		revp[next(it)] = i
	pairs = [-1] * (n + 1)
	pairs[-1] = n - 1
	for _ in range(m):
		(a, b) = (next(it), next(it))
		(a, b) = (revp[a], revp[b])
		if b > a:
			(a, b) = (b, a)
		pairs[a] = max(pairs[a], b)
	res = 0
	pos = 0
	for (right, left) in enumerate(pairs):
		if left == -1:
			continue
		for i in range(pos, left + 1):
			res = res + right - i
		pos = max(pos, left + 1)
	return res

def main():
	import sys
	res = solver()
	sys.stdout.write('{}\n'.format(res))
	return 0

def __starting_point():
	main()
__starting_point()


import sys
(n, m) = map(int, sys.stdin.buffer.readline().decode('utf-8').split())
a = list(map(int, sys.stdin.buffer.readline().decode('utf-8').split()))
index = [0] * (n + 1)
ng = [-1] * n
for (i, x) in enumerate(a):
	index[x] = i
for (x, y) in (map(int, line.decode('utf-8').split()) for line in sys.stdin.buffer):
	(s, t) = (min(index[x], index[y]), max(index[x], index[y]))
	ng[t] = max(ng[t], s)
left = -1
ans = 0
for i in range(n):
	left = max(left, ng[i])
	ans += i - left
print(ans)


from sys import stdin

def main():
	(n, k) = list(map(int, stdin.readline().split()))
	ar = list(map(int, stdin.readline().split()))
	lk = {ar[i] - 1: i for i in range(n)}
	pair = [-1 for _ in range(n)]
	for _ in range(k):
		(x, y) = list(map(int, stdin.readline().split()))
		if lk[y - 1] > lk[x - 1]:
			pair[y - 1] = max(pair[y - 1], lk[x - 1])
		else:
			pair[x - 1] = max(pair[x - 1], lk[y - 1])
	start = 0
	end = 0
	ok = True
	ans = 0
	while end < n:
		while end < n and ok:
			curr = ar[end] - 1
			if start <= pair[curr]:
				ok = False
			else:
				end += 1
		if end < n:
			while start <= pair[ar[end] - 1]:
				ans += end - start
				start += 1
		else:
			ans += (end - start + 1) * (end - start) // 2
		ok = True
	print(ans)

def __starting_point():
	main()
__starting_point()


from __future__ import division, print_function

def solver(ifs):
	(n, m) = list(map(int, ifs.readline().split()))
	p = list(map(int, ifs.readline().split()))
	revp = [0] * (n + 1)
	for (i, val) in enumerate(p):
		revp[val] = i
	pairs = [-1] * (n + 1)
	pairs[-1] = n - 1
	for i in range(m):
		(a, b) = list(map(int, ifs.readline().split()))
		(a, b) = (revp[a], revp[b])
		if b > a:
			(a, b) = (b, a)
		pairs[a] = max(pairs[a], b)
	res = 0
	pos = 0
	for (right, left) in enumerate(pairs):
		if left == -1:
			continue
		for i in range(pos, left + 1):
			res = res + right - i
		pos = max(pos, left + 1)
	print(res)

def main():
	import sys
	if sys.version_info.major == 3:
		from io import StringIO as StreamIO
	else:
		from io import BytesIO as StreamIO
	with StreamIO(sys.stdin.buffer.read().decode('ascii')) as ifs, StreamIO() as ofs:
		_stdout = sys.stdout
		sys.stdout = ofs
		solver(ifs)
		sys.stdout = _stdout
		sys.stdout.write(ofs.getvalue())
	return 0
main()


from sys import stdin

def main():
	(n, m) = map(int, input().split())
	(pos, left) = ([0] * n, [0] * n)
	for (i, a) in enumerate(map(int, input().split())):
		pos[a - 1] = i
	for s in stdin.read().splitlines():
		(l, r) = map(int, s.split())
		(l, r) = (pos[l - 1], pos[r - 1])
		if l > r:
			(l, r) = (r, l)
		if l >= left[r]:
			left[r] = l + 1
	ans = l = 0
	for i in range(n):
		if left[i] > l:
			l = left[i]
		ans += i - l + 1
	print(ans)
main()


import sys

def solver():
	import re
	lines = iter(sys.stdin.read().split('\n'))
	(n, m) = tuple(map(int, next(lines).split(' ')))
	revp = [0] * (n + 1)
	for (i, val) in enumerate(map(int, next(lines).split(' '))):
		revp[val] = i
	pairs = [-1] * (n + 1)
	pairs[-1] = n - 1
	for i in range(m):
		(a, b) = tuple(map(int, next(lines).split(' ')))
		(a, b) = (revp[a], revp[b])
		if b > a:
			(a, b) = (b, a)
		pairs[a] = max(pairs[a], b)
	res = 0
	pos = 0
	for (right, left) in enumerate(pairs):
		if left == -1:
			continue
		for i in range(pos, left + 1):
			res = res + right - i
		pos = max(pos, left + 1)
	return res

def main():
	res = solver()
	sys.stdout.write('{}\n'.format(res))
	return 0

def __starting_point():
	main()
__starting_point()


from __future__ import division, print_function

def solver():
	import sys
	it = (int(x) for line in sys.stdin.buffer.read().decode('ascii').splitlines() for x in line.split(' '))
	(n, m) = (next(it), next(it))
	revp = [0] * (n + 1)
	for i in range(n):
		revp[next(it)] = i
	pairs = [-1] * (n + 1)
	pairs[-1] = n - 1
	for _ in range(m):
		(a, b) = (next(it), next(it))
		(a, b) = (revp[a], revp[b])
		if b > a:
			(a, b) = (b, a)
		pairs[a] = max(pairs[a], b)
	res = 0
	pos = 0
	for (right, left) in enumerate(pairs):
		if left == -1:
			continue
		for i in range(pos, left + 1):
			res = res + right - i
		pos = max(pos, left + 1)
	return res

def main():
	import sys
	res = solver()
	sys.stdout.write('{}\n'.format(res))
	return 0
main()


def f():
	sizes = input().split(' ')
	(n, m) = (int(sizes[0]), int(sizes[1]))
	permStr = input().split(' ')
	pairsStr = [input() for i in range(m)]
	indexes = [0 for i in range(n + 1)]
	for i in range(n):
		indexes[int(permStr[i])] = i + 1
	lowerNums = [0 for i in range(n + 1)]
	for i in range(m):
		pair = pairsStr[i].split(' ')
		(a, b) = (indexes[int(pair[0])], indexes[int(pair[1])])
		if a < b:
			l = a
			h = b
		else:
			l = b
			h = a
		if l > lowerNums[h]:
			lowerNums[h] = l
	counter = 0
	left = 0
	for i in range(1, n + 1):
		candidate = lowerNums[i]
		if candidate > left:
			r = i - 1 - left
			q = i - 1 - candidate
			counter += (r * (r - 1) - q * (q - 1)) // 2
			left = candidate
	r = i - left
	counter += r * (r - 1) // 2
	print(counter + n)
f()


import sys
import bisect
from bisect import bisect_left as lb
input_ = lambda : sys.stdin.readline().strip('\r\n')
from math import log
from math import gcd
from math import atan2, acos
from random import randint
sa = lambda : input_()
sb = lambda : int(input_())
sc = lambda : input_().split()
sd = lambda : list(map(int, input_().split()))
se = lambda : float(input_())
sf = lambda : list(input_())
flsh = lambda : sys.stdout.flush()
mod = 10 ** 9 + 7
gp = []
cost = []
dp = []
mx = []
ans1 = []
ans2 = []
special = []
specnode = []
a = 0
kthpar = []

def dfs(root, par):
	if par != -1:
		dp[root] = dp[par] + 1
	for i in range(1, 20):
		if kthpar[root][i - 1] != -1:
			kthpar[root][i] = kthpar[kthpar[root][i - 1]][i - 1]
	for child in gp[root]:
		if child == par:
			continue
		kthpar[child][0] = root
		dfs(child, root)

def hnbhai():
	(n, m) = sd()
	a = sd()
	pos = [0] * (n + 3)
	left = [0] * (n + 3)
	for i in range(1, n + 1):
		pos[a[i - 1]] = i
		left[a[i - 1]] = n + 1
	for i in range(m):
		(x, y) = sd()
		x = pos[x]
		y = pos[y]
		if x > y:
			(x, y) = (y, x)
		left[x] = min(left[x], y)
	for i in range(n - 1, 0, -1):
		left[i] = min(left[i], left[i + 1])
	tot = 0
	for i in range(1, n + 1):
		tot += left[i] - i
	print(tot)
for _ in range(1):
	hnbhai()