Consider a tunnel on a one-way road. During a particular day, $n$ cars numbered from $1$ to $n$ entered and exited the tunnel exactly once. All the cars passed through the tunnel at constant speeds.

A traffic enforcement camera is mounted at the tunnel entrance. Another traffic enforcement camera is mounted at the tunnel exit. Perfectly balanced.

Thanks to the cameras, the order in which the cars entered and exited the tunnel is known. No two cars entered or exited at the same time.

Traffic regulations prohibit overtaking inside the tunnel. If car $i$ overtakes any other car $j$ inside the tunnel, car $i$ must be fined. However, each car can be fined at most once.

Formally, let's say that car $i$ definitely overtook car $j$ if car $i$ entered the tunnel later than car $j$ and exited the tunnel earlier than car $j$. Then, car $i$ must be fined if and only if it definitely overtook at least one other car.

Find the number of cars that must be fined. 


-----Input-----

The first line contains a single integer $n$ ($2 \le n \le 10^5$), denoting the number of cars.

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$), denoting the ids of cars in order of entering the tunnel. All $a_i$ are pairwise distinct.

The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$), denoting the ids of cars in order of exiting the tunnel. All $b_i$ are pairwise distinct.


-----Output-----

Output the number of cars to be fined.


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

Output
2

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

Output
6

Input
2
1 2
1 2

Output
0



-----Note-----

The first example is depicted below:

[Image]

Car $2$ definitely overtook car $5$, while car $4$ definitely overtook cars $1$, $2$, $3$ and $5$. Cars $2$ and $4$ must be fined.

In the second example car $5$ was definitely overtaken by all other cars.

In the third example no car must be fined.

Source: codeforces | Difficulty: EASY
Tags: Data structures

Sorting

Amortized analysis


Solutions:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
o = set()
i = 0
j = 0
while j < n:
	if a[i] in o:
		i += 1
	elif a[i] != b[j]:
		o.add(b[j])
		j += 1
	else:
		i += 1
		j += 1
print(len(o))


n = int(input())
ptr = 0
overtake = 0
di = dict()
enter = list(map(int, input().split()))
exit = list(map(int, input().split()))
enter.reverse()
exit.reverse()
for i in range(n):
	while exit[ptr] in di:
		ptr += 1
	if enter[i] != exit[ptr]:
		overtake += 1
		di[enter[i]] = True
	else:
		ptr += 1
print(overtake)


n = int(input())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
pos = [0] * n
pos1 = [0] * n
c = -1
s = 0
for i in range(n):
	pos[l2[i] - 1] = i + 1
for i in range(n):
	pos1[i] = pos[l1[i] - 1]
for i in range(0, n):
	if pos1[i] > c:
		c = pos1[i]
	else:
		s += 1
print(s)


import sys
n = int(sys.stdin.readline())
arr1 = [int(i) - 1 for i in sys.stdin.readline().split()]
arr2 = [int(i) - 1 for i in sys.stdin.readline().split()]
new_arr = [0 for i in range(n)]
new_arr2 = [0 for i in range(n)]
result = 0
max_element = -1
for i in range(n):
	new_arr[arr2[i]] = i
for i in range(n):
	new_arr2[i] = new_arr[arr1[i]]
for i in range(n):
	if new_arr2[i] > max_element:
		max_element = new_arr2[i]
	else:
		result += 1
print(result)


from bisect import bisect_left
from sys import stdin

def input():
	return stdin.readline()[:-1]

def intput():
	return int(input())

def sinput():
	return input().split()

def intsput():
	return list(map(int, sinput()))
n = intput()
a = intsput()[::-1]
b = intsput()[::-1]
realloc = {}
for i in range(n):
	realloc[a[i]] = i
for i in range(n):
	b[i] = realloc[b[i]]
cnt = 0
cur = -1
for i in range(n):
	if cur > b[i]:
		cnt += 1
	else:
		cur = b[i]
print(cnt)


class BIT:

	def __init__(self, n):
		self.n = n
		self.bit = [0] * (self.n + 1)

	def init(self, init_val):
		for (i, v) in enumerate(init_val):
			self.add(i, v)

	def add(self, i, x):
		i += 1
		while i <= self.n:
			self.bit[i] += x
			i += i & -i

	def sum(self, i, j):
		return self._sum(j) - self._sum(i)

	def _sum(self, i):
		res = 0
		while i > 0:
			res += self.bit[i]
			i -= i & -i
		return res

	def lower_bound(self, x):
		s = 0
		pos = 0
		depth = self.n.bit_length()
		v = 1 << depth
		for i in range(depth, -1, -1):
			k = pos + v
			if k <= self.n and s + self.bit[k] < x:
				s += self.bit[k]
				pos += v
			v >>= 1
		return pos

	def __str__(self):
		arr = [self.sum(i, i + 1) for i in range(self.n)]
		return str(arr)
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
toid = {}
for (i, a) in enumerate(A):
	toid[a] = i
bit = BIT(n + 1)
ans = 0
for i in reversed(range(n)):
	b = B[i]
	b = toid[b]
	if bit.sum(0, b) >= 1:
		ans += 1
	bit.add(b, 1)
print(ans)


import sys, atexit
from io import BytesIO
inp = BytesIO(sys.stdin.buffer.read())
input = lambda : inp.readline().decode('ascii')
buf = BytesIO()
atexit.register(lambda : sys.__stdout__.buffer.write(buf.getvalue()))
n = int(input())
a = list(map(int, input().split()))[::-1]
b = list(map(int, input().split()))[::-1]
ans = 0
s = set()
while a and b:
	if a[-1] in s:
		a.pop()
	elif b[-1] in s:
		b.pop()
	elif a[-1] == b[-1]:
		s.add(a.pop())
		b.pop()
	else:
		ans += 1
		s.add(b.pop())
print(ans)


maxn = 10 ** 5 + 10
st = [0] * maxn

def update(i, v):
	while i < maxn:
		st[i] += v
		i += i & -i

def query(i):
	ans = 0
	while i > 0:
		ans += st[i]
		i -= i & -i
	return ans
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
go = {}
for i in range(n):
	go[a[i]] = i + 1
for i in range(n):
	b[i] = go[b[i]]
for i in range(n):
	update(i + 1, 1)
ans = 0
for i in range(n):
	update(b[i], -1)
	if query(b[i]) > 0:
		ans += 1
print(ans)


import sys

def I():
	return sys.stdin.readline().strip()
n = int(I())
a = list(map(int, I().split()))
a.reverse()
b = list(map(int, I().split()))
b.reverse()
c = 0
v = [False for _ in range(100005)]
while len(b):
	out = b.pop()
	if a[-1] != out:
		c += 1
	v[out] = True
	while len(a) > 0 and v[a[-1]]:
		a.pop()
print(c)


def get_int():
	from sys import stdin
	return int(stdin.readline().replace('\n', ''))

def get_int_list():
	from sys import stdin
	return [int(x) for x in stdin.readline().replace('\n', '').split(' ')]

def get_str():
	from sys import stdin
	return stdin.readline().replace('\n', '')

def fined_cars(entering, exiting):
	fined_cars = set()
	i = 0
	while i < len(exiting):
		if entering[i] != exiting[i]:
			if entering[i] not in fined_cars:
				fined_cars.add(exiting.pop(i))
			else:
				entering.pop(i)
		else:
			i += 1
	return len(fined_cars)
n = get_int()
entering = get_int_list()
exiting = get_int_list()
print(fined_cars(entering, exiting))


n = int(input())
(a, b) = ([0 for i in range(n + 1)], [0 for i in range(n + 1)])
(c, d) = (list(map(int, input().split())), list(map(int, input().split())))
for i in range(1, n + 1):
	a[c[i - 1]] = i
for i in range(1, n + 1):
	b[i] = a[d[i - 1]]
(ans, mi) = (0, n + 1)
for i in range(n, 0, -1):
	mi = min(mi, b[i])
	ans += b[i] > mi
print(ans)


import sys
input = sys.stdin.readline
n = int(input())
enter = list(map(int, input().split()))
exit = [0] * (n + 1)
t = input().split()
for i in range(n):
	exit[int(t[i])] = i
(ans, pos) = (0, 0)
for i in enter:
	if exit[i] < pos:
		ans += 1
	else:
		pos = exit[i]
print(ans)


n = int(input())
dat = list(map(int, input().split()))
prio = list(map(int, input().split()))
fine = set()
i = 0
j = 0
while j < n:
	if dat[i] in fine:
		i += 1
	elif dat[i] != prio[j]:
		fine.add(prio[j])
		j += 1
	else:
		j += 1
		i += 1
print(len(fine))


n = int(input())
it = list(map(int, input().split()))
aa = list(map(int, input().split()))
ss = {}
for i in range(n):
	ss[it[i]] = i
aa = [ss[i] for i in aa]
ma = n + 1
tot = 0
d = [0] * n
mi = 0
for i in range(n):
	if aa[i] > mi:
		tot += 1
	d[aa[i]] = 1
	if mi == aa[i]:
		while mi < n:
			if d[mi] == 1:
				mi += 1
			else:
				break
print(tot)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
l = [0 for i in range(n + 1)]
flag = [0 for i in range(n + 1)]
for i in range(n):
	l[a[i]] = i
ct = 0
z = 0
for i in range(n):
	if flag[a[i]] != 1:
		for j in range(z, n):
			if b[j] != a[i]:
				ct += 1
				flag[b[j]] = 1
			else:
				z = j + 1
				break
print(ct)


from math import *
from sys import *
from heapq import *
from collections import defaultdict
import os, sys
from io import IOBase, BytesIO
M = 10 ** 9 + 7

def graph_as_tree_1_root():
	n = int(input())
	dict = defaultdict(list)
	for _ in range(n - 1):
		(x, y) = list(map(int, input().split()))
		dict[x].append(y)
		dict[y].append(x)
	tree = [[1]]
	temp = [1]
	vall = 0
	seen = set([i for i in range(2, n + 1)])
	lol = []
	while len(seen) > 0:
		lol = list(seen)
		tt = []
		for x in temp:
			aa = []
			for val in dict[x]:
				if val in seen:
					aa += [val]
					seen.remove(val)
			dict[x] = aa
			tt += aa
		tree.append(tt)
		temp = tt
		vall += 1
	for x in lol:
		dict[x] = []
	print(tree, dict)

def pow(a, b):
	res = 1
	while b > 0:
		if b & 1:
			res *= a
		a *= a
		b >>= 1
	return res

def powmod(a, b, m):
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % m
		a *= a
		b >>= 1
	return res

def inv(a, m):
	return powmod(a, m - 2, m)

def alldivisors(n):
	list = []
	arr = []
	for i in range(1, int(sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				arr += [i]
			else:
				arr += [i]
				list.append(n // i)
	arr += list[::-1]
	return arr

def primefactorisation(n):
	potentional_p = 3
	itog_list = defaultdict(int)
	if n % 2 == 0:
		itog_list[2] = 0
	while n % 2 == 0:
		n = n // 2
		itog_list[2] += 1
	while n - 1:
		if potentional_p > n ** 0.5:
			itog_list[n] += 1
			return itog_list
		while n % potentional_p == 0:
			n = n // potentional_p
			itog_list[potentional_p] += 1
		potentional_p += 2
	return itog_list

def main():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	res = 0
	dict = defaultdict(int)
	i = 0
	bj = 0
	while bj < n:
		if dict[a[i]] == 1:
			i += 1
			continue
		val = a[i]
		cc = 0
		while 1:
			if b[bj] != val:
				cc += 1
				dict[b[bj]] = 1
				bj += 1
			else:
				res += cc
				dict[b[bj]] = 1
				bj += 1
				break
		i += 1
	print(res)
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
import itertools
import math
import collections
from collections import Counter

def pow(x, y, mod):
	r = 1
	x = x % mod
	while y > 0:
		if y & 1:
			r = r * x % mod
		y = y >> 1
		x = x * x % mod
	return r

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	prime[0] = prime[1] = False
	r = [p for p in range(n + 1) if prime[p]]
	return r

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return collections.defaultdict(int)
n = ii()
t = li()
o = li()
dic = {}
for i in range(n):
	dic[o[i]] = i
r = [dic[t[0]]]
for i in range(1, n):
	r.append(max(r[-1], dic[t[i]]))
res = 0
for i in range(1, n):
	if dic[t[i]] < r[i - 1]:
		res += 1
print(res)


from sys import stdin
n = int(stdin.readline().strip())
s = list(map(int, stdin.readline().strip().split()))
s1 = list(map(int, stdin.readline().strip().split()))
d = [-1 for i in range(n + 2)]
fin = [False for i in range(n + n)]
for i in range(n):
	d[s[i]] = i
for i in range(n):
	s1[i] = d[s1[i]]
ans = 0
mn = s1[-1]
for i in range(n - 2, -1, -1):
	if s1[i] > mn:
		ans += 1
	mn = min(mn, s1[i])
print(ans)


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools
from collections import deque, defaultdict, OrderedDict
import collections

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(1):
		n = ri()
		a = ria()
		b = ria()
		d = {}
		t = 0
		c = 0
		for i in range(n):
			if a[i] not in d:
				for j in range(t, n):
					if a[i] == b[j]:
						t = j + 1
						break
					else:
						d[b[j]] = 1
						c += 1
		print(c)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


n = int(input())
e = list(map(int, input().split()))
s = list(map(int, input().split()))
j = 0
i = 0
count = 0
f = set()
while len(f) < n:
	if e[i] == s[j]:
		f.add(s[j])
		j += 1
		i += 1
	elif e[i] != s[j] and e[i] not in f:
		count += 1
		f.add(s[j])
		j += 1
	else:
		i += 1
print(count)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = dict()
i = 0
j = 0
count = 0
while i < n:
	if c.get(a[i]):
		i += 1
		pass
	elif a[i] != b[j]:
		count += 1
		c[b[j]] = 1
		j += 1
	else:
		i += 1
		j += 1
print(count)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
arr = [0] * (100000 + 1)
i = 0
j = 0
while j < n or i < n:
	if arr[a[i]] == 0:
		if b[j] != a[i]:
			arr[b[j]] = 1
			j += 1
		else:
			i += 1
			j += 1
	else:
		i += 1
print(arr.count(1))


n = int(input())
count = 0
arr = [int(x) for x in input().split()]
brr = [int(x) for x in input().split()]
c = [0 for i in range(0, 100001)]
i = 0
j = 0
while i < n:
	if c[arr[i]] == 1:
		i += 1
		continue
	if arr[i] == brr[j]:
		i += 1
		j += 1
		continue
	else:
		c[brr[j]] = 1
		count += 1
		j += 1
print(count)


n = int(input())
enter = list(map(int, input().split()))
end = list(map(int, input().split()))
l = 0
r = 0
result = 0
passed = []
for i in range(n + 1):
	passed.append(0)
while l < n and r < n:
	passed[enter[l]] = 1
	while r < n and enter[l] != end[r]:
		passed[end[r]] = 1
		result += 1
		r += 1
	r += 1
	if r == n:
		break
	while passed[enter[l]] == 1:
		l += 1
print(result)


import bisect
import os
import gc
import sys
from io import BytesIO, IOBase
from collections import Counter
from collections import deque
import heapq
import math
import statistics

def sin():
	return input()

def ain():
	return list(map(int, sin().split()))

def sain():
	return input().split()

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	s = set()
	for p in range(2, n + 1):
		if prime[p]:
			s.add(p)
	return s

def readTree(n, m):
	adj = [deque([]) for _ in range(n + 1)]
	for _ in range(m):
		(u, v) = ain()
		adj[u].append(v)
		adj[v].append(u)
	return adj

def main():
	n = iin()
	a = ain()
	b = ain()
	pos = [0] * (n + 1)
	for i in range(1, n + 1):
		pos[b[i - 1]] = i
	c = [0] * (n + 1)
	for i in range(1, n + 1):
		c[i] = pos[a[i - 1]]
	mx = -1
	ans = 0
	for i in range(1, n + 1):
		if c[i] > mx:
			mx = c[i]
		else:
			ans += 1
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
if os.getcwd() == 'D:\\code':
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = int(input())
in_sequency = list(map(int, input().split()))
out_sequency = list(map(int, input().split()))
index = [0] * (n + 1)
aux = [0] * (n + 1)
max = -1
infractors = 0
for i in range(n):
	index[out_sequency[i]] = i
for i in range(n):
	aux[i] = index[in_sequency[i]]
for i in range(n):
	if aux[i] > max:
		max = aux[i]
	else:
		infractors += 1
print(infractors)


n = int(input())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
l1.reverse()
l2.reverse()
trans = [0] * n
for i in range(n):
	trans[l1[i] - 1] = i
end = [0] * n
for i in range(n):
	end[i] = trans[l2[i] - 1]
wyn = 0
maksi = end[0]
for i in range(1, n):
	if end[i] < maksi:
		wyn += 1
	maksi = max(end[i], maksi)
print(wyn)


import sys
readline = sys.stdin.readline

class BIT:

	def __init__(self, n):
		self.size = n
		self.tree = [0] * (n + 1)
		self.p = 2 ** (n.bit_length() - 1)
		self.dep = n.bit_length()

	def get(self, i):
		s = 0
		while i > 0:
			s += self.tree[i]
			i -= i & -i
		return s

	def add(self, i, x):
		while i <= self.size:
			self.tree[i] += x
			i += i & -i

	def bl(self, v):
		if v <= 0:
			return -1
		s = 0
		k = self.p
		for _ in range(self.dep):
			if s + k <= self.size and self.tree[s + k] < v:
				s += k
				v -= self.tree[s + k]
			k //= 2
		return s + 1
N = int(readline())
A = list(map(int, readline().split()))
B = list(map(int, readline().split()))
A.reverse()
B.reverse()
Bi = [None] * (N + 1)
for i in range(N):
	b = B[i]
	Bi[b] = i + 1
T = BIT(N)
ans = 0
for i in range(N - 1, -1, -1):
	a = A[i]
	bi = Bi[a]
	if T.get(bi) > 0:
		ans += 1
	T.add(bi, 1)
print(ans)


rr = lambda : input().strip()
rri = lambda : int(rr())
rrm = lambda : [int(x) for x in rr().split()]
from sys import stdin, stdout

def sol():
	n = rri()
	a = rrm()
	b = rrm()
	c = [0 for i in range(n + 1)]
	for (i, v) in enumerate(b):
		c[v] = i
	cm = 0
	res = 0
	for i in a:
		if c[i] < cm:
			res += 1
		else:
			cm = c[i]
	print(res)
	return
sol()


from collections import defaultdict
n = int(input())
order_enter = list(map(int, input().split()))
order_exit = list(map(int, input().split()))
seen = defaultdict(lambda : 0)
ans = 0
i = 0
j = 0
while i < n or j < n:
	if seen[order_enter[i]] == 0:
		if order_exit[j] != order_enter[i]:
			ans += 1
			seen[order_exit[j]] = 1
			j += 1
		elif order_exit[j] == order_enter[i]:
			j += 1
			i += 1
	else:
		seen[order_enter[i]] = 0
		i += 1
print(ans)


n = int(input())
cnt = 0
l = list(map(int, input().split()))
s = list(map(int, input().split()))
d = {}
pos = [0 for x in range(n)]
for i in range(n):
	d[s[i]] = i + 1
for i in range(n):
	pos[i] = d[l[i]]
max1 = pos[0]
for i in range(1, n):
	if max1 > pos[i]:
		cnt += 1
	max1 = max(max1, pos[i])
print(cnt)


mod = 10 ** 9 + 7
from sys import stdin, stdout
import bisect
import sys
sys.setrecursionlimit(10 ** 6)
from bisect import bisect_left as bl
from bisect import bisect_right as br
import itertools
import collections
import math
import heapq
from random import randint as rn

def modinv(n, p):
	return pow(n, p - 2, p)

def ncr(n, r, p):
	t = fact[n] * (ifact[r] * ifact[n - r] % p) % p
	return t

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())

def GCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def main():
	t = inin()
	a = ain()
	b = ain()
	x = 0
	y = 0
	d = {}
	c = 0
	while 1:
		if x == t or y == t:
			break
		while 1:
			if a[x] not in d:
				break
			x += 1
			if x == t or y == t:
				break
		if x == t or y == t:
			break
		if a[x] != b[y]:
			c += 1
			d[b[y]] = 1
			y += 1
		else:
			d[b[y]] = 1
			x += 1
			y += 1
	print(c)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def readnumbers(zero=0):
	conv = ord if py2 else lambda x: x
	A = []
	numb = zero
	sign = 1
	i = 0
	s = sys.stdin.buffer.read()
	try:
		while True:
			if s[i] >= b'0'[0]:
				numb = 10 * numb + conv(s[i]) - 48
			elif s[i] == b'-'[0]:
				sign = -1
			elif s[i] != b'\r'[0]:
				A.append(sign * numb)
				numb = zero
				sign = 1
			i += 1
	except:
		pass
	if s and s[-1] >= b'0'[0]:
		A.append(sign * numb)
	return A
main()


import collections
n = int(input())
ent = list(map(int, input().strip().split(' ')))
exi = list(map(int, input().strip().split(' ')))
de = collections.deque()
for i in ent:
	de.append(i)
vis = {}
i = 0
fine = 0
while i < len(exi):
	if de[0] in vis:
		de.popleft()
		continue
	if de[0] == exi[i]:
		de.popleft()
	else:
		fine += 1
		vis[exi[i]] = 1
	i += 1
print(fine)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def main():
	t = 1
	rr = []
	for _ in range(t):
		n = I()
		a = LI()
		b = LI()
		d = {}
		for (i, c) in enumerate(a):
			d[c] = i
		s = set()
		ai = 0
		r = 0
		for i in range(n):
			bi = d[b[i]]
			if ai == bi:
				ai += 1
				while ai < n and a[ai] in s:
					s.remove(a[ai])
					ai += 1
			else:
				s.add(a[bi])
			if ai < bi:
				r += 1
		rr.append(r)
	return JA(rr, '\n')
print(main())


n = int(input())
a = []
stra = input()
a = stra.split()
strb = input()
b = strb.split()
cnt = 0
ans = 0
s = set()
for x in a:
	if x in s:
		ans += 1
		continue
	while cnt < n and x != b[cnt]:
		s.add(b[cnt])
		cnt += 1
print(ans)


n = int(input())
entering = input().split()
exiting = input().split()
set_ = set()
i = 0
counter = 0
j = 0
while j < len(exiting):
	if entering[i] == exiting[j]:
		i += 1
		j += 1
	elif entering[i] in set_:
		counter += 1
		set_.remove(entering[i])
		i += 1
	else:
		set_.add(exiting[j])
		j += 1
counter += len(set_)
print(counter)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = dict()
s = set()
for (i, c) in enumerate(b):
	m[c] = i
max_last = 0
ans = 0
for (i, c) in enumerate(a):
	if i > 0:
		max_last = max(max_last, m[a[i - 1]])
	if m[c] < max_last:
		ans += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
k = 1
f = {}
for i in b:
	f[i] = k
	k += 1
mx = f[a[0]]
ans = 0
a = [f[i] for i in a]
for i in a:
	if i < mx:
		ans += 1
	else:
		mx = i
print(ans)


(z, zz) = (input, lambda : list(map(int, z().split())))
fast = lambda : stdin.readline().strip()
zzz = lambda : [int(i) for i in fast().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = 1
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
n = int(z())
arr1 = zzz()
arr2 = zzz()
d = {}
for i in range(n):
	d[arr2[i]] = i + 1
x = 0
ans = n
for i in range(n):
	if d[arr1[i]] > x:
		x = d[arr1[i]]
		ans -= 1
print(ans)


n = int(input())
a = input().split()
b = input().split()
indices = dict()
for i in range(n):
	car = b[i]
	indices[car] = i
aux = 0
finned = 0
for i in range(n):
	ath_at_b = indices[a[i]]
	aux = max(aux, ath_at_b)
	if ath_at_b < aux:
		finned += 1
print(finned)


n = int(input())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
i = 0
j = 0
temp = set()
c = 0
while i < n:
	if l2[i] == l1[j]:
		j = j + 1
		i = i + 1
	elif l2[i] != l1[j] and l1[j] not in temp:
		c = c + 1
		temp.add(l2[i])
		i = i + 1
	elif l2[i] != l1[j] and l1[j] in temp:
		j = j + 1
print(c)


n = int(input())
line = input()
orders = [i for i in line.split()]
line = input()
cur_expected = 0
res = 0
visited = dict()
for (order, no) in enumerate(line.split()):
	if no != orders[cur_expected] and orders[cur_expected] not in visited:
		res += 1
	else:
		cur_expected += 1
		while cur_expected < n and orders[cur_expected] in visited:
			cur_expected += 1
	visited[no] = True
print(res)


import array
import math
import os
import sys

def main():
	N = read_int()
	A = read_ints()
	B = read_ints()
	print(solve(N, A, B))

def solve(N, A, B):
	T = {}
	i = 0
	for b in B:
		T[b] = i
		i += 1
	last_pos = -1
	ans = 0
	for a in A:
		t = T[a]
		if t > last_pos:
			last_pos = t
		else:
			ans += 1
	return ans
DEBUG = 'DEBUG' in os.environ

def inp():
	return sys.stdin.readline().rstrip()

def read_int():
	return int(inp())

def read_ints():
	return [int(e) for e in inp().split()]

def dprint(*value, sep=' ', end='\n'):
	if DEBUG:
		print(*value, sep=sep, end=end)
main()


from collections import defaultdict, deque, Counter
from sys import stdin, stdout
from heapq import heappush, heappop
import math
import io
import os
import math
import bisect

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def digits(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def ceil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def mapin():
	return map(int, input().split())
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [0] * n
for i in range(n):
	c[b[i] - 1] = i + 1
ma = 0
ans = 0
for i in range(n):
	if c[a[i] - 1] < ma:
		ans += 1
	ma = max(ma, c[a[i] - 1])
print(ans)


import math

def main():
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	out = set()
	j = 0
	result = 0
	for i in range(n):
		if b[i] != a[j]:
			out.add(b[i])
			result += 1
		else:
			j += 1
			while j < n and a[j] in out:
				j += 1
	print(result)
main()


n = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
mp = {}
x = 0
for i in b:
	mp[i] = x
	x += 1
ans = 0
mx = -1
for i in a:
	if mp.get(i, 0) < mx:
		ans += 1
	mx = max(mx, mp.get(i, 0))
print(ans)


n = int(input())
_in = list(map(int, input().split()))
_out = list(map(int, input().split()))
fine = set()
j = 0
for i in range(n):
	if _in[i] not in fine:
		while j < n and _out[j] != _in[i]:
			fine.add(_out[j])
			j += 1
		j += 1
	i += 1
print(len(fine))


n = int(input())
As = [int(x) for x in input().split()][::-1]
Bs = [int(x) for x in input().split()][::-1]
count = 0
s = set()
while As and Bs:
	if As[-1] in s:
		As.pop()
	elif Bs[-1] in s:
		Bs.pop()
	elif As[-1] == Bs[-1]:
		As.pop()
		Bs.pop()
	else:
		count += 1
		s.add(Bs.pop())
print(count)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = {}
for i in a:
	d[i] = 1
i = j = ans = 0
while i < n and j < n:
	if d[a[i]] == 0:
		i += 1
		continue
	if a[i] == b[j]:
		i += 1
		j += 1
	else:
		d[b[j]] = 0
		ans += 1
		j += 1
print(ans)


n = int(input())
entry = list(map(int, input().split()))
exit = list(map(int, input().split()))
temp = [0] * 1000000
i = j = 0
flag = cnt = 0
for k in range(2 * n):
	if i == n or j == n:
		break
	if temp[entry[i]] == 1:
		i += 1
		continue
	if entry[i] == exit[j]:
		i += 1
		j += 1
	else:
		cnt += 1
		temp[exit[j]] = 1
		j += 1
print(cnt)


import sys, math
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
q = [0] * max(a)
for i in range(len(a)):
	q[a[i] - 1] = i
i = len(a) - 1
c = -1
w = len(a) + 1
while i >= 0:
	if q[b[i] - 1] < w:
		c = c + 1
		w = q[b[i] - 1]
	i = i - 1
print(len(a) - c - 1)


from collections import Counter
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
mod = int(1000000000.0) + 7

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
n = iinput()
a = rlinput()
b = rlinput()
d = {b[i]: i for i in range(n)}
ans = 0
mx = -1
for i in range(n):
	if d[a[i]] < mx:
		ans += 1
	mx = max(mx, d[a[i]])
print(ans)


from collections import deque
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
i = 0
j = 0
count = 0
visited_b = [False] * n
while j < n:
	if b[j] != a[i] and (not visited_b[a[i] - 1]):
		visited_b[b[j] - 1] = True
		count += 1
		j += 1
	elif a[i] == b[j]:
		visited_b[b[j] - 1] = True
		i += 1
		j += 1
	else:
		i += 1
print(count)


n = int(input())
l = input().split()
li = [int(i) for i in l]
ou = input().split()
out = [int(i) for i in ou]
visited = [0 for i in range(n)]
count = 0
li.reverse()
out.reverse()
while out != []:
	while li != [] and visited[li[-1] - 1] == 1:
		li.pop(-1)
	if li == []:
		break
	while out != [] and out[-1] != li[-1]:
		count += 1
		visited[out[-1] - 1] = 1
		out.pop(-1)
	out.pop(-1)
	li.pop(-1)
print(count)


n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
aP = 0
bP = 0
cnt = 0
exploredB = [False for _ in range(n + 1)]
for aP in range(n):
	if exploredB[a[aP]] == False:
		while bP < n and b[bP] != a[aP]:
			exploredB[b[bP]] = True
			bP += 1
			cnt += 1
		if bP < n:
			exploredB[b[bP]] = True
			bP += 1
print(cnt)


n = int(input())
going = list(map(int, input().strip().split()))
coming = list(map(int, input().strip().split()))
dick = {}
(i, j, fine) = (0, 0, 0)
while True:
	if i > n - 1 or j > n - 1:
		break
	if dick.get(going[i], 0) == 1:
		i += 1
		continue
	if going[i] == coming[j]:
		i += 1
	else:
		fine += 1
		dick[coming[j]] = 1
	j += 1
print(fine)


n_casos = int(input())
carros_na_ordem = input().split(' ')
carros_fora_ordem = input().split(' ')
ponteiro_fila1 = 0
ponteiro_fila2 = 0
banco_de_carros_fugoes = set()
while ponteiro_fila2 < n_casos:
	if not carros_na_ordem[ponteiro_fila1] in banco_de_carros_fugoes:
		if carros_na_ordem[ponteiro_fila1] == carros_fora_ordem[ponteiro_fila2]:
			ponteiro_fila1 += 1
			ponteiro_fila2 += 1
		else:
			banco_de_carros_fugoes.add(carros_fora_ordem[ponteiro_fila2])
			ponteiro_fila2 += 1
	else:
		ponteiro_fila1 += 1
print(len(banco_de_carros_fugoes))


n = int(input())
start = map(int, input().split(' '))
start_dict = {}
for (start_i, car_id) in enumerate(start):
	start_dict[car_id] = start_i
end = list(map(int, input().split(' ')))
out = 0
for count in range(n):
	i = n - 1 - count
	car_id = end[i]
	if count == 0:
		last_start_i = start_dict[car_id]
		continue
	cur_start_i = start_dict[car_id]
	if cur_start_i < last_start_i:
		last_start_i = cur_start_i
	else:
		out += 1
print(out)


num_de_carros = int(input())
ids_entrada = list(map(int, input().split()))
ids_saida = list(map(int, input().split()))
ids_entrada_reverso = reversed(ids_entrada)
ids_saida_reverso = reversed(ids_saida)
saida_correta = {}
aux = 1
for i in ids_entrada_reverso:
	saida_correta[i] = aux
	aux += 1
saida_incorreta = []
for i in ids_saida_reverso:
	saida_incorreta.append(saida_correta[i])
multados = 0
maximo = 0
for i in saida_incorreta:
	if maximo < i:
		maximo = i
	else:
		multados += 1
print(multados)


n = int(input())
arr = [0] + list(map(int, input().split()))
brr = [0] + list(map(int, input().split()))
m = n
maxi = 0
i = 1
j = 1
dic = {}
while i <= m and j <= n:
	if arr[i] in dic:
		if dic[arr[i]] == 1:
			dic[arr[i]] = 0
			i += 1
	elif arr[i] == brr[j]:
		i += 1
		j += 1
	else:
		maxi += 1
		dic[brr[j]] = 1
		j += 1
print(maxi)


from collections import deque
n = int(input())
a = deque(input().split(' '))
b = deque(input().split(' '))
m = [False] * (n + 1)
count = 0
while a and b:
	flag = True
	while a[0] != b[0]:
		if m[int(a[0])]:
			a.popleft()
			flag = False
			break
		count += 1
		m[int(b.popleft())] = True
	if flag:
		a.popleft()
		b.popleft()
print(count)


n = int(input())
enter = list(map(int, input().split()))
exit = list(map(int, input().split()))
(i, j) = (0, 0)
cost = 0
exit_cars = {}
while i < n and j < n and (len(exit_cars) != n):
	try:
		val = exit_cars[enter[i]]
		i += 1
	except:
		if enter[i] != exit[j]:
			cost += 1
			if i < n and j < n:
				exit_cars[exit[j]] = 0
			j += 1
		else:
			if i < n and j < n:
				exit_cars[exit[j]] = 0
			i += 1
			j += 1
print(cost)


def main():
	t = 1
	for _ in range(t):
		n = int(input())
		tab1 = list(map(int, input().split()))
		ind1 = [0] * n
		ind2 = [0] * n
		inc = 0
		tab2 = list(map(int, input().split()))
		for x in range(n):
			ind2[tab2[x] - 1] = x
		resm = -10000000
		for x in range(n):
			if ind2[tab1[x] - 1] <= resm:
				inc += 1
			else:
				resm = ind2[tab1[x] - 1]
		print(inc)
main()


n = int(input())
enter = [int(x) for x in input().split(' ')]
exit = [int(x) for x in input().split(' ')]
i = 1
fine = 0
indexOF = {}
for item in enter:
	indexOF[item] = i
	i += 1
arr = [indexOF[item] for item in exit]
sanku = arr[-1]
x = n - 2
while x >= 0:
	if arr[x] > sanku:
		fine += 1
	sanku = min(sanku, arr[x])
	x -= 1
print(fine)


x = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = x - 1
c = x - 1
f = 0
e = {}
while c > 0:
	if b[d] in e:
		d -= 1
	elif a[c] != b[d]:
		f += 1
		e[a[c]] = 1
		c -= 1
	else:
		c -= 1
		d -= 1
print(f)


number = input()
entering = input().split()
exiting = input().split()
eliminados = set()
i = 0
while len(entering) > 0:
	if entering[0] in eliminados:
		entering.pop(0)
		continue
	elif exiting[i] != entering[0]:
		eliminados.add(exiting[i])
		i += 1
		continue
	else:
		entering.pop(0)
		i += 1
print(len(eliminados))


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
isFined = [False] * (n + 1)
cur = 0
i = 0
ans = 0
while i < n and cur < n:
	if b[i] == a[cur]:
		cur += 1
		while cur < n and isFined[a[cur]]:
			cur += 1
	else:
		ans += 1
		isFined[b[i]] = True
	i += 1
print(ans)


from sys import stdin
n = int(input())
a = list(map(int, stdin.readline().split()))
b = list(map(int, stdin.readline().split()))
pos = [0 for i in range(n + 1)]
for i in range(n):
	pos[b[i]] = i + 1
cur = pos[a[0]]
ans = 0
for i in range(1, n):
	if pos[a[i]] < cur:
		ans += 1
	cur = max(cur, pos[a[i]])
print(ans)


from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
k = defaultdict(bool)
i = j = 0
cnt = 0
while i < n and j < n:
	if not k[a[i]] and a[i] != b[j]:
		k[b[j]] = True
		cnt += 1
		j += 1
	elif k[a[i]]:
		i += 1
	else:
		i += 1
		j += 1
print(cnt)


from sys import stdin, stdout
n = int(stdin.readline())
exit_suc = [False] * n
entering = list(map(int, stdin.readline().split()))
exiting = list(map(int, stdin.readline().split()))
(cont, j) = (0, 0)
for i in range(n):
	while exit_suc[entering[j] - 1]:
		j += 1
	if exiting[i] == entering[j]:
		j += 1
	else:
		cont += 1
	exit_suc[exiting[i] - 1] = True
stdout.write(str(cont) + '\n')


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
res = 0
fined = dict()
i = j = 0
while j < n and i < n:
	if fined.get(a[i], -1) == -1:
		if a[i] == b[j]:
			i += 1
			j += 1
		else:
			res += 1
			fined[b[j]] = 1
			j += 1
	else:
		i += 1
print(res)


from collections import deque
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = {}
res = 0
for i in range(n):
	d[b[i]] = i + 1
counter = -1
for i in range(n):
	if d[a[i]] < counter:
		res += 1
	counter = max(counter, d[a[i]])
print(res)


n = int(input())
a = [int(o) for o in input().split()]
b = [int(o) for o in input().split()]
i = 0
j = 0
s = set()
ans = 0
while i < n and j < n:
	while i < n and a[i] in s:
		i += 1
	if i < n and a[i] == b[j]:
		i += 1
		j += 1
	elif j < n and a[i] != b[j]:
		s.add(b[j])
		ans += 1
		j += 1
print(ans)


import queue
n = int(input())
q = queue.Queue(n)
xs = list(map(int, input().split()))
for x in xs:
	q.put(x)
l = list(map(int, input().split()))
e = q.get()
fine = 0
cars = [False] * (n + 1)
for x in l:
	cars[x] = True
	if x != e:
		fine += 1
	else:
		while not q.empty():
			e = q.get()
			if not cars[e]:
				break
print(fine)


from sys import stdin

def solve(enterOrder, leaveOrder, x):
	enterOrderMap = {}
	leaveOrderMap = {}
	for i in range(x):
		enterOrderMap[enterOrder[i]] = i
	for i in range(x):
		leaveOrderMap[leaveOrder[i]] = i
	count = 0
	last = 0
	for key in enterOrder:
		if enterOrderMap[key] <= leaveOrderMap[key] and leaveOrderMap[key] >= last:
			count += leaveOrderMap[key] - last
			last = leaveOrderMap[key] + 1
	return count
x = int(input())
enterOrder = list(map(int, stdin.readline().rstrip().split()))
leaveOrder = list(map(int, stdin.readline().rstrip().split()))
print(solve(enterOrder, leaveOrder, x))


n = int(input())
entry = list(map(int, input().split()))
exit = list(map(int, input().split()))
d = {}
s = 0
for i in range(0, len(exit)):
	d[exit[i]] = i
m = d[entry[0]]
for i in range(0, len(exit) - 1):
	if d[entry[i + 1]] > m:
		pass
	else:
		s += 1
	if d[entry[i + 1]] > m:
		m = d[entry[i + 1]]
print(s)


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

class SegTree:

	def __init__(self, n, func, init):
		self.n = n
		self.func = func
		self.init = init
		n2 = 1
		while n2 < n:
			n2 <<= 1
		self.n2 = n2
		self.tree = [self.init] * (n2 << 1)

	def update(self, i, x):
		i += self.n2
		self.tree[i] = x
		while i > 1:
			self.tree[i >> 1] = x = self.func(x, self.tree[i ^ 1])
			i >>= 1

	def query(self, a, b):
		l = a + self.n2
		r = b + self.n2
		s = self.init
		while l < r:
			if r & 1:
				r -= 1
				s = self.func(s, self.tree[r])
			if l & 1:
				s = self.func(s, self.tree[l])
				l += 1
			l >>= 1
			r >>= 1
		return s
N = INT()
A = LIST()
B = LIST()
d = {}
for (i, a) in enumerate(A):
	d[a] = i
st = SegTree(N, min, INF)
B2 = [0] * N
for (i, b) in enumerate(B):
	st.update(i, d[b])
	B2[i] = d[b]
ans = 0
for (i, b) in enumerate(B2):
	mn = st.query(i + 1, N)
	if mn < b:
		ans += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
A = {}
B = {}
for i in range(n):
	A[a[i]] = i
	B[b[i]] = i
coun = 0
mi = n - 1
for i in range(n - 2, -1, -1):
	if A[b[i]] < A[b[mi]]:
		mi = i
	if B[b[i]] < mi:
		coun += 1
print(coun)


n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
i = 0
j = 0
d = {}
count = 0
while i < n and j < n:
	if d.get(a[i]):
		i += 1
		continue
	if a[i] != b[j]:
		d[b[j]] = 1
		count += 1
		j += 1
	else:
		i += 1
		j += 1
print(count)


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
i = 0
cur = 0
count = 0
seen = set()
while i < n:
	while b[i] != a[cur]:
		count += 1
		seen.add(b[i])
		i += 1
	cur += 1
	while cur < n and a[cur] in seen:
		cur += 1
	i += 1
print(count)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
culprit = []
count = 0
for c in range(n):
	culprit.append(False)
j = 0
i = 0
while i < n and j < n:
	if culprit[a[j] - 1]:
		j += 1
	elif a[j] == b[i]:
		i += 1
		j += 1
	else:
		culprit[b[i] - 1] = True
		count += 1
		i += 1
print(count)


a = int(input())
array = list(map(int, input().split()))
aray = list(map(int, input().split()))
x = 0
y = 0
dic = {}
answer = 0
while True:
	if x == a or y == a:
		break
	if array[x] in dic:
		x += 1
	elif array[x] == aray[y]:
		x += 1
		y += 1
	else:
		answer += 1
		dic[aray[y]] = 5
		y += 1
print(answer)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = {}
for i in range(n):
	d[b[i]] = i + 1
(x, r) = (0, n)
for i in range(n):
	if d[a[i]] > x:
		x = d[a[i]]
		r -= 1
print(r)


n = int(input())
a = list(map(lambda x: int(x) - 1, input().split()))
b = list(map(lambda x: int(x) - 1, input().split()))
ind = [0] * n
for i in range(n):
	ind[a[i]] = i
for i in range(n):
	b[i] = ind[b[i]]
res = 0
m = float('inf')
for i in range(n - 1, -1, -1):
	if b[i] > m:
		res += 1
	m = min(m, b[i])
print(res)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.reverse()
pos = [0] * (n + 1)
for i in range(n):
	pos[b[i]] = i
check = 0
count = 0
for i in range(1, n):
	if pos[a[i]] > pos[a[check]]:
		count -= -1
	else:
		check = i
print(count)


a = int(input())
b = list(map(int, input().split()))
d = dict(zip(b, range(a)))
c = list(map(int, input().split()))
(i, j, s) = (0, 0, 0)
while i < a and j < a:
	if b[i] == 0:
		i += 1
		s += 1
		continue
	while j < a and b[i] != c[j]:
		b[d[c[j]]] = 0
		j += 1
	i += 1
print(s)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import Counter
import sys
import math
from math import gcd
n = int(input())
arr = list(map(int, input().split()))
ls = list(map(int, input().split()))
v = [False] * (10 ** 5 + 1)
i = 0
ptr = 0
m = 0
for i in range(n):
	cnt = 0
	if v[arr[i]] == True:
		continue
	while ls[ptr] != arr[i]:
		v[ls[ptr]] = True
		ptr += 1
		cnt += 1
	m += cnt
	ptr += 1
print(m)


import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
inda = 0
indb = 0
ANS = 0
USE = [0] * (n + 1)
while inda < n and indb < n:
	NOW = A[inda]
	USE[A[inda]] = 1
	for i in range(indb, n):
		if B[i] == NOW:
			break
		elif USE[B[i]] == 0:
			ANS += 1
			USE[B[i]] = 1
	indb = i + 1
	while inda < n and USE[A[inda]] == 1:
		inda += 1
print(ANS)


n = int(input())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
l3 = [-1] * (n + 1)
l4 = [-1] * n
ans = 0
t = 0
for i in range(n):
	l3[l2[i]] = i
for i in range(n):
	l4[i] = l3[l1[i]]
m = l4[0]
for i in range(1, n):
	if l4[i] < m:
		ans += 1
	m = max(m, l4[i])
print(ans)


n = int(input())
a = [int(s) for s in input().split()]
b = [int(s) for s in input().split()]
visit = {}
for i in a:
	visit[i] = 0
i = 0
j = 0
fine = []
while i < n:
	if not visit[a[i]]:
		while j < n:
			if b[j] == a[i]:
				visit[a[i]] = 1
				j = j + 1
				break
			else:
				visit[b[j]] = 1
				fine.append(b[j])
				j = j + 1
	i += 1
print(len(fine))


n = int(input())
s = list(input().split())
t = list(input().split())
total = 0
idex = 0
out = set()
for v in t:
	while s[idex] in out:
		idex += 1
	if v == s[idex]:
		idex += 1
	else:
		total += 1
		out.add(v)
print(total)


n = int(input())
arr = list(map(int, input().split()))
brr = list(map(int, input().split()))
prev = -1
d = dict()
for i in range(n):
	d[brr[i]] = i
ans = 0
for i in range(n):
	if d[arr[i]] - prev > 0:
		ans += d[arr[i]] - prev - 1
		prev = d[arr[i]]
print(ans)


input()
ans = 0
enter = [int(x) for x in input().split()]
ex = [int(x) for x in input().split()]
cur = 0
fined_cars = set()
for ex_car in ex:
	if ex_car != enter[cur]:
		if enter[cur] in fined_cars:
			while enter[cur] in fined_cars:
				cur += 1
			if ex_car != enter[cur]:
				ans += 1
				fined_cars.add(ex_car)
			else:
				cur += 1
		else:
			fined_cars.add(ex_car)
			ans += 1
	else:
		cur += 1
print(ans)


n = int(input())
enter = list(map(int, input().split()))
out = list(map(int, input().split()))
posicao = {}
fine = n
j = 0
for i in range(n):
	posicao[out[i]] = i + 1
for e in range(n):
	if posicao[enter[e]] > j:
		j = posicao[enter[e]]
		fine -= 1
print(fine)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = dict()
cnt = 0
a.reverse()
b.reverse()
ptr = 0
for i in range(0, n):
	while b[ptr] in d:
		ptr += 1
	if a[i] != b[ptr]:
		cnt += 1
		d[a[i]] = True
	else:
		ptr += 1
print(cnt)


import bisect
import decimal
from decimal import Decimal
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
decimal.getcontext().prec = 46

def primeFactors(n):
	prime = set()
	while n % 2 == 0:
		prime.add(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			prime.add(i)
			n = n // i
	if n > 2:
		prime.add(n)
	return list(prime)

def getFactors(n):
	factors = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				factors.append(i)
			else:
				factors.append(i)
				factors.append(n // i)
		i = i + 1
	return factors

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	num = []
	for p in range(2, n + 1):
		if prime[p]:
			num.append(p)
	return num

def lcm(a, b):
	return a * b // math.gcd(a, b)

def sort_dict(key_value):
	return sorted(key_value.items(), key=lambda kv: (kv[1], kv[0]))

def list_input():
	return list(map(int, input().split()))

def num_input():
	return map(int, input().split())

def string_list():
	return list(input())

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def binaryToDecimal(n):
	return int(n, 2)

def DFS(n, s, adj):
	visited = [False for i in range(n + 1)]
	stack = []
	stack.append(s)
	while len(stack):
		s = stack[-1]
		stack.pop()
		if not visited[s]:
			visited[s] = True
		for node in adj[s]:
			if not visited[node]:
				stack.append(node)

def solve():
	n = int(input())
	num = [False] * (n + 1)
	inside = list_input()
	outside = list_input()
	fine = i = j = 0
	while i < n:
		if num[inside[i]]:
			i += 1
		elif inside[i] == outside[j]:
			i += 1
			j += 1
		else:
			fine += 1
			num[outside[j]] = True
			j += 1
	print(fine)
t = 1
for _ in range(t):
	solve()


from os import path
from io import BytesIO, IOBase
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, Counter, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('0')
file = 1

def ceil(a, b):
	return (a + b - 1) // b

def solve():
	n = ii()
	a = li()
	b = li()
	m = {}
	for i in range(n):
		m[a[i]] = i
	sufmnx = [0] * n
	sufmnx[-1] = m[b[i]]
	for i in range(n - 2, -1, -1):
		sufmnx[i] = min(sufmnx[i + 1], m[b[i]])
	cnt = 0
	for i in range(n):
		if m[b[i]] > sufmnx[i]:
			cnt += 1
	print(cnt)
if file:
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
solve()


n = input()
n = int(n)
l1 = input()
ent = [int(i) for i in l1.split()]
l2 = input()
ext = [int(i) for i in l2.split()]
con = 0
fin = 0
seen = [None] * 100050
i = 0
j = 0
while j != n:
	if seen[ent[i]] == True:
		i += 1
		continue
	if ent[i] == ext[j]:
		i += 1
		j += 1
	else:
		seen[ext[j]] = True
		fin += 1
		j += 1
print(fin)


n = input()
cars_in = list(map(int, input().split()))
cars_out = list(map(int, input().split()))
out = set([])
fined = 0
(i, j) = (0, 0)
while i < len(cars_in) and j < len(cars_in):
	if cars_in[i] in out:
		i += 1
	elif cars_out[j] == cars_in[i]:
		out.add(cars_out[j])
		i += 1
		j += 1
	else:
		out.add(cars_out[j])
		j += 1
		fined += 1
print(fined)


from sys import stdin
input = stdin.readline
R = lambda : map(int, input().split())
ans = p = 0
n = int(input())
pos = [0] * (n + 1)
p = ans = 0
v = list(R())
for i in range(n):
	pos[v[i]] = i
for i in R():
	while not v[p]:
		p += 1
	if i == v[p]:
		p += 1
	else:
		v[pos[i]] = 0
		ans += 1
print(ans)


def solver():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	maps = {a[i]: i for i in range(n)}
	ok = [0] * (n + 10)
	ret = 0
	ans = 0
	for i in range(n):
		cnt = maps[b[i]]
		if cnt == ret:
			ans += 1
			ok[ret] = 1
			while ok[ret] == 1:
				ret += 1
		ok[cnt] = 1
	print(n - ans)

def main():
	while 1:
		try:
			solver()
		except EOFError:
			break
main()


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
i = n - 1
j = n - 1
count = 0
d = {}
while i > 0:
	if b[j] in d:
		j -= 1
	elif a[i] != b[j]:
		count += 1
		d[a[i]] = 1
		i -= 1
	else:
		i -= 1
		j -= 1
print(count)


n = int(input())
to = list(map(int, input().split()))
out = list(map(int, input().split()))
bad_boyz = [False] * n
counter = i = j = 0
while j < n:
	if bad_boyz[to[i] - 1]:
		i += 1
	else:
		if to[i] != out[j]:
			counter += 1
			bad_boyz[out[j] - 1] = True
		else:
			i += 1
		j += 1
print(counter)


n = int(input())
a = [int(s) for s in input().split(' ')]
b = [int(s) for s in input().split(' ')]
a_map = {c: i for (i, c) in enumerate(a)}
c = [a_map[i] for i in b]
ans = 0
min_num = c[-1]
for a in c[::-1]:
	min_num = min(a, min_num)
	if a != min_num:
		ans += 1
print(ans)


n = int(input())
entry = list(map(int, input().split()))
exi = list(map(int, input().split()))
dic1 = {}
dic2 = {}
for i in range(n):
	dic2[exi[i]] = i
final = []
for i in entry:
	final.append(dic2[i] + 1)
maxi = 0
cnt = 0
for i in range(n - 1):
	maxi = max(maxi, final[i])
	if final[i + 1] < maxi:
		cnt += 1
print(cnt)


n = int(input())
e = list(map(int, input().split(' ')))
o = list(map(int, input().split(' ')))
aux = [False for x in range(n)]
(aux_e, aux_o, amount) = (0, 0, 0)
while aux_o != n and aux_e != n:
	if e[aux_e] != o[aux_o]:
		if not aux[e[aux_e] - 1]:
			aux[o[aux_o] - 1] = True
			amount += 1
			aux_o += 1
		else:
			aux_e += 1
	else:
		aux[o[aux_o] - 1] = True
		aux_e += 1
		aux_o += 1
print(amount)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
up = 0
ans = n
di = [0] * (n + 1)
for i in range(n):
	di[b[i]] = i + 1
for i in range(n):
	pos = di[a[i]]
	if pos > up:
		up = pos
		ans -= 1
print(ans)


n = int(input())
a = [0] * n
b = [0] * n
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [0] * n
for i in range(n):
	c[a[i] - 1] = i
mn = c[b[n - 1] - 1]
ans = 0
for i in range(n - 2, -1, -1):
	if c[b[i] - 1] > mn:
		ans += 1
	mn = min(mn, c[b[i] - 1])
print(ans)


n = int(input())
c = [0 for i in range(n)]
a = [int(i) - 1 for i in input().split(' ')]
for i in range(n):
	c[a[i]] = i
b = [int(i) - 1 for i in input().split(' ')]
od = [c[i] for i in b]
pre = 0
fi = od[n - 1]
for i in range(n - 2, -1, -1):
	if od[i] > fi:
		pre += 1
	else:
		fi = od[i]
print(pre)


import sys
import math
from collections import deque

def read_line():
	return sys.stdin.readline()[:-1]

def read_int():
	return int(sys.stdin.readline())

def read_int_line():
	return [int(v) for v in sys.stdin.readline().split()]
n = read_int()
a = read_int_line()
b = read_int_line()
q = deque()
cnt = 0
for i in range(n):
	q.append(a[i])
i = 0
mask = [0] * n
while i < n:
	if mask[b[i] - 1] != 1:
		if b[i] != q[0]:
			if mask[q[0] - 1] == 1:
				q.popleft()
				continue
			mask[b[i] - 1] = 1
			cnt += 1
		else:
			q.popleft()
			mask[b[i] - 1] = 1
		i += 1
print(cnt)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.reverse()
b.reverse()
d = dict()
for i in range(n + 1):
	d[i] = 1
cn = 0
while len(b) and len(a):
	while d[a[-1]] != 1:
		a.pop()
	if a[-1] == b[-1]:
		d[a[-1]] = 0
		d[b[-1]] = 0
		a.pop()
		b.pop()
	else:
		m = b.pop()
		d[m] = 0
		cn += 1
print(cn)


n = int(input())
s1 = list(map(int, input().split()))
s2 = list(map(int, input().split()))
d = dict()
for i in range(n):
	d[s2[i]] = i
pre_i = -1
over = 0
for i in range(n):
	x = d[s1[i]]
	if x > pre_i:
		over += x - pre_i - 1
		if x > pre_i:
			pre_i = x
print(over)


t = 1
for T in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	b = list(map(int, input().split(' ')))
	l = []
	d = {}
	cnt = 1
	for i in range(n):
		a[i] -= 1
		b[i] -= 1
		d[b[i]] = i
	for i in a:
		l.append(d[i])
	ans = 0
	mx = l[0]
	for i in range(1, n):
		if l[i] <= mx:
			ans += 1
		else:
			mx = l[i]
	print(ans)


n = int(input())
enter = [None] * n
exit = [None] * n
i = n - 1
for _ in input().split():
	enter[i] = int(_)
	i -= 1
i = n - 1
for _ in input().split():
	exit[i] = int(_)
	i -= 1
fined = [False] * (1 + n)
i = 0
s = 0
for c in exit:
	if not fined[c]:
		while enter[i] != c:
			fined[enter[i]] = True
			i += 1
		i += 1
	else:
		s += 1
print(s)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
s = 0
k = 1
c = [n for i in range(n)]
a1 = [0 for i in range(n)]
for i in range(n):
	a1[a[i] - 1] = i
for i in range(n):
	b[i] = a1[b[i] - 1]
c[n - 1] = b[n - 1]
for i in range(n - 2, -1, -1):
	c[i] = min(c[i + 1], b[i])
for i in range(n - 1):
	if b[i] > c[i + 1]:
		s += 1
print(s)


from collections import defaultdict
al = defaultdict(int)
be = defaultdict(int)
a = int(input())
z = list(map(int, input().split()))
r = list(map(int, input().split()))
for i in range(len(r)):
	al[r[i]] = i + 1
k = []
ma = []
count = 0
for i in range(len(z)):
	k.append(al[z[i]])
for i in range(len(k)):
	if i == 0:
		ma.append(k[0])
	else:
		ma.append(max(ma[-1], k[i]))
for i in range(len(k)):
	if k[i] < ma[i]:
		count = count + 1
print(count)


from sys import stdin
from collections import deque, Counter
import sys
import math
import operator
import random
from fractions import Fraction
n = int(input())
en = list(map(int, input().split()))
ex = list(map(int, input().split()))
c = Counter()
for i in range(n):
	c[ex[i]] = i
a = []
for i in en:
	a.append(c[i])
fine = 0
m = 0
for i in range(n):
	m = max(m, a[i])
	if a[i] < m:
		fine += 1
print(fine)


import sys

def rint():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline().rstrip('\n')

def oint():
	return int(input())
n = oint()
a = list(rint())
b = list(rint())
aord = [0] * n
for i in range(n):
	aord[a[i] - 1] = i
bord = [0] * n
for i in range(n):
	bord[i] = aord[b[i] - 1]
maxi = n
cnt = 0
for i in range(n - 1, -1, -1):
	if bord[i] > maxi:
		cnt += 1
	else:
		maxi = bord[i]
print(cnt)


I = input
n = int(I())
a = list(map(int, I().split(' ')))
e = list(map(int, I().split(' ')))
ans = [0 for i in range(n)]
i = 0
j = 0
while j < n:
	if ans[a[i] - 1] == 1:
		i += 1
		continue
	elif a[i] == e[j]:
		i += 1
		j += 1
	else:
		ans[e[j] - 1] = 1
		j += 1
print(sum(ans))


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = {}
for i in range(n):
	c[b[i]] = i + 1
fine = 0
counter = -1
for i in range(n):
	if c[a[i]] < counter:
		fine += 1
	counter = max(counter, c[a[i]])
print(fine)


n = int(input())
ent = [int(c) for c in input().split()][::-1]
ext = [int(c) for c in input().split()][::-1]
ans = set()
i = j = 0
while i < n:
	if ent[i] == ext[j]:
		i += 1
		j += 1
	elif ext[j] in ans:
		j += 1
	else:
		ans.add(ent[i])
		i += 1
print(len(ans))


import sys
input = sys.stdin.buffer.readline
n = int(input())
a = [int(item) for item in input().split()]
b = [int(item) for item in input().split()]
rank = [0] * n
for (i, item) in enumerate(b):
	rank[item - 1] = i
bit = [0] * (n + 1)

def bit_init():
	for i in range(1, n):
		if i + (i & -i) > n:
			continue
		bit[i + (i & -i)] += bit[i]

def bit_add(x, w):
	while x <= n:
		bit[x] += w
		x += x & -x

def bit_sum(x):
	ret = 0
	while x > 0:
		ret += bit[x]
		x -= x & -x
	return ret
ans = 0
for (i, item) in enumerate(a):
	ret = bit_sum(rank[item - 1] + 1)
	bit_add(rank[item - 1] + 1, 1)
	if i > ret:
		ans += 1
print(ans)


def arr_inp():
	return [int(x) for x in input().split()]
(n, ans, p1, p2) = (int(input()), 0, 0, 0)
(a, b) = [arr_inp() for i in range(2)]
dic = dict()
while p2 < n:
	if b[p2] != a[p1]:
		if a[p1] not in dic:
			ans += 1
			dic[b[p2]] = True
			p2 += 1
		else:
			p1 += 1
	else:
		p1 += 1
		p2 += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
m = {}
for (i, j) in enumerate(list(map(int, input().split()))):
	m[j] = i
k = 0
p = 0
s = 0
for i in a:
	p = m[i]
	if p < k:
		s += 1
	else:
		k = p
print(s)


n = int(input())
into = list(map(int, input().split()))
fact = list(map(int, input().split()))
BAD = [False] * n
i = j = 0
while i < n and j < n:
	if BAD[into[i] - 1]:
		i += 1
	else:
		if fact[j] != into[i]:
			BAD[fact[j] - 1] = True
		else:
			i += 1
		j += 1
print(BAD.count(True))


from bisect import bisect
from collections import defaultdict
from math import gcd, sqrt, ceil, inf
from collections import Counter
import sys
sys.setrecursionlimit(10 ** 9)
n = int(input())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
hash = defaultdict(int)
for (i, j) in enumerate(l2):
	hash[j] = i + 1
c = []
for i in l1:
	c.append(hash[i])
ans = 0
maxi = c[0]
for i in range(1, n):
	if c[i] < maxi:
		ans += 1
	maxi = max(maxi, c[i])
print(ans)


import sys
import math as m

def fi():
	return sys.stdin.readline()
n = int(fi())
l = list(map(int, fi().split()))
p = list(map(int, fi().split()))
d1 = dict()
d2 = dict()
for i in range(n):
	d1.setdefault(l[i], i)
	d2.setdefault(p[i], i)
prev = 0
max1 = 0
for i in range(n):
	if max1 > d2[l[i]]:
		prev += 1
	max1 = max(max1, d2[l[i]])
print(prev)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a_to_i = [0] * n
for i in range(n):
	a_to_i[a[i] - 1] = i
b_to_i = [0] * n
for i in range(n):
	b[i] = a_to_i[b[i] - 1]
	b_to_i[b[i]] = i
m = -1
ans = 0
for bi in range(n):
	index = b_to_i[bi]
	if index < m:
		ans += 1
	m = max(m, index)
print(ans)


n = int(input())
start = [int(x) for x in input().split()]
end = [int(x) for x in input().split()]
k = 0
i = j = 0
ls = len(start)
checked = set()
while j < ls:
	if start[i] not in checked:
		if start[i] != end[j]:
			while start[i] != end[j]:
				checked.add(end[j])
				j += 1
				k += 1
				if j == ls - 1:
					break
			i += 1
			j += 1
		else:
			i += 1
			j += 1
	else:
		i += 1
	if i == ls - 1:
		break
print(k)


inp = lambda cast=int: [cast(x) for x in input().split()]
printf = lambda s='', *args, **kwargs: print(str(s).format(*args), flush=True, **kwargs)
(n,) = inp()
A = inp()
B = inp()
C = [n + 1 for i in range(n + 1)]
M = {}
for (i, a) in enumerate(A):
	M[a] = i + 1
for i in range(n):
	C[n - i - 1] = min(C[n - i], M[B[n - i - 1]])
res = 0
for i in range(n):
	if M[B[i]] > C[i]:
		res += 1
print(res)


n = int(input())
arr1 = list(map(int, input().split()))[::-1]
arr2 = list(map(int, input().split()))[::-1]
myset = set()
i = n - 1
count = 0
j = n - 1
while i != -1 and j != -1:
	if arr1[i] in myset:
		i -= 1
		continue
	if arr1[i] != arr2[j]:
		myset.add(arr2[j])
		count += 1
		j -= 1
	else:
		i -= 1
		j -= 1
print(count)


n = int(input())
arr = list(map(int, input().split()))
brr = list(map(int, input().split()))
Q = 0
dic = {}
cnt = 0
for i in range(0, n):
	if dic.get(arr[i], 0) == 0:
		j = Q
		while j < n:
			if arr[i] != brr[j]:
				cnt += 1
				dic[brr[j]] = 1
				j += 1
			else:
				Q = j + 1
				j = n
print(cnt)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.reverse()
d = {}
for i in range(n):
	d[b[i]] = i
ans = 0
m = 0
last = d[a[0]]
for i in range(1, n):
	if d[a[i]] > last:
		ans += 1
	last = min(d[a[i]], last)
print(ans)


from math import sqrt

def main():
	n = int(input())
	a = [0] + list(map(int, input().split()))
	b = [0] + list(map(int, input().split()))
	d = [False] * (n + 1)
	req = a[1]
	k = 1
	ans = 0
	for i in range(1, n + 1):
		if b[i] == req:
			d[req] = True
			if k <= n:
				while d[a[k]] != False:
					k += 1
					if k > n:
						k = n - 1
						break
			req = a[k]
		else:
			d[b[i]] = True
			ans += 1
	print(ans)
main()


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
i = 0
j = 0
c = 1
dic = {}
while 1:
	if j == n:
		break
	if a[i] == b[j]:
		i += 1
		j += 1
	elif a[i] in dic.keys():
		i += 1
		continue
	else:
		dic[b[j]] = 1
		j += 1
		c += 1
print(len(dic))


n = int(input())
initial = list(map(int, input().split()))
final = list(map(int, input().split()))
d = {}
f = {}
for i in range(n):
	d[initial[i]] = i
	f[final[i]] = i
count = 0
ind = 0
arr = [-1] * n
for i in initial:
	if ind <= f[i]:
		arr[f[i]] = i
		ind = f[i]
print(arr.count(-1))


import math
import sys
from collections import defaultdict
sys.setrecursionlimit(10 ** 6)
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	input = sys.stdin.readline

def takeInput():
	return [int(x) for x in input().strip().split()]

def mergeSortInversions(arr):
	l = len(arr)
	if l == 1:
		return (arr, 0)
	else:
		a = arr[:l // 2]
		b = arr[l // 2:]
		(a, ai) = mergeSortInversions(a)
		(b, bi) = mergeSortInversions(b)
		c = []
		i = 0
		j = 0
		inversions = 0 + ai + bi
	while i < len(a) and j < len(b):
		if a[i] > b[j]:
			count[a[i]] += 1
			c.append(a[i])
			i += 1
		else:
			c.append(b[j])
			j += 1
			inversions += l - i
	c += a[i:]
	c += b[j:]
	return (c, inversions)
n = int(input())
entry = takeInput()
exit = takeInput()
order = {}
index = 1
for ele in entry:
	order[ele] = index
	index += 1
for i in range(n):
	exit[i] = order[exit[i]]
seen = [False for i in range(n + 1)]
fines = 0
count = [0 for i in range(n + 1)]
mergeSortInversions(exit)
for i in range(n + 1):
	if count[i] != 0:
		fines += 1
print(fines)


import sys
import io
import os
total = 0
failed = 0

def run(test, res):
	x = io.StringIO()
	with io.StringIO(test) as sys.stdin:
		with x as sys.stdout:
			work()
			z = x.getvalue().strip()
	sys.stdout = sys.__stdout__
	print('Passed?', z == res)
	print('Expected: ', res)
	print('Actual  : ', z)
	global total, failed
	total += 1
	failed += 1 if z != res else 0

def work():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = 0
	i = 0
	skip = set()
	for x in b:
		while a[i] in skip:
			i += 1
		if a[i] == x:
			i += 1
		else:
			c += 1
			skip.add(x)
	print(c)

def test():
	run('5\n3 5 2 1 4\n4 3 2 5 1', '2')
	run('7\n5 2 3 6 7 1 4\n2 3 6 7 1 4 5', '6')
	run('2\n1 2\n1 2', '0')
if 'LOCALTEST' in os.environ:
	test()
	print('\n Result: %s (%d total, %d failed)' % ('FAILED' if failed > 0 else 'PASSED', total, failed))
else:
	work()


import sys

class BBalancedTunnel:

	def solve(self):
		n = int(input())
		a = [int(_) for _ in input().split()]
		b = [int(_) for _ in input().split()]
		posa = [0] * (n + 1)
		posb = [0] * (n + 1)
		for i in range(n):
			posa[a[i]] = i
			posb[b[i]] = i
		ans = 0
		cur = a[0]
		for i in range(1, n):
			if posb[a[i]] < posb[cur]:
				ans += 1
			else:
				cur = a[i]
		print(ans)
solver = BBalancedTunnel()
input = sys.stdin.readline
solver.solve()


n = int(input())
enter = list(map(int, input().split()))
exit = list(map(int, input().split()))
exit_times = {}
ans = 0
for i in range(n):
	exit_times[exit[i]] = i + 1
max = 0
for i in range(n):
	if exit_times[enter[i]] > max:
		max = exit_times[enter[i]]
	else:
		ans += 1
print(ans)


n = int(input())
entry = [int(c) for c in input().strip().split(' ')]
exit_t = [int(c) for c in input().strip().split(' ')]
e_1 = [0 for c in range(n)]
e_2 = [0 for c in range(n)]
for i in range(n):
	e_1[entry[i] - 1] = i
	e_2[exit_t[i] - 1] = i
fine = [0 for c in range(n)]
for i in range(1, n):
	if e_2[entry[i] - 1] < e_2[entry[i - 1] - 1]:
		fine[i] += 1
		e_2[entry[i] - 1] = e_2[entry[i - 1] - 1]
print(sum(fine))


n = int(input())
idEnter = list(map(int, input().split()))
idExit = list(map(int, input().split()))
pos1 = {}
pos2 = {}
for i in range(n):
	pos1[idEnter[i]] = i
	pos2[idExit[i]] = i
maxIndex = 0
counter = [-1] * n
for i in idEnter:
	if maxIndex <= pos2[i]:
		counter[pos2[i]] = i
		maxIndex = pos2[i]
print(counter.count(-1))


n = int(input())
entrada_tunel = input().split()
saida_tunel = input().split()
aux = set()
i = n - 1
j = n - 1
while i >= 0 and j >= 0:
	if saida_tunel[j] in aux:
		j -= 1
	elif entrada_tunel[i] == saida_tunel[j]:
		i -= 1
		j -= 1
	else:
		aux.add(entrada_tunel[i])
		i -= 1
print(len(aux))


n = int(input())
l = list(map(int, input().split()))
m = list(map(int, input().split()))
dic = {}
for i in range(n):
	dic[l[i]] = n - i
dic2 = {}
for i in range(n):
	dic2[m[i]] = n - i
m = m[::-1]
x1 = 10 ** 20
x2 = -1
ans = 0
for i in range(n):
	x3 = m[i]
	if x2 == -1:
		x2 = x3
		x1 = dic[x3]
	else:
		if dic[x3] < x1:
			ans += 1
		if dic[x3] > x1:
			x2 = x3
			x1 = dic[x3]
print(ans)


from collections import Counter as C, defaultdict as D, deque as Q
from operator import itemgetter as I
from itertools import product as P, permutations as PERMUT
from bisect import bisect_left as BL, bisect_right as BR, insort as INSORT
from heapq import heappush as HPUSH, heappop as HPOP
from math import floor as MF, ceil as MC, gcd as MG, factorial as F, sqrt as SQRT, inf as INFINITY, log as LOG
from sys import stdin, stdout
INPUT = stdin.readline
PRINT = stdout.write
L = list
M = map

def Player1():
	print('')

def Player2():
	print('')

def Yes():
	PRINT('Yes\n')

def No():
	PRINT('No\n')

def IsPrime(n):
	for i in range(2, MC(SQRT(n)) + 1):
		if n % i == 0:
			return False
	return True

def Factors(x):
	ans = []
	for i in range(1, MC(SQRT(x)) + 1):
		if x % i == 0:
			ans.append(i)
			if x % (x // i) == 0:
				ans.append(x // i)
	return ans

def CheckPath(source, destination, g):
	visited = [0] * 101
	q = Q()
	q.append(source)
	visited[source] = 1
	while q:
		node = q.popleft()
		if node == destination:
			return 1
		for v in g[node]:
			if not visited[v]:
				q.append(v)
				visited[v] = 1
	return 0

def Sieve(n):
	prime = [1] * (n + 1)
	p = 2
	while p * p <= n:
		if prime[p]:
			for i in range(p * p, n + 1, p):
				prime[i] = 0
		p += 1
	primes = []
	for p in range(2, n + 1):
		if prime[p]:
			primes.append(p)
	return primes

def Prefix(a, n):
	p = []
	for i in range(n):
		if i == 0:
			p.append(a[0])
		else:
			p.append(p[-1] + a[i])
	return p

def Suffix(a, n):
	s = [0] * n
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			s[i] = a[i]
		else:
			s[i] = s[i + 1] + a[i]
	return s

def Spf(n):
	spf = [0 for i in range(n)]
	spf[1] = 1
	for i in range(2, n):
		spf[i] = i
	for i in range(4, n, 2):
		spf[i] = 2
	for i in range(3, MC(SQRT(n)) + 1):
		if spf[i] == i:
			for j in range(i * i, n, i):
				if spf[j] == j:
					spf[j] = i
	return spf

def DFS(g, s, visited, ans):
	visited[s] = 1
	for (u, c) in g[s]:
		if visited[u]:
			continue
		if c == ans[s]:
			if c == 1:
				ans[u] = 2
			else:
				ans[u] = 1
		else:
			ans[u] = c
		DFS(g, u, visited, ans)

def lcm(a, b):
	return a * b // MG(a, b)

def Kadane(numbers):
	max_so_far = -INFINITY
	max_ending_here = 0
	max_element = -INFINITY
	for i in range(len(numbers)):
		max_ending_here = max(max_ending_here + numbers[i], 0)
		max_so_far = max(max_ending_here, max_so_far)
		max_element = max(max_element, numbers[i])
	if max_so_far == 0:
		max_so_far = max_element
	return max_so_far

def subset(start, a, res, ans):
	res.append(ans)
	for i in range(start, 9):
		subset(i + 1, a, res, ans + [a[i]])

def Main():
	n = int(INPUT())
	a = L(M(int, INPUT().split()))
	b = L(M(int, INPUT().split()))
	ans = set()
	d = {}
	for i in range(n):
		d[b[i]] = i
	prev = -1
	for i in range(n):
		if prev == -1:
			ind = d[a[i]]
			for j in range(ind):
				ans.add(b[j])
			prev = ind
		else:
			ind = d[a[i]]
			if ind <= prev:
				continue
			for j in range(prev + 1, ind):
				ans.add(b[j])
			prev = ind
	PRINT('%d\n' % len(ans))
Main()


i1 = int(input())
ar = list(map(int, input().split()))
br = list(map(int, input().split()))
dic = {}
for i in range(i1):
	dic[br[i]] = i
count = 0
maxu = -2
for j in range(i1):
	ar[j] = dic[ar[j]]
	if ar[j] < maxu:
		count += 1
	if ar[j] > maxu:
		maxu = ar[j]
print(count)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
initial = list(map(int, input().split()))
last = list(map(int, input().split()))
dict1 = {}
for s in range(n):
	dict1[initial[s]] = s
inver = []
for s in range(n):
	val = last[s]
	inver.append(dict1[last[s]])
suf = []
minsofar = inver[-1]
for s in range(n - 1, -1, -1):
	if inver[s] < minsofar:
		minsofar = inver[s]
	suf.append(minsofar)
suf.reverse()
ans = 0
for s in range(n):
	if inver[s] > suf[s]:
		ans += 1
print(ans)


n = int(input())
s = list(map(int, input().split()))
t = list(map(int, input().split()))
c = []
fined = 0
exit = [0] * (n + 1)
for i in range(n):
	exit[t[i]] = i + 1
for i in s:
	c.append(exit[i])
m = c[0]
for i in range(1, n):
	if c[i] < m:
		fined += 1
	m = max(m, c[i])
print(fined)


sum = 0
mark = set()
n = int(input())
q1 = [int(x) for x in input().split()]
q2 = [int(x) for x in input().split()]
q1.reverse()
q2.reverse()
while len(q1) > 0:
	if len(q2) > 0:
		if q1[-1] != q2[-1] and (not q1[-1] in mark):
			sum = sum + 1
			mark.add(q2[-1])
			q2.pop()
		elif q1[-1] != q2[-1] and q1[-1] in mark:
			q1.pop()
		else:
			q1.pop()
			q2.pop()
	else:
		q1.pop()
print(sum)


n = int(input())
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
bin = [0] * 100001
for i in range(0, n):
	bin[b[i]] = i
maxi = bin[a[0]]
c = 0
for i in range(1, n):
	maxi = max(maxi, bin[a[i]])
	if maxi != bin[a[i]]:
		c += 1
print(c)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
out = 0
(i, j) = (0, 0)
m = set()
while i < n and j < n:
	if a[i] == b[j]:
		i += 1
		j += 1
	elif a[i] in m:
		i += 1
	else:
		out += 1
		m.add(b[j])
		j += 1
print(out)


import bisect
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a_dict = {}
for i in range(n):
	a_dict[a[i]] = i + 1
for i in range(n):
	b[i] = a_dict[b[i]]
b.reverse()
ans = 0
mini = n
for i in range(n):
	if mini >= b[i]:
		mini = b[i]
	else:
		ans += 1
print(ans)


n = int(input())
in_ = list(map(int, input().split()))
out_ = list(map(int, input().split()))
in_pointer = 0
out_pointer = 0
over_takers = {}
while out_pointer < n:
	while in_[in_pointer] in over_takers:
		in_pointer += 1
	if in_[in_pointer] == out_[out_pointer]:
		in_pointer += 1
		out_pointer += 1
	else:
		over_takers[out_[out_pointer]] = 0
		out_pointer += 1
print(len(over_takers))


import math
n = int(input())
a = list(map(int, input().strip().split(' ')))
b = list(map(int, input().strip().split(' ')))
c = []
for j in range(n):
	c.append(j + 1)
l = []
i = 0
j = 0
while i < n and j < n:
	if a[i] != b[j]:
		if c[a[i] - 1] != 0:
			l.append(b[j])
			c[b[j] - 1] = 0
			j += 1
		else:
			i += 1
	elif a[i] == b[j]:
		i += 1
		j += 1
print(len(l))


n = int(input())
entry = list(map(int, input().split()))
exit = list(map(int, input().split()))
for i in range(n):
	entry[i] -= 1
	exit[i] -= 1
dic = {}
for i in range(n):
	dic[exit[i]] = i
dp = [0] * n
for i in range(n):
	dp[i] = dic[entry[i]]
mx = -1
ans = 0
for i in range(n):
	if dp[i] > mx:
		mx = dp[i]
	else:
		ans += 1
print(ans)


a = int(input())
s = list(map(int, input().strip(' ').split(' ')))
k = list(map(int, input().strip(' ').split(' ')))
sd = dict()
for (i, j) in enumerate(k):
	sd[j] = i
count = 0
prev = 0
for i in range(0, a):
	if i == 0:
		index = sd[s[i]]
		count += 1
	else:
		index1 = sd[s[i]]
		if index1 > index:
			index = index1
			count += 1
print(abs(count - a))


n = int(input())
d = list(map(int, input().split()))
p = list(map(int, input().split()))
o = set()
i = 0
j = 0
while j < n:
	if d[i] in o:
		i += 1
	elif d[i] != p[j]:
		o.add(p[j])
		j += 1
	else:
		i += 1
		j += 1
print(len(o))


import os
import sys
from io import BytesIO, IOBase

def solution(arr, brr, n):
	arr.insert(0, 0)
	brr.insert(0, 0)
	pos = [0] * (n + 1)
	for i in range(1, n + 1):
		pos[brr[i]] = i
	crr = [0] * (n + 1)
	for i in range(1, n + 1):
		crr[i] = pos[arr[i]]
	(mx, res) = (-1, 0)
	for i in range(1, n + 1):
		if crr[i] > mx:
			mx = crr[i]
		else:
			res += 1
	write(res)

def main():
	n = r_int()
	arr = r_array()
	brr = r_array()
	solution(arr, brr, n)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def write(*args, end='\n'):
	for x in args:
		sys.stdout.write(str(x) + ' ')
	sys.stdout.write(end)

def r_array():
	return [int(x) for x in input().split()]

def r_int():
	return int(input())
main()


__ = input()
car_in = list(map(int, input().split()))
car_out = list(map(int, reversed(input().split())))
in_left = set(car_in)
fined_car = set()
for car in car_in:
	if not car_out:
		break
	if car in fined_car:
		continue
	while car_out and car_out[-1] != car:
		fined_car.add(car_out.pop())
	if car_out and car_out[-1] == car:
		car_out.pop()
print(len(fined_car))


n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
(skip, ans, pointer) = ([0] * (n + 1), 0, 0)
for i in range(n):
	if skip[x[i]] == 1:
		continue
	while pointer < n and y[pointer] != x[i]:
		skip[y[pointer]] = 1
		pointer += 1
		ans += 1
	pointer += 1
print(ans)


def car_n_tunnel(arr1, arr2):
	st1 = 0
	st2 = 0
	count = 0
	vis = {}
	for i in arr1:
		vis[i] = False
	count = 0
	while st1 < len(arr1) and st2 < len(arr1):
		if arr1[st1] != arr2[st2] and vis[arr1[st1]] == False and (vis[arr2[st2]] == False):
			vis[arr2[st2]] = True
			st2 += 1
			count += 1
		elif arr1[st1] == arr2[st2]:
			st1 += 1
			st2 += 1
		elif vis[arr1[st1]] == True:
			st1 += 1
		elif vis[arr2[st2]] == True:
			st2 += 1
	return count
n = int(input())
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
print(car_n_tunnel(arr1, arr2))


n = int(input())
a = [0] + list(map(int, input().split()))
b = [0] + list(map(int, input().split()))
inb = [0] * (n + 1)
c = [0] * (n + 1)
mx = -1
ans = 0
for i in range(1, n + 1):
	inb[b[i]] = i
for i in range(1, n + 1):
	c[i] = inb[a[i]]
for i in range(1, n + 1):
	if mx < c[i]:
		mx = c[i]
	else:
		ans += 1
print(ans)


n = int(input())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
l1.reverse()
l2.reverse()
o = [0] * (n + 1)
pos = [[0, 0] for i in range(n + 1)]
for i in range(n):
	pos[l1[i]][0] = i
	pos[l2[i]][1] = i
i = n - 1
was = [0] * (n + 1)
bound = n - 1
while i >= 0:
	was[l1[i]] = 1
	car = l1[i]
	pos2 = pos[car][1]
	if pos2 < i:
		bound = min(bound, i)
		while bound > pos2:
			if not was[l2[bound]]:
				o[l2[bound]] = 1
			bound -= 1
	i -= 1
print(o.count(1))


from sys import stdin
n = int(stdin.readline())
a = list(map(int, stdin.readline().rstrip().split()))
b = list(map(int, stdin.readline().rstrip().split()))
(p1, p2) = (0, 0)
over = set()
count = 0
while p1 < n and p2 < n:
	if a[p1] == b[p2]:
		p1 += 1
		p2 += 1
	elif a[p1] in over:
		p1 += 1
		continue
	else:
		over.add(b[p2])
		p2 += 1
		count += 1
		continue
print(count)


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
res = 0
fined = [0 for _ in range(len(A) + 1)]
si = 0
for a in A:
	if fined[a]:
		continue
	while B[si] != a:
		res += 1
		fined[B[si]] = 1
		si += 1
	si += 1
print(res)


import math as m
n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
i = 0
j = 0
n = len(a)
visited = [False for k in range(n + 1)]
fine = 0
while i < n and j < n:
	if visited[a[i]]:
		i += 1
		continue
	elif visited[b[j]]:
		j += 1
		continue
	elif b[j] != a[i]:
		fine += 1
		visited[b[j]] = True
		j += 1
	else:
		visited[a[i]] = True
		i += 1
		j += 1
print(fine)


from collections import defaultdict
n = int(input())
rec = {}
rec = defaultdict(lambda : 0, rec)
a = input().split()
b = input().split()
a1 = 0
b1 = 0
ans = 0
while a1 < n and b1 < n:
	if rec[a[a1]] != 0:
		a1 += 1
	elif a[a1] == b[b1]:
		rec[a[a1]] += 1
		a1 += 1
		b1 += 1
	else:
		ans += 1
		rec[b[b1]] += 1
		b1 += 1
print(ans)


I = input
n = int(I())
d = dict(zip(I().split(), range(n)))
r = 0
for x in I().split()[::-1]:
	r += d[x] > n
	n = min(n, d[x])
print(r)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
di = {}
for i in range(n):
	di[b[i]] = i + 1
c = [0] * n
for i in range(n):
	c[i] = di[a[i]]
m = 0
fined = 0
for i in range(n):
	if c[i] < m:
		fined += 1
	else:
		m = c[i]
print(fined)


import math
from collections import defaultdict, Counter, deque
INF = float('inf')

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def primeFactor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i ** 2 <= n:
		if n % i == 0:
			return i
		i += 1
	return n

def main():
	n = int(input())
	enter = list(map(int, input().split()))
	exit = list(map(int, input().split()))
	i = 0
	j = 0
	ans = 0
	m = defaultdict(int)
	while i < n and j < n:
		while m[enter[i]] == 0 and j < n and (enter[i] != exit[j]):
			m[exit[j]] = 1
			j += 1
			ans += 1
		if m[enter[i]] == 1:
			i += 1
		else:
			i += 1
			j += 1
	print(ans)
t = 1
for _ in range(t):
	main()


from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a = [0] + a
b = [0] + b
d = defaultdict(lambda : 0)
d2 = dict()
c = 0
j = 0
i = 0
while i < n and j < n:
	if d[a[i]] == -1:
		i += 1
		continue
	if a[i] != b[j]:
		c += 1
		d[b[j]] = -1
		j += 1
	else:
		i += 1
		j += 1
print(c)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b = {v: i for (i, v) in enumerate(b)}
mv = b[a[0]]
c = 0
for i in range(1, n):
	if b[a[i]] < mv:
		c += 1
	mv = max(mv, b[a[i]])
print(c)


import sys
import math
import collections
import bisect

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
for t in range(1):
	n = int(input())
	first = get_list()
	second = get_list()
	pos2 = dict()
	for i in range(n):
		pos2[second[i]] = i
	c = [0] * n
	for i in range(n):
		pos = pos2[first[i]]
		c[i] = pos + 1
	ans = 0
	maxim = 0
	for i in range(n):
		if c[i] < maxim:
			ans += 1
		maxim = max(maxim, c[i])
	print(ans)


n = int(input())
enter = list(map(int, input().split()))
out = list(map(int, input().split()))
expected = [0] * n
for i in range(0, n):
	expected[enter[i] - 1] = i
fined = 0
pos = expected[out[n - 1] - 1]
for i in range(n - 2, -1, -1):
	car = out[i] - 1
	if int(expected[car]) < pos:
		pos = expected[car]
	else:
		fined += 1
print(fined)


ans = 0
d = {}
ans = 0
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
i = n - 1
j = n - 1
d = {}
while i >= 0:
	if a[i] == b[j]:
		i -= 1
		j -= 1
	else:
		d[a[i]] = 1
		if b[j] not in d:
			i -= 1
			ans += 1
		else:
			j -= 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
pos = [0] * (n + 1)
c = [0] * n
for i in range(n):
	pos[b[i]] = i
for i in range(n):
	c[i] = pos[a[i]]
maxx = -100
cnt = 0
for i in range(n):
	if c[i] > maxx:
		maxx = c[i]
	else:
		cnt += 1
print(cnt)


import sys
at = 0
n = 0
for line in sys.stdin:
	if at == 0:
		at = at + 1
		n = int(line)
		continue
	if at == 1:
		at = at + 1
		start = [int(a) for a in line.split()]
		continue
	if at == 2:
		at = at + 1
		end = [int(a) for a in line.split()]
		break
rename = {}
for (at, i) in enumerate(start):
	rename[i] = at
for (at, i) in enumerate(end):
	end[at] = rename[i]
ans = 0
seen = [False] * n
at = 0
for car in end:
	if car > at:
		ans = ans + 1
	seen[car] = True
	while at < n and seen[at]:
		at = at + 1
print(ans)


max2 = lambda x, y: x if x > y else y
min2 = lambda x, y: x if x < y else y
N = int(input())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
a2i = [0] * N
for (i, a) in enumerate(A):
	a2i[a - 1] = i
B = [a2i[b - 1] for b in B]
c = 0
m = N
for b in reversed(B):
	if b > m:
		c += 1
	m = min2(m, b)
print(c)


n = int(input())
a = [*map(int, input().split())]
b = [*map(int, input().split())]
d = {e: i for (i, e) in enumerate(a)}
res = 0
cur = float('inf')
for e in b[::-1]:
	if d[e] > cur:
		res += 1
	else:
		cur = d[e]
print(res)


import sys
_ = input()
before_tunel = list(map(int, sys.stdin.readline().split()))
after_tunel = list(map(int, sys.stdin.readline().split()))
speedy_drivers = set()
while before_tunel:
	if after_tunel[-1] in speedy_drivers:
		after_tunel.pop()
		continue
	last_driver = before_tunel.pop()
	if after_tunel[-1] == last_driver:
		after_tunel.pop()
		continue
	if after_tunel[-1] in speedy_drivers:
		after_tunel.pop()
		continue
	speedy_drivers.add(last_driver)
print(len(speedy_drivers))


import io, sys, atexit, os
import math as ma
from sys import exit
from decimal import Decimal as dec
from itertools import permutations
from itertools import combinations

def li():
	return list(map(int, sys.stdin.readline().split()))

def num():
	return map(int, sys.stdin.readline().split())

def nu():
	return int(input())

def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	gg = find_gcd(x, y)
	return x * y // gg
mm = 1000000007
fact_powers = [0] * 19
tset = set()

def go(ind, x):
	if ind == 19:
		tset.add(x)
		return
	if x in tset:
		return
	go(ind + 1, x)
	i = 0
	for xx in fact_powers[ind]:
		p = x * xx
		if p > 1e+18:
			break
		go(ind + 1, p)

def solve():
	t = 1
	for tt in range(t):
		n = nu()
		a = li()
		b = li()
		i = 0
		j = 0
		s = set()
		cc = 0
		while i < n and j < n:
			if a[i] == b[j]:
				i += 1
				j += 1
			else:
				if a[i] in s:
					i += 1
					continue
				s.add(b[j])
				cc += 1
				j += 1
		print(cc)
solve()


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ban = [False for _ in range(n)]
j = 0
for i in range(n):
	if ban[a[i] - 1]:
		continue
	while b[j] != a[i]:
		ban[b[j] - 1] = True
		j += 1
	j += 1
print(sum(ban))


from collections import defaultdict, Counter
from math import sqrt, log10, log2, log, gcd, floor, factorial
from bisect import bisect_left, bisect_right
from itertools import combinations, combinations_with_replacement
import sys, io, os
input = sys.stdin.readline
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
mod = 10 ** 9 + 7
inf = float('inf')

def get_list():
	return [int(i) for i in input().split()]
yn = lambda a: print('YES' if a else 'NO')
ceil = lambda a, b: (a + b - 1) // b
t = 1
for i in range(t):
	n = int(input())
	l = get_list()
	m = get_list()
	iter1 = 0
	iter2 = 0
	counter = 0
	s = set()
	while iter1 < n and iter2 < n:
		if l[iter1] in s:
			iter1 += 1
		elif l[iter1] == m[iter2]:
			iter1 += 1
			iter2 += 1
		else:
			s.add(m[iter2])
			counter += 1
			iter2 += 1
	print(counter)


import sys
from heapq import heappop
from heapq import heappush
from heapq import heapify
from bisect import insort
from bisect import bisect_right
from bisect import bisect_left
from sys import stdin, stdout
from collections import defaultdict
inp = lambda : int(stdin.readline())
sip = lambda : input()
mulip = lambda : map(int, input().split())
lst = lambda : list(map(int, stdin.readline().split()))
slst = lambda : list(sip())
arr2d = lambda x: [[int(j) for j in input().split()] for i in range(x)]
odds = lambda l: len(list(filter(lambda x: x % 2 != 0, l)))
evens = lambda l: len(list(filter(lambda x: x % 2 == 0, l)))
mod = pow(10, 9) + 7
n = inp()
A = lst()
B = lst()
ida = [0] * (n + 1)
idb = [0] * (n + 1)
cnt = 0
(i, j) = (0, 0)
while i != n and j != n:
	if ida[A[i]] == 1:
		i += 1
	elif A[i] == B[j]:
		i += 1
		j += 1
	else:
		cnt += 1
		ida[B[j]] = 1
		j += 1
print(cnt)


from sys import stdin, stdout, setrecursionlimit
from collections import OrderedDict

class Tail_Recursion_Optimization:

	def __init__(self, RECURSION_LIMIT, STACK_SIZE):
		setrecursionlimit(RECURSION_LIMIT)
		threading.stack_size(STACK_SIZE)
		return None

class SOLVE:

	def solve(self):
		R = stdin.readline
		W = stdout.write
		n = int(R())
		a = [int(x) for x in R().split()]
		b = [int(x) for x in R().split()]
		c = OrderedDict()
		for i in range(n):
			c[b[i]] = i
		mx = -1
		cnt = 0
		for i in range(n):
			if c[a[i]] < mx:
				cnt += 1
			mx = max(mx, c[a[i]])
		W('%d\n' % cnt)
		return 0

def main():
	s = SOLVE()
	s.solve()
main()


n = int(input())
dic = {}
l = list(map(int, input().split()))
m = list(map(int, input().split()))
l = l[-1::-1]
m = m[-1::-1]
for i in range(n):
	dic[l[i]] = i + 1
i = 0
ans = 0
cur = 0
while i < len(m):
	if cur < dic[m[i]]:
		cur = dic[m[i]]
	else:
		ans += 1
	i += 1
print(ans)


def balance(n, enter, exit):
	overtook = set()
	i = 0
	j = 0
	while i < n and j < n:
		if exit[j] in overtook:
			j += 1
		elif enter[i] != exit[j]:
			overtook.add(enter[i])
			i += 1
		else:
			i += 1
			j += 1
	return len(overtook)
n = int(input())
enter = list(map(int, input().split()))
exit = list(map(int, input().split()))
print(balance(n, enter[::-1], exit[::-1]))


from sys import stdin, stdout
from collections import Counter
from itertools import permutations
import bisect
import math
I = lambda : map(int, stdin.readline().split())
I1 = lambda : stdin.readline()

def unique(sequence):
	seen = set()
	return [x for x in sequence if not (x in seen or seen.add(x))]
n = int(I1())
a = list(I())
b = list(I())
(i, j, c, ans) = (0, 0, 0, 0)
l = [0] * (n + 1)
while i < n and j < n:
	if a[i] == b[j]:
		i += 1
		j += 1
	elif l[a[i]] == 1:
		i += 1
	elif a[i] != b[j]:
		l[b[j]] = 1
		j += 1
		ans += 1
print(ans)


n = int(input())
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
c = [0] * (n + 1)
i = 0
j = 0
d = 0
while i < len(a):
	if c[a[i]] == 1:
		i += 1
	elif a[i] == b[j]:
		i += 1
		j += 1
	else:
		d += 1
		c[b[j]] = 1
		j += 1
print(d)


n = int(input())
In = [int(i) for i in input().split()]
Out = [int(i) for i in input().split()]
dict = {}
fine = 0
j = -1
for i in range(n):
	if In[i] not in dict:
		for k in range(j + 1, n):
			if Out[k] == In[i]:
				fine += k - j - 1
				j = k
				dict[Out[k]] = 1
				break
			else:
				dict[Out[k]] = 1
print(fine)


n = int(input())
a_i_inp = input()
a_i_arr = a_i_inp.split(' ')
a_i_arr = [int(i) for i in a_i_arr]
b_i_inp = input()
b_i_arr = b_i_inp.split(' ')
b_i_arr = [int(i) for i in b_i_arr]
arr = [[0, 0] for i in range(n)]
for i in range(n):
	arr[a_i_arr[i] - 1][0] = i
	arr[b_i_arr[i] - 1][1] = i
arr1 = [arr[i][1] - arr[i][0] for i in range(n)]
maxim = 0
count = 0
for i in a_i_arr:
	if arr[i - 1][1] >= maxim:
		maxim = arr[i - 1][1]
	else:
		count += 1
print(count)


n = int(input())
enters = list(map(int, input().split()))[::-1]
exits = list(map(int, input().split()))[::-1]
overtaker = {}
e = 0
for s in range(n):
	if enters[s] != exits[e]:
		overtaker[enters[s]] = 0
	else:
		e += 1
		while e != n and exits[e] in overtaker:
			e += 1
print(len(overtaker))


from collections import deque
a = int(input())
b = list(map(int, input().split()))
c = list(map(int, input().split()))
b = b[::-1]
k = [0 for i in range(a + 1)]
l = 0
r = -1
ans = 0
for l in range(a):
	while k[b[r]] == 1:
		r -= 1
	if c[l] == b[r]:
		r -= 1
	else:
		ans += 1
		k[c[l]] = 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = {}
for i in range(n):
	d[b[i]] = i
j = 0
t = 0
p = {}
cnt = 0
for i in range(n):
	if d[a[i]] != i and p.get(a[i], 0) == 0:
		if t == 0:
			c = i - 1
		for j in range(c + 1, d[a[i]] + 1):
			if j < d[a[i]]:
				cnt += 1
			p[b[j]] = 1
			c = j
			t = 1
	elif d[a[i]] == i and p.get(a[i], 0) == 0:
		p[a[i]] = 1
		c = i
print(cnt)


import bisect
import functools
import math
import sys
from collections import defaultdict
rt = lambda : map(int, input().split())
ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))

def main():
	n = ri()
	at_entrance = list(map(int, input().split()))
	at_exit = list(map(int, input().split()))
	used = set()
	exi_pos = 0
	fined = 0
	for ent_pos in range(n):
		car = at_entrance[ent_pos]
		if car in used:
			continue
		while at_exit[exi_pos] != car:
			if at_exit[exi_pos] not in used:
				used.add(at_exit[exi_pos])
				fined += 1
			exi_pos += 1
		exi_pos += 1
	print(fined)
main()


n = int(input())
entered = [int(x) for x in input().split()]
exited = [int(x) for x in input().split()]
entry_dict = {}
exit_dict = {}
i = 1
for e in entered:
	entry_dict[e] = i
	i += 1
i = 1
for e in exited:
	exit_dict[e] = i
	i += 1
max_exit_times = [0]
for e in entered:
	max_exit_times.append(max(max_exit_times[-1], exit_dict[e]))
ans = 0
i = 0
for e in entered:
	if exit_dict[e] < max_exit_times[i]:
		ans += 1
	i += 1
print(ans)


from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations
import sys
import bisect
import string

def I():
	return int(input())

def MI():
	return map(int, input().split())

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def StoI():
	return [ord(i) - 97 for i in input()]

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
YN = ['Yes', 'No']
mo = 10 ** 9 + 7
inf = float('inf')
input = lambda : sys.stdin.readline().rstrip()
import sys
sys.setrecursionlimit(10 ** 7)
show_flg = False
n = I()
en = LI()[::-1]
ex = LI()[::-1]
show(en, ex)
ans = 0
f = [0] * (n + 1)
while en:
	c = en.pop()
	if f[c] == 1:
		continue
	while ex:
		d = ex.pop()
		if d == c:
			break
		ans += 1
		f[d] = 1
		show(c, d, en, ex)
print(ans)
show(f)


n = int(input())
l = list(map(int, input().split()))
k = list(map(int, input().split()))
l.reverse()
k.reverse()
ans = 0
d = {}
while 1:
	if len(k) == 0 or len(l) == 0:
		break
	val = d.get(l[-1], 0)
	while val and len(l) != 0:
		l.pop(-1)
		val = d.get(l[-1], 0)
	while (len(k) != 0 and len(l) != 0) and k[-1] != l[-1]:
		ans += 1
		d[k[-1]] = 1
		k.pop(-1)
	while (len(k) != 0 and len(l) != 0) and k[-1] == l[-1]:
		k.pop(-1)
		l.pop(-1)
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = {v: i + 1 for (i, v) in enumerate(b)}
res = pos = 0
for (i, v) in enumerate(a):
	if d[v] > pos:
		pos = max(pos, d[v])
	else:
		res += 1
print(res)


n = int(input())
e = input().split()
ex = input().split()
e = [int(i) for i in e]
ez = [e[-i] for i in range(1, n + 1)]
ex = [int(i) for i in ex]
m = [0] * (n + 1)
for i in range(n):
	m[e[i]] = 1
i = n - 1
j = 0
sum = 0
while i > 0:
	if ex[j] != ez[i]:
		sum += 1
		m[ex[j]] = 0
	else:
		i = i - 1
		while m[ez[i]] == 0 and i >= 0:
			i -= 1
	j += 1
print(sum)


n = int(input())
ar = list(map(int, input().split()))
ar2 = list(map(int, input().split()))
i = 0
j = 0
ans = 0
dictionary = {}
while i < n and j < n:
	if dictionary.get(ar[i]):
		i += 1
	elif ar[i] == ar2[j]:
		i += 1
		j += 1
	else:
		ans += 1
		dictionary[ar2[j]] = 1
		j += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
pos = [0] * (max(a) + 2)
for i in range(n):
	pos[b[i]] += i
m = pos[a[0]]
cnt = 0
for i in range(1, n):
	if pos[a[i]] < m:
		cnt += 1
	if pos[a[i]] > m:
		m = pos[a[i]]
print(cnt)


n = int(input())
inputlist = list(map(int, input().split()))
dictmap = {}
index = 1
for j in inputlist:
	dictmap[j] = index
	index += 1
finallist = list(map(int, input().split()))
ranklist = []
for j in finallist:
	ranklist.append(dictmap[j])
ranklist.reverse()
count = 0
minholder = ranklist[0]
for j in range(1, n):
	if ranklist[j] < minholder:
		minholder = ranklist[j]
		continue
	else:
		count += 1
		continue
print(count)


n = int(input())
(a, b) = (list(map(int, input().split())), list(map(int, input().split())))
inv = [0 for i in range(n + 1)]
for i in range(n):
	inv[b[i]] = i
ans = 0
ma = 0
for i in range(n):
	if ma > inv[a[i]]:
		ans += 1
	else:
		ma = inv[a[i]]
print(ans)


from collections import deque as de
import math
from sys import stdin, stdout
import re
from collections import Counter as cnt
from functools import reduce
from itertools import groupby as gb
from bisect import bisect_left as bl, bisect_right as br

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

class My_stack:

	def __init__(self):
		self.data = []

	def my_push(self, x):
		return self.data.append(x)

	def my_pop(self):
		return self.data.pop()

	def my_peak(self):
		return self.data[-1]

	def my_contains(self, x):
		return self.data.count(x)

	def my_show_all(self):
		return self.data

	def isEmpty(self):
		return len(self.data) == 0
arrStack = My_stack()

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def binarytodecimal(n):
	return int(n, 2)

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def get_prime_factors(number):
	prime_factors = []
	while number % 2 == 0:
		prime_factors.append(2)
		number = number / 2
	for i in range(3, int(math.sqrt(number)) + 1, 2):
		while number % i == 0:
			prime_factors.append(int(i))
			number = number / i
	if number > 2:
		prime_factors.append(int(number))
	return prime_factors

def get_frequency(list):
	dic = {}
	for ele in list:
		if ele in dic:
			dic[ele] += 1
		else:
			dic[ele] = 1
	return dic

def Log2(x):
	return math.log10(x) / math.log10(2)

def getProduct(n):
	product = 1
	while n != 0:
		product = product * (n % 10)
		n = n // 10
	return product

def lcm(x, y):
	lcm = x * y // math.gcd(x, y)
	return lcm

def isPowerOfTwo(n):
	return math.ceil(Log2(n)) == math.floor(Log2(n))

def checkisap(list):
	d = list[1] - list[0]
	for i in range(2, len(list)):
		temp = list[i] - list[i - 1]
		if temp != d:
			return False
	return True

def ceildiv(x, y):
	return (x + y - 1) // y

def di():
	return map(int, input().split())

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))

def si():
	return list(map(str, input()))

def indict():
	dic = {}
	for (index, value) in enumerate(input().split()):
		dic[int(value)] = int(index) + 1
	return dic

def frqdict():
	dic = {}
	for (index, value) in enumerate(input().split()):
		if int(value) not in dic:
			dic[int(value)] = 1
		else:
			dic[int(value)] += 1
	return dic
n = ii()
a = li()
b = indict()
mx = b[a[0]]
ans = 0
for i in range(1, n):
	if b[a[i]] < mx:
		ans += 1
	if b[a[i]] > mx:
		mx = b[a[i]]
print(ans)


n = int(input())
incoming = list(map(int, input().split()))
outgoing = list(map(int, input().split()))
p1 = 0
p2 = 0
visited = {}
cnt = 0
while p1 < n and p2 < n:
	if incoming[p1] == outgoing[p2]:
		p1 += 1
		p2 += 1
	else:
		if incoming[p1] in visited:
			p1 += 1
			continue
		visited[outgoing[p2]] = 1
		cnt += 1
		p2 += 1
print(cnt)


n = int(input())
a = list(map(int, input().split()))
d = [0] * n
for q in range(len(a)):
	d[a[q] - 1] = q
s = list(map(int, input().split()))
for q in range(len(s)):
	s[q] = d[s[q] - 1]
f = [0] * n
ans = q1 = 0
for q in range(len(s)):
	f[s[q]] = 1
	if s[q] == q1:
		while q1 < len(f) and f[q1]:
			q1 += 1
	if s[q] > q1:
		ans += 1
print(ans)


n = int(input())
a = input().split()
b = input().split()
mapa = dict()
for i in range(n):
	car = b[i]
	mapa[car] = i
finned = 0
most_ahead = 0
for i in range(n):
	pos = mapa[a[i]]
	most_ahead = max(most_ahead, pos)
	if pos < most_ahead:
		finned += 1
print(finned)


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.reverse()
B.reverse()
stack = A
ans = 0
was = set()
i = n - 1
while stack:
	if stack[-1] not in was:
		if stack[-1] == B[i]:
			stack.pop()
		else:
			ans += 1
			was.add(B[i])
		i -= 1
	else:
		stack.pop()
print(ans)


n = int(input())
s1 = list(map(int, input().split()))
s2 = list(map(int, input().split()))
res = set()
(u1, u2) = (len(s1) - 1, len(s2) - 1)
while u1 >= 0:
	if s2[u2] not in res:
		if s1[u1] == s2[u2]:
			u1 -= 1
			u2 -= 1
		else:
			res.add(s1[u1])
			u1 -= 1
	else:
		u2 -= 1
print(len(res))


n = int(input())
t = [0] * (n + 1)
(*l,) = map(int, input().split())
(*m,) = map(int, input().split())
i = 0
j = 0
c = 0
while i < n and j < n:
	if t[l[i]]:
		i += 1
		continue
	if l[i] != m[j]:
		t[m[j]] = 1
		c += 1
	else:
		i += 1
	j += 1
print(c)


n = int(input())
a = list(map(int, input().split()))
dictt = {}
for x in range(n):
	dictt[a[x]] = x
b = list(map(int, input().split()))
bb = []
for x in b:
	bb.append(dictt[x])
bb.reverse()
fines = 0
smallest = bb[0]
largest = bb[0]
for i in range(1, n):
	if bb[i] > smallest:
		fines += 1
	if bb[i] > largest:
		largest = bb[i]
	if bb[i] < smallest:
		smallest = bb[i]
print(fines)


n = int(input())
s = input().split(' ')
a = [int(s[i]) for i in range(n)]
s = input().split(' ')
b = [int(s[i]) for i in range(n)]
f = [0] * n
for i in range(n):
	f[a[i] - 1] = i
for i in range(n):
	b[i] = f[b[i] - 1]
m = [0] * n
m[n - 1] = b[n - 1]
for i in range(n - 2, -1, -1):
	m[i] = min(m[i + 1], b[i])
r = 0
for i in range(n):
	if b[i] > m[i]:
		r += 1
print(r)


from collections import deque
cars = int(input())
visited = [0] * (cars + 1)
ans = 0
enter = [int(x) for x in input().split()]
queue = deque(enter)
exit = [int(x) for x in input().split()]
i = 0
while i < cars:
	if visited[queue[0]] == 1:
		queue.popleft()
		continue
	if queue[0] != exit[i]:
		ans += 1
		visited[exit[i]] = 1
		i += 1
	else:
		queue.popleft()
		visited[exit[i]] = 1
		i += 1
print(ans)


n = int(input())
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
a = a[::-1]
b = b[::-1]
hm = dict()
for i in range(n):
	hm[i + 1] = False
ans = 0
i = 0
j = 0
while i < n and j < n:
	while hm[b[j]]:
		j += 1
	if a[i] == b[j]:
		i += 1
		j += 1
	else:
		ans += 1
		hm[a[i]] = True
		i += 1
print(ans)


n = int(input())
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
p1 = 0
p2 = 0
res = 0
v = [0 for _ in range(0, 10 ** 5 + 1)]
while p1 < n:
	if v[a[p1]] == 0:
		v[a[p1]] = 1
		to_find = a[p1]
		while True:
			if b[p2] == to_find:
				p1 += 1
				p2 += 1
				break
			res += 1
			v[b[p2]] = 1
			p2 += 1
	else:
		p1 += 1
print(res)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [0] * n
pos = [0] * n
mx = cnt = 0
for i in range(n):
	pos[b[i] - 1] = i + 1
for i in range(n):
	c[i] = pos[a[i] - 1]
for item in c:
	if item < mx:
		cnt += 1
	mx = max(item, mx)
print(cnt)


n = int(input())
l1 = [int(i) for i in input().split()]
l2 = [int(i) for i in input().split()]
look = [0] * n
j = 0
res = 0
for i in range(n):
	if look[l1[i] - 1]:
		continue
	while j < n and l2[j] != l1[i]:
		res += 1
		look[l2[j] - 1] = 1
		j += 1
	if j == n:
		break
	else:
		look[l2[j] - 1] = 1
		j += 1
print(res)


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [0] * (n + 1)
(i, j, d) = (0, 0, 0)
while i < len(a):
	if c[a[i]] == 1:
		i += 1
	elif a[i] == b[j]:
		i += 1
		j += 1
	else:
		d += 1
		c[b[j]] = 1
		j += 1
print(d)


n = int(input())
pre = map(int, input().split())
post = map(int, input().split())
pre_map = {}
for (i, p) in enumerate(pre):
	pre_map[p] = i
post_ = []
for p in post:
	post_.append(pre_map[p])
current_min = n
ans = 0
for p in post_[::-1]:
	if current_min < p:
		ans += 1
	current_min = min(current_min, p)
print(ans)


n = int(input())
(*a,) = map(int, input().split())
(*b,) = map(int, input().split())
a.reverse()
b.reverse()
s = set()
i = j = cnt = 0
while i < n and j < n:
	if b[j] in s:
		j += 1
		continue
	s.add(a[i])
	if a[i] != b[j]:
		cnt += 1
	else:
		j += 1
	i += 1
print(cnt)


from sys import stdin as sin

def aint():
	return int(input())

def amap():
	return map(int, sin.readline().split())

def alist():
	return list(map(int, sin.readline().split()))

def astr():
	return input()
n = aint()
a = alist()
b = alist()
ans = 0
i = 0
j = 0
d = {}
g = 0
while i < n:
	if d.get(a[i], None) == None:
		g += 1
		while j < n:
			d[b[j]] = True
			if b[j] == a[i]:
				break
			j += 1
		ans = j - g + 1
	i += 1
print(ans)


import sys, math, bisect

def input():
	return sys.stdin.readline()[:-1]

def main():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split())) + [-1]
	u = [0 for k in range(n + 1)]
	i = 0
	j = 0
	f = 0
	ans = 0
	while j < n and i < n:
		u[a[i]] = 1
		while a[i] != b[j]:
			u[b[j]] = 1
			j += 1
			if j == n:
				break
		ans += j - f
		f = j + 1
		while u[a[i]] == 1:
			i += 1
			if i == n:
				break
	print(ans)
main()


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
pos = [0 for i in range(len(a))]
c = [0 for i in range(len(a))]
for i in range(len(a)):
	pos[a[i] - 1] = i
for i in range(len(b)):
	c[pos[b[i] - 1]] = i
max1 = -1
res = 0
for i in range(len(c)):
	if c[i] < max1:
		res += 1
	max1 = max(max1, c[i])
print(res)


def f(a, b):
	l = 0
	exit = [0] * (n + 1)
	for i in range(len(a)):
		exit[b[i]] = i
	cmax = 0
	cnt = 0
	for i in range(len(a)):
		if exit[a[i]] < cmax:
			cnt += 1
		cmax = max(exit[a[i]], cmax)
	return cnt
n = int(input())
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
print(f(a, b))


from collections import defaultdict, deque
from itertools import permutations
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
from copy import deepcopy
from random import randint
int_input = lambda : int(stdin.readline())
string_input = lambda : stdin.readline()
multi_int_input = lambda : map(int, stdin.readline().split())
multi_input = lambda : stdin.readline().split()
list_input = lambda : list(map(int, stdin.readline().split()))
string_list_input = lambda : list(string_input())
MOD = pow(10, 9) + 7
n = int_input()
input_sequence = list_input()
output_sequence = list_input()
fines = 0
state_car = {}
for i in range(1, n + 1):
	state_car[i] = 0
k = 0
l = 0
while k < len(input_sequence):
	if state_car[input_sequence[k]] == 1:
		k += 1
	elif input_sequence[k] != output_sequence[l]:
		state_car[output_sequence[l]] = 1
		l += 1
		fines += 1
	else:
		l += 1
		k += 1
print(fines)


import sys
import math
input = sys.stdin.readline
sys.setrecursionlimit(100000)

def getN():
	return int(input())

def getList():
	return list(map(int, input().split()))
n = getN()
anums = getList()
bnums = getList()
through = [0 for i in range(n + 1)]
aidx = -1
bidx = -1
ans = 0
while True:
	if bidx == -(n + 1) or aidx == -(n + 1):
		break
	(at, bt) = (anums[aidx], bnums[bidx])
	if at != bt:
		if not through[bt]:
			ans += 1
			through[at] = 1
			aidx -= 1
		else:
			bidx -= 1
	else:
		through[at] = 1
		aidx -= 1
		bidx -= 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
up = 0
answer = n
di = [0] * (n + 1)
for i in range(n):
	di[b[i]] = i + 1
for i in range(n):
	position = di[a[i]]
	if position > up:
		up = position
		answer -= 1
print(answer)


from sys import stdin, stdout

def ii():
	return int(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def li():
	return list(mi())

def si():
	return stdin.readline()
n = ii()
l1 = li()
l2 = li()
l3 = [0] * (n + 1)
l4 = []
for i in range(n):
	l3[l2[i]] = i + 1
for i in l1:
	l4.append(l3[i])
ans = 0
max1 = l4[0]
for i in l4[1:]:
	if i > max1:
		max1 = i
	else:
		ans += 1
print(ans)


def solve(n, a, b):
	r = 0
	i = 0
	t = b.copy()
	m = {}
	while i < len(a) and t:
		if a[i] == t[0]:
			i += 1
			t.pop(0)
		elif a[i] in m:
			i += 1
		else:
			m[t.pop(0)] = i
			r += 1
	return r
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(solve(n, a, b))


def main():
	n = int(input())
	a = list(map(lambda x: int(x) - 1, input().split()))
	b = list(map(lambda x: int(x) - 1, input().split()))
	pos = [0 for _ in range(n)]
	for i in range(n):
		pos[b[i]] = i
	c = [0 for _ in range(n)]
	for i in range(n):
		c[i] = pos[a[i]]
	(m, ans) = (-1, 0)
	for i in range(n):
		if c[i] > m:
			m = c[i]
		else:
			ans += 1
	print(ans)
main()


elem = int(input())
ar = []
for x in input().split(' '):
	ar.append(int(x))
ar2 = []
for x in input().split(' '):
	ar2.append(int(x))
curr = ar[0]
ar.pop(0)
goneCars = set()
count = 0
check = 0
for x in ar2:
	if x == curr:
		if len(ar) == 0:
			break
		while True:
			if ar[0] in goneCars:
				ar.pop(0)
				if len(ar) == 0:
					check = 1
					break
			else:
				curr = ar[0]
				ar.pop(0)
				break
	else:
		count = count + 1
		goneCars.add(x)
	if check == 1:
		break
print(count)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cnt = 0
c = set()
(i, j) = (n - 1, n - 1)
while i > -1 and j > -1:
	if b[j] in c:
		j -= 1
		continue
	if a[i] != b[j]:
		c.add(a[i])
		i -= 1
		cnt += 1
		continue
	i -= 1
	j -= 1
print(cnt)


n = int(input())
import copy
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
count = 0
j = 0
i = 0
d = {}
for i in range(n):
	d[a[i]] = i
i = 0
while i < n:
	if a[j] == -1:
		j += 1
		continue
	if a[j] != b[i] and a[j] != -1:
		a[d[b[i]]] = -1
		count += 1
	else:
		j += 1
	i += 1
print(count)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
i = 0
j = 0
ans = 0
s = set()
while i < len(a):
	if a[i] in s:
		i += 1
		continue
	while b[j] != a[i]:
		s.add(b[j])
		ans += 1
		j += 1
	i += 1
	j += 1
print(ans)


n = int(input())
enter = list(map(int, input().split()))
exit = list(map(int, input().split()))
(i, j) = (0, 0)
vis = set()
res = 0
while i < n and j < n:
	if enter[i] in vis:
		i += 1
	elif enter[i] == exit[j]:
		i += 1
		j += 1
	else:
		vis.add(exit[j])
		res += 1
		j += 1
print(res)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
danhdau = [0] * (n + 1)
k = 0
i = 0
res = 0
while i < n:
	if danhdau[a[i]] == 1:
		i += 1
	elif a[i] == b[k]:
		i += 1
		k += 1
	else:
		danhdau[b[k]] = 1
		k += 1
		res += 1
print(res)


n = int(input())
entry = [int(k) for k in input().split()]
exit = [int(k) for k in input().split()]
ans = 0
dic = {}
(i, j) = (0, 0)
while True:
	if i >= n or j >= n:
		break
	if entry[i] == exit[j]:
		i += 1
		j += 1
		continue
	if entry[i] in dic:
		i += 1
		continue
	dic[exit[j]] = 1
	j += 1
	ans += 1
print(ans)


import os.path
import sys
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def inp():
	return sys.stdin.readline().strip()
n = int(input())
A = [int(x) - 1 for x in input().split()]
B = [int(x) - 1 for x in input().split()]
dis = [0] * n
for i in range(n):
	dis[A[i]] = i
B = [dis[b] for b in B]
fine = 0
over = n
for i in reversed(range(n)):
	if over < B[i]:
		fine += 1
	over = min(over, B[i])
print(fine)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
memo = {}
for i in range(n):
	memo[a[i]] = i
ans = [0] * n
for i in range(n):
	ans[i] = memo[b[i]]
min_num = 10 ** 9
cnt = 0
for i in range(n)[::-1]:
	if ans[i] > min_num:
		cnt += 1
	min_num = min(min_num, ans[i])
print(cnt)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
count = 0
ans = 0
j = -1
i = 0
d = {}
fine = 0
for i in range(n):
	if a[i] not in d:
		for k in range(j + 1, n):
			if b[k] == a[i]:
				fine += k - j - 1
				j = k
				d[b[k]] = 1
				break
			else:
				d[b[k]] = 1
print(fine)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
h = [0] * (n + 1)
(i, j) = (0, 0)
fine = 0
while j < n and i < n:
	if h[a[i]] == 1:
		i += 1
	elif a[i] != b[j]:
		fine += 1
		h[b[j]] += 1
		j += 1
	else:
		h[b[j]] += 1
		j += 1
		i += 1
print(fine)


n = int(input())
ch1 = input()
ch2 = input()
L1 = [int(i) for i in ch1.split()]
L2 = [int(i) for i in ch2.split()]
d = {}
for i in range(n):
	d[L2[i]] = i
x = 0
nb = 0
for i in range(n):
	if d[L1[i]] - i >= x:
		x = d[L1[i]] - i
	else:
		nb += 1
		x -= 1
print(nb)


z = int(input())
x = list(map(int, input().split()))
c = list(map(int, input().split()))
lst = [0] * z
a = 0
ans = 0
i = 0
while i < z:
	if lst[x[a] - 1] != 0:
		a += 1
		continue
	lst[c[i] - 1] = 1
	if x[a] != c[i]:
		ans += 1
	else:
		a += 1
	i += 1
print(ans)


from sys import stdin, setrecursionlimit
setrecursionlimit(10 ** 7)

def iin():
	return int(stdin.readline())

def lin():
	return list(map(int, stdin.readline().split()))

def main():
	n = iin()
	e = lin()
	ex = lin()
	fine = set()
	(i, j) = (0, 0)
	while i < n and j < n:
		if e[i] == ex[j]:
			i += 1
			j += 1
		else:
			while i < n and e[i] in fine:
				i += 1
			while j < n and ex[j] != e[i]:
				fine.add(ex[j])
				j += 1
	print(len(fine))
main()


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cnt = 0
k = 0
c = set()
t = 0
while k < n and t < n:
	if b[k] == a[t]:
		k += 1
		t += 1
		while t < n and a[t] in c:
			t += 1
	else:
		cnt += 1
		c.add(b[k])
		k += 1
print(cnt)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = 0
d = set(b)
j = 0
for i in a:
	if i in d:
		while j < n and b[j] != i:
			c += 1
			d.remove(b[j])
			j += 1
		j += 1
		d.remove(i)
print(c)


def funcao(listaE, listaS):
	ult = set()
	i = 0
	j = 0
	while j < len(listaS) or i < len(listaE):
		if listaE[i] not in ult:
			if listaE[i] == listaS[j]:
				i += 1
			else:
				ult.add(listaS[j])
			j += 1
		else:
			i += 1
	return ult
input()
listaE = input().split()
listaS = input().split()
print(len(funcao(listaE, listaS)))


import sys
import os
from io import IOBase, BytesIO

def main():
	n = int(input())
	a = get_array()
	b = get_array()
	d = {}
	for i in range(n):
		d[a[i]] = i
	arr = []
	for i in range(n):
		arr.append(d[b[i]])
	curr = 0
	ans = 0
	chk = {}
	for i in range(n):
		if arr[i] != curr:
			ans += 1
			chk[arr[i]] = 1
		else:
			chk[curr] = 1
			while curr in chk:
				curr += 1
	print(ans)
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		py2 = round(0.5)
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2 == 1:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline().strip()
main()


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a = a[::-1]
b = b[::-1]
cou = 0
x = [0] * n
while len(a) and len(b):
	if b[-1] == a[-1]:
		x[b.pop() - 1] += 1
		a.pop()
	elif x[a[-1] - 1] == 0:
		x[b.pop() - 1] = 1
		cou += 1
	else:
		a.pop()
print(cou)


def find(a, b):
	j = 0
	while b <= len(Last) - 1:
		if check[Last[b] - 1] == True:
			return j
		else:
			check[Last[b] - 1] = True
			j += 1
		b += 1
number_car = int(input())
firstseq = input()
First = [int(n) for n in firstseq.split()]
lastseq = input()
Last = [int(n) for n in lastseq.split()]
check = [False] * number_car
c = 0
b = 0
i = 0
while i < number_car - 1:
	if check[First[i] - 1] == True:
		i = i
	else:
		check[First[i] - 1] = True
		z = find(First[i], b)
		c += z
		b += z + 1
	i += 1
print(c)


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
from math import *
(n,) = I()
ans = 0
a = I()
b = I()
c = [0] * (n + 1)
d = [0] * (n + 1)
for i in range(n):
	c[a[i]] = i + 1
	d[b[i]] = i + 1
for i in range(1, n):
	s = d[a[i]]
	f = d[a[i - 1]]
	if s < f:
		d[a[i]] = f
		ans += 1
print(ans)


from collections import deque

def get_num_fined_cars(num_of_cars, car_enters, car_exits):
	car_enter_orders = [0] * num_of_cars
	for (index, car) in enumerate(car_enters):
		car_enter_orders[car] = index
	car_exit_orders = [0] * num_of_cars
	for (index, car) in enumerate(car_exits):
		car_exit_orders[car_enter_orders[car]] = index
	max_exit_order = car_exit_orders[0]
	num_of_fines = 0
	for car_exit_order in car_exit_orders[1:]:
		if car_exit_order < max_exit_order:
			num_of_fines += 1
		max_exit_order = max(car_exit_order, max_exit_order)
	return num_of_fines

def solve():
	num_of_cars = int(input())
	first_line = input()
	car_enters = [int(num) - 1 for num in first_line.split()]
	second_line = input()
	car_exits = [int(num) - 1 for num in second_line.split()]
	num_of_fines = get_num_fined_cars(num_of_cars, car_enters, car_exits)
	print(num_of_fines)
solve()


import sys
input = lambda : sys.stdin.readline().strip('\r\n')
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = {value: index + 1 for (index, value) in enumerate(b)}
(result, pos) = (0, 0)
for (i, v) in enumerate(a):
	if d[v] > pos:
		pos = d[v]
	else:
		result += 1
print(result)


n = int(input())
entering = list(map(int, input().split()))
exiting = list(map(int, input().split()))
i = 0
j = 0
fined = set()
while n > 0:
	if entering[i] in fined:
		i += 1
		continue
	if j == len(exiting) - 1:
		if entering[i] == exiting[j]:
			i += 1
		else:
			fined.add(entering[i])
	elif entering[i] == exiting[j]:
		i += 1
		j += 1
	else:
		fined.add(exiting[j])
		j += 1
	n -= 1
print(len(fined))


n = int(input())
i = [int(x) for x in input().split()]
o = [int(x) for x in input().split()]
x = 0
y = 0
s = set()
ans = 0
while x < n and y < n:
	if i[x] == o[y]:
		x += 1
		y += 1
	elif i[x] in s:
		x += 1
	else:
		s.add(o[y])
		y += 1
		ans += 1
print(ans)


n = int(input())
arr = [0] * n
a = list(map(int, input().split()))[::-1]
b = list(map(int, input().split()))[::-1]
for i in range(n):
	arr[a[i] - 1] = i
for i in range(n):
	b[i] = arr[b[i] - 1]
check = -1
ans = 0
for i in b:
	if check > i:
		ans += 1
	check = max(check, i)
print(ans)


n = int(input())
ans = 0
a = list(map(int, input().split()))
b = list(map(int, input().split()))
order = {}
c = [None] * n
for (i, x) in enumerate(b):
	order[x] = i
for (i, x) in enumerate(a):
	c[i] = order[x]
maxi = -1
for (i, x) in enumerate(c):
	if i != 0:
		if x > maxi:
			maxi = x
		else:
			ans += 1
	else:
		maxi = x
print(ans)


import sys, math, heapq, copy
from collections import defaultdict, deque
from bisect import bisect_left, bisect_right
from functools import cmp_to_key
from itertools import permutations, combinations, combinations_with_replacement
mod = 1000000007

def get_int():
	return int(sys.stdin.readline().strip())

def get_str():
	return sys.stdin.readline().strip()

def get_float():
	return float(sys.stdin.readline().strip())

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_strs():
	return map(str, sys.stdin.readline().strip().split())

def get_floats():
	return map(float, sys.stdin.readline().strip().split())

def list_ints():
	return list(map(int, sys.stdin.readline().strip().split()))

def list_strs():
	return list(map(str, sys.stdin.readline().strip().split()))

def list_floats():
	return list(map(float, sys.stdin.readline().strip().split()))
n = get_int()
In = list_ints()
Out = list_ints()
i = j = cnt = 0
d = defaultdict(bool)
while i < n and j < n:
	if In[i] == Out[j]:
		i += 1
		j += 1
	elif d[In[i]] == True:
		i += 1
	else:
		d[Out[j]] = True
		j += 1
		cnt += 1
print(cnt)


from collections import deque
n = int(input())
a = [int(x) for x in input().split()]
b = deque([int(x) for x in input().split()])
saiu = set([])
i = 0
while i < n and len(b) > 0:
	if a[i] in saiu:
		i += 1
		continue
	if b[0] != a[i]:
		saiu.add(b.popleft())
	else:
		b.popleft()
		i += 1
print(len(saiu))


import sys
from collections import defaultdict, deque
from math import sqrt

def input():
	return sys.stdin.readline()[:-1]
n = int(input())
a = deque([int(x) for x in input().split()])
b = deque([int(x) for x in input().split()])
a.reverse()
b.reverse()
was = set()
while a:
	if a[0] == b[0]:
		a.popleft()
		b.popleft()
	elif b[0] in was:
		b.popleft()
	else:
		x = a.popleft()
		was.add(x)
print(len(was))


n = int(input())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
d = {}
for i in range(n):
	d[l2[i]] = 0
c = 0
q = 0
s = l1[q]
q = q + 1
for i in range(n):
	if q > n - 1:
		break
	if d[l2[i]] != 1:
		if l2[i] != s:
			c = c + 1
			d[l2[i]] = 1
		else:
			while d[l1[q]] == 1:
				q = q + 1
				if q > n - 1:
					break
			if q > n - 1:
				break
			s = l1[q]
			q = q + 1
print(c)


n = int(input())
enter = list(map(int, input().split()))
exited = list(map(int, input().split()))
o = set()
i = 0
j = 0
while j < n:
	if enter[i] in o:
		i += 1
	elif enter[i] != exited[j]:
		o.add(exited[j])
		j += 1
	else:
		i += 1
		j += 1
print(len(o))


n = int(input())
arr = list(map(int, input().split()))
brr = list(map(int, input().split()))
diff = []
visited = set([brr[0]])
if arr[0] == brr[0]:
	diff.append(1)
else:
	diff.append(0)
for i in range(1, n):
	visited.add(brr[i])
	diff_cand = diff[i - 1]
	while arr[diff_cand] in visited:
		diff_cand += 1
		if diff_cand == n:
			break
	diff.append(diff_cand)
elt_to_index = {}
for i in range(n):
	elt_to_index[arr[i]] = i
ans = 0
for i in range(n):
	elt = brr[i]
	elt_a = elt_to_index[elt]
	diff_bound = diff[i]
	if diff_bound < elt_a:
		ans += 1
print(ans)


n = int(input())
a = [int(i) - 1 for i in input().split()]
b = [int(i) - 1 for i in input().split()]
pos = [0 for i in range(n)]
c = [0 for i in range(n)]
for i in range(n):
	pos[b[i]] = i
for i in range(n):
	c[i] = pos[a[i]]
m = c[0]
ans = 0
for i in range(1, n):
	if c[i] < m:
		ans += 1
	else:
		m = c[i]
print(ans)


import sys
import string
import math
from collections import defaultdict
from functools import lru_cache
from collections import Counter
from fractions import Fraction

def mi(s):
	return map(int, s.strip().split())

def lmi(s):
	return list(mi(s))

def tmi(s):
	return tuple(mi(s))

def mf(f, s):
	return map(f, s)

def lmf(f, s):
	return list(mf(f, s))

def js(lst):
	return ' '.join((str(d) for d in lst))

def jsns(lst):
	return ''.join((str(d) for d in lst))

def line():
	return sys.stdin.readline().strip()

def linesp():
	return line().split()

def iline():
	return int(line())

def mat(n):
	matr = []
	for _ in range(n):
		matr.append(linesp())
	return matr

def matns(n):
	mat = []
	for _ in range(n):
		mat.append([c for c in line()])
	return mat

def mati(n):
	mat = []
	for _ in range(n):
		mat.append(lmi(line()))
	return mat

def pmat(mat):
	for row in mat:
		print(js(row))

def main():
	line()
	A = lmi(line())
	B = lmi(line())
	i = 0
	j = 0
	done = set()
	count = 0
	while j < len(B):
		if A[i] in done:
			i += 1
		elif A[i] == B[j]:
			i += 1
			j += 1
		elif A[i] != B[j]:
			count += 1
			done.add(B[j])
			j += 1
	print(count)
main()


def main():
	n = int(input())
	enter = list(map(int, input().rstrip(' ').split(' ')))
	exit = list(map(int, input().rstrip(' ').split(' ')))
	d = {}
	res = 0
	li = []
	for i in range(len(enter)):
		d[enter[i]] = i
	for j in range(len(exit)):
		li.append(d[exit[j]])
	mini = li[len(li) - 1]
	for i in range(len(li) - 2, -1, -1):
		if li[i] > mini:
			res += 1
		else:
			mini = li[i]
	print(res)
main()


def ii():
	a = int(input())
	return a

def ai():
	a = list(map(int, input().split()))
	return a

def mi():
	a = map(int, input().split())
	return a
n = ii()
ans = 0
enter = ai()
exit = ai()
fined = [False] * 1000001
for i in range(n - 1, -1, -1):
	en_car = enter[i]
	chek = fined[exit[-1]]
	while chek:
		exit.pop()
		chek = fined[exit[-1]]
	ex_car = exit[-1]
	if en_car == ex_car:
		exit.pop()
	else:
		fined[en_car] = True
		ans += 1
print(ans)


from collections import defaultdict as dc
from collections import deque as dq
from bisect import bisect_left, bisect_right, insort_left
import sys
import math
mod = 10 ** 9 + 7

def bs(a, x):
	i = bisect_left(a, x)
	if i != len(a) and a[i] == x:
		return i
	else:
		return -1

def inp():
	p = sys.stdin.readline()
	return p

def line():
	p = list(map(int, inp().split()))
	return p
n = int(inp())
a = line()
b = line()
c = 0
b = dq(b)
q = dc(int)
i = 0
while i < n and len(b) > 0:
	p = b[0]
	b.popleft()
	q[p] = 1
	if a[i] == p:
		i += 1
	elif q[a[i]] == 1:
		i += 1
		b.appendleft(p)
	else:
		c += 1
print(c)


import array as arr
n = int(input())
ar = list(map(int, input().split()))
br = list(map(int, input().split()))
cr = dict()
for i in range(0, n):
	cr[br[i]] = i
last = -1
cnt = 0
for i in range(0, n):
	it = cr[ar[i]]
	if last < it:
		last = it
		cnt = cnt + 1
print(n - cnt)


from io import BytesIO, IOBase
import os
import sys
from itertools import *
from collections import *

def solve():
	n = inp()
	a = li()
	b = li()
	b = {v: k for (k, v) in enumerate(b)}
	ans = 0
	minn = b[a[0]]
	for i in range(1, n):
		if b[a[i]] < minn:
			ans += 1
		minn = max(minn, b[a[i]])
	pr(ans)

def main():
	T = 1
	for _ in range(T):
		solve()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def li():
	return list(map(int, sys.stdin.readline().split()))

def mp():
	return map(int, sys.stdin.readline().split())

def inp():
	return int(sys.stdin.readline())

def pr(n):
	return sys.stdout.write(str(n) + '\n')

def prl(n):
	return sys.stdout.write(str(n) + ' ')
main()


I = input
I()
a = I().split()
d = dict(zip(I().split(), range(7 << 14)))
r = m = 0
for x in a:
	r += d[x] < m
	m = max(m, d[x])
print(r)


from collections import deque
from typing import DefaultDict
inf = float('inf')
en = lambda x: list(enumerate(x))
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())
n = ii()
arr = rr()[::-1]
brr = rr()[::-1]
ok = DefaultDict(lambda : 0)
ans = 0
while arr:
	x = arr.pop()
	if ok[x]:
		continue
	while 1:
		y = brr.pop()
		if y == x:
			break
		ok[y] = 1
		ans += 1
print(ans)


from sys import stdin

def scal(typ=int):
	return typ(stdin.readline())

def vec(typ=int):
	if isinstance(typ, list):
		inp = stdin.readline().split()
		return [typ[i](inp[i]) for i in range(len(inp))]
	else:
		return list(map(typ, stdin.readline().split()))
(n, a, b) = (scal(), vec(), vec())
o = {v: k for (k, v) in enumerate(b, 1)}
b = [o[e] for e in a]
(res, m_so_far) = (0, 0)
for e in b:
	if e < m_so_far:
		res += 1
	else:
		m_so_far = e
print(res)


import sys
import collections
input = sys.stdin.readline

def rints():
	return map(int, input().strip().split())

def rint():
	return int(input().strip())

def rintas():
	return [int(i) for i in input().strip().split()]

def gcd(a, b):
	if a == 0:
		return b
	return gcd(a % b, a)
n = rint()
a = rintas()
b = rintas()
d = [i for i in range(n + 1)]
for (i, v) in enumerate(b):
	d[v] = i
(cur, maxval) = (0, -1)
ans = 0
(i, j) = (0, 0)
for i in range(n):
	if d[a[i]] > maxval:
		maxval = d[a[i]]
	else:
		ans += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
visited = [0] * (n + 1)
i = 0
j = 0
ans = 0
while j < n and i < n:
	if visited[a[i]]:
		i += 1
	elif a[i] != b[j]:
		ans += 1
		visited[b[j]] = 1
		j += 1
	else:
		i += 1
		j += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [0] * n
m = {}
for i in range(n):
	m[a[i]] = i + 1
for i in range(n):
	c[i] = m[b[i]]
count = 0
minn = c[-1]
for i in range(n - 1, -1, -1):
	if minn < c[i]:
		count += 1
	minn = min(minn, c[i])
print(count)


n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
dic = {}
arr = 0
maxim = 0
for i in range(n):
	dic[b[i]] = i
for item in a:
	if dic[item] < maxim:
		arr += 1
	maxim = max(maxim, dic[item])
print(arr)


import sys
n = int(sys.stdin.readline())
a = [int(i) for i in sys.stdin.readline().split()]
b = [int(i) for i in sys.stdin.readline().split()]
x = 0
y = 0
fine_car = [0] * (n + 1)
while x < n and y < n:
	if fine_car[a[x]] == 1:
		x += 1
	elif a[x] == b[y]:
		x += 1
		y += 1
	else:
		fine_car[b[y]] = 1
		y += 1
count = 0
for h in range(n + 1):
	count += fine_car[h]
print(count)


_ = int(input())
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
bhash = [0] * len(a)
i = 0
j = 0
p = 0
while j < len(a):
	if bhash[a[i] - 1] == 1:
		i += 1
		continue
	bhash[b[j] - 1] = 1
	if a[i] == b[j]:
		i += 1
	else:
		p += 1
	j += 1
print(p)


n = int(input())
d = dict()
l = [int(j) for j in input().split()]
l2 = [int(j) for j in input().split()]
f = 0
c = 0
for i in range(n):
	t = l[i]
	if f == n:
		break
	if not t in d:
		while l2[c] != t and c < n:
			k = l2[c]
			if not k in d:
				d[k] = 1
				f += 1
			c += 1
		c += 1
print(f)


import sys, math, string, bisect
input = sys.stdin.readline
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
n = I()
l = L()
g = L()
d = [1] * (n + 1)
i = 0
start = 0
c = 0
while i < n and start < n:
	if g[i] != l[start] and d[l[start]] == 1:
		c += 1
		d[g[i]] = 0
		i += 1
	elif g[i] == l[start]:
		d[g[i]] = 0
		i += 1
		start += 1
	else:
		start += 1
print(c)


n = int(input())
enter = [int(x) for x in input().split()]
exite = [int(x) for x in input().split()]
visited = [False] * (n + 10)
enterPointer = 0
exitPointer = 0
Fined = 0
while exitPointer != n or enterPointer != n:
	if visited[enter[enterPointer]] == True:
		enterPointer += 1
		continue
	if exite[exitPointer] != enter[enterPointer]:
		visited[exite[exitPointer]] = True
		Fined += 1
		exitPointer += 1
	else:
		visited[enter[enterPointer]] = True
		enterPointer += 1
		exitPointer += 1
print(Fined)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
pos = [-1] * n
c = [-1] * n
count = 0
for i in range(n):
	pos[b[i] - 1] = i
for i in range(n):
	c[i] = pos[a[i] - 1]
mx = -1
for i in range(n):
	if c[i] > mx:
		mx = c[i]
	else:
		count += 1
print(count)


n = int(input())
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
op = [0] * (n + 1)
i = 0
j = 0
while i < n and j < n:
	if a[i] == b[j]:
		i += 1
		while i < n and op[a[i]] == 1:
			i += 1
		j += 1
	else:
		op[b[j]] = 1
		j += 1
print(sum(op))


import math
from math import gcd, sqrt, floor, factorial, ceil
from bisect import bisect_left, bisect_right
import bisect
import sys
from sys import stdin, stdout
import os
sys.setrecursionlimit(pow(10, 7))
import collections
from collections import defaultdict, Counter
from statistics import median
from queue import Queue
inf = float('inf')
n = int(input())
i = list(map(int, input().split()))
o = list(map(int, input().split()))
it1 = 0
it2 = 0
dict = defaultdict(int)
count = 0
while it1 < n and it2 < n:
	if dict[i[it1]]:
		it1 += 1
		continue
	if i[it1] == o[it2]:
		it1 += 1
		it2 += 1
	else:
		dict[o[it2]] = 1
		count += 1
		it2 += 1
print(count)


n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
ov = 0
x = [0] * (n + 1)
for i in range(n):
	x[a[i]] = i
q = 0
an = 0
for i in range(n):
	if b[i] != a[q]:
		a[x[b[i]]] = 0
		an += 1
	else:
		q += 1
		while q < n and a[q] == 0:
			q += 1
print(an)


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
bad = [0] * (n + 1)
pos = [0] * (n + 1)
for i in range(n):
	pos[b[i]] = i

def msort(ll):
	lll = len(ll)
	mid = lll // 2
	if mid == 0:
		return ll
	l = msort(ll[:mid])
	r = msort(ll[mid:])
	li = mid - 1
	ri = lll - mid - 1
	ans = [0] * lll
	lmoved = False
	for i in range(lll - 1, -1, -1):
		if ri == -1 or (li != -1 and pos[l[li]] > pos[r[ri]]):
			ans[i] = l[li]
			li -= 1
			if not lmoved:
				lmoved = True
		else:
			ans[i] = r[ri]
			ri -= 1
			if lmoved:
				bad[ans[i]] = 1
	return ans
msort(a)
print(sum(bad))


N = int(input())
L1 = list(map(int, input().split()))
L2 = list(map(int, input().split()))
FLAG = [0] * (N + 1)
index = 0
count = 0
i = 0
while i < N and index < N:
	if L1[i] != L2[index]:
		if FLAG[L1[i]] == 0:
			count += 1
			FLAG[L2[index]] = 1
			index += 1
		else:
			i += 1
	else:
		i += 1
		index += 1
print(count)


n = int(input())
ar1 = list(map(int, input().split()))
ar2 = list(map(int, input().split()))
ar1.reverse()
ar2.reverse()
ind1 = [0] * n
ind2 = [0] * n
for i in range(n):
	ar1[i] -= 1
	ind1[ar1[i]] = i
for i in range(n):
	ar2[i] -= 1
	ind2[ar2[i]] = i
car = ar1[-1]
ans = 0
for i in range(n - 2, -1, -1):
	if ind2[car] < ind2[ar1[i]]:
		ans += 1
	else:
		car = ar1[i]
print(ans)


n = int(input())
f = 1
init = list(map(int, input().split()))
out = list(map(int, input().split()))
s = set()
ans = 0
index = 0
i = 0
while index != n - 1:
	if out[i] != init[index]:
		ans += 1
		s.add(out[i])
	else:
		index += 1
		while index != n - 1 and init[index] in s:
			index += 1
	i += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ind = {}
for (i, xa) in enumerate(a):
	ind[xa] = i + 1
comp = 1
vis = set()
cnt = 0
for xb in b:
	if comp != ind[xb]:
		cnt += 1
	vis.add(ind[xb])
	while comp in vis:
		comp += 1
print(cnt)


from sys import stdin, stdout
import heapq
import cProfile, math
from collections import Counter, defaultdict, deque
from bisect import bisect_left, bisect, bisect_right
import itertools
from copy import deepcopy
from fractions import Fraction
import sys, threading
import operator as op
from functools import reduce
import sys
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)
fac_warm_up = False
printHeap = str()
memory_constrained = False
P = 10 ** 9 + 7

class MergeFind:

	def __init__(self, n):
		self.parent = list(range(n))
		self.size = [1] * n
		self.num_sets = n
		self.lista = [[_] for _ in range(n)]

	def find(self, a):
		to_update = []
		while a != self.parent[a]:
			to_update.append(a)
			a = self.parent[a]
		for b in to_update:
			self.parent[b] = a
		return self.parent[a]

	def merge(self, a, b):
		a = self.find(a)
		b = self.find(b)
		if a == b:
			return
		if self.size[a] < self.size[b]:
			(a, b) = (b, a)
		self.num_sets -= 1
		self.parent[b] = a
		self.size[a] += self.size[b]
		self.lista[a] += self.lista[b]

	def set_size(self, a):
		return self.size[self.find(a)]

	def __len__(self):
		return self.num_sets

def display(string_to_print):
	stdout.write(str(string_to_print) + '\n')

def prime_factors(n):
	factors = dict()
	for i in range(2, math.ceil(math.sqrt(n)) + 1):
		while n % i == 0:
			if i in factors:
				factors[i] += 1
			else:
				factors[i] = 1
			n = n // i
	if n > 2:
		factors[n] = 1
	return factors

def all_factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def fibonacci_modP(n, MOD):
	if n < 2:
		return 1
	return (cached_fn(fibonacci_modP, (n + 1) // 2, MOD) * cached_fn(fibonacci_modP, n // 2, MOD) + cached_fn(fibonacci_modP, (n - 1) // 2, MOD) * cached_fn(fibonacci_modP, (n - 2) // 2, MOD)) % MOD

def factorial_modP_Wilson(n, p):
	if p <= n:
		return 0
	res = p - 1
	for i in range(n + 1, p):
		res = res * cached_fn(InverseEuler, i, p) % p
	return res

def binary(n, digits=20):
	b = bin(n)[2:]
	b = '0' * (digits - len(b)) + b
	return b

def is_prime(n):
	if n < 4:
		return True
	if n % 2 == 0:
		return False
	if n % 3 == 0:
		return False
	i = 5
	w = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += w
		w = 6 - w
	return True

def generate_primes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	return prime
factorial_modP = []

def warm_up_fac(MOD):
	nonlocal factorial_modP, fac_warm_up
	if fac_warm_up:
		return
	factorial_modP = [1 for _ in range(fac_warm_up_size + 1)]
	for i in range(2, fac_warm_up_size):
		factorial_modP[i] = factorial_modP[i - 1] * i % MOD
	fac_warm_up = True

def InverseEuler(n, MOD):
	return pow(n, MOD - 2, MOD)

def nCr(n, r, MOD):
	nonlocal fac_warm_up, factorial_modP
	if not fac_warm_up:
		warm_up_fac(MOD)
		fac_warm_up = True
	return factorial_modP[n] * (pow(factorial_modP[r], MOD - 2, MOD) * pow(factorial_modP[n - r], MOD - 2, MOD) % MOD) % MOD

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return list(map(int, stdin.readline().split()))

def get_list():
	return list(map(int, stdin.readline().split()))
memory = dict()

def clear_cache():
	nonlocal memory
	memory = dict()

def cached_fn(fn, *args):
	nonlocal memory
	if args in memory:
		return memory[args]
	else:
		result = fn(*args)
		memory[args] = result
		return result

def ncr(n, r):
	return math.factorial(n) / (math.factorial(n - r) * math.factorial(r))

def binary_search(i, li):
	fn = lambda x: li[x] - x // i
	x = -1
	b = len(li)
	while b >= 1:
		while b + x < len(li) and fn(b + x) > 0:
			x += b
		b = b // 2
	return x
TestCases = False
optimise_for_recursion = True

def main():
	n = get_int()
	ai = get_list()
	bi = get_list()
	bi.reverse()
	ai.reverse()
	pos = 0
	fined = set()
	for ele in ai:
		while pos < n and bi[pos] in fined:
			pos += 1
		if ele == bi[pos]:
			pos += 1
		else:
			fined.add(ele)
	print(len(fined))
if TestCases:
	for i in range(get_int()):
		main()
else:
	main() if not optimise_for_recursion else threading.Thread(target=main).start()


n = int(input())
a = [int(s) - 1 for s in input().split()]
b = [int(s) - 1 for s in input().split()]
m = [False] * n
(i, j) = (0, 0)
c = 0
while i < n and j < n:
	if m[a[i]]:
		i += 1
	elif a[i] != b[j]:
		c += 1
		m[b[j]] = True
		j += 1
	else:
		m[b[j]] = True
		i += 1
		j += 1
print(c)


def solve(a, b, n):
	sp = [0] * n
	ai = 0
	bi = 0
	while max(ai, bi) < n:
		if a[ai] != b[bi] and (not sp[a[ai]]):
			sp[b[bi]] += 1
			bi += 1
		else:
			if not sp[a[ai]]:
				bi += 1
			ai += 1
	return sum(sp)

def main():
	n = int(input())
	a = [int(i) - 1 for i in input().split()]
	b = [int(i) - 1 for i in input().split()]
	print(solve(a, b, n))
main()


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
posb = {}
for i in range(1, n + 1):
	posb[b[i - 1]] = i
pos = [0] * (n + 1)
for i in range(1, n + 1):
	num = a[i - 1]
	pos[i] = posb[num]
if a == b:
	print(0)
else:
	(max, count) = (0, 0)
	for i in range(1, n + 1):
		if pos[i] >= max:
			max = pos[i]
		else:
			count += 1
	print(count)


n = int(input())
a = []
b = []
a = list(map(int, input().split()))
b = list(map(int, input().split()))
skip = set()
x = 0
ans = 0
for c in b:
	while a[x] in skip:
		x += 1
	if a[x] == c:
		pass
	else:
		ans += 1
	skip.add(c)
print(ans)


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
inda = [-1] * (n + 2)
d = {}
indb = [-1] * (n + 2)
for i in range(n):
	indb[b[i]] = i
move = 0
cnt = 0
pr = -1
for i in range(n):
	now = indb[a[i]]
	if now < pr:
		pass
	else:
		move += pr - now + 1
		pr = now
print(-move)


def main():
	n = int(input())
	enter = list(map(int, input().split()))
	leave = list(map(int, input().split()))
	nums = {}
	for i in range(1, n + 1):
		nums[i] = 0
	for i in range(n):
		nums[enter[i]] = i + 1
	arr = []
	for i in leave:
		arr.append(nums[i])
	ans = 0
	min_arr = []
	for i in range(n - 1, -1, -1):
		if not min_arr:
			min_arr.append(arr[i])
		else:
			min_arr.append(min(arr[i], min_arr[-1]))
	min_arr.reverse()
	for i in range(n - 1):
		if arr[i] > min_arr[i + 1]:
			ans += 1
	print(ans)
main()


n = int(input())
a1 = [int(i) for i in input().split()]
a2 = [0] * (n + 1)
ind = 1
for i in input().split():
	i = int(i)
	a2[i] = ind
	ind += 1
c = []
ans = 0
for i in range(n):
	c.append(a2[a1[i]] + 1)
mmax = 0
for i in range(n):
	if c[i] < mmax:
		ans += 1
	mmax = max(mmax, c[i])
print(ans)


n = int(input())
d = {}
en = []
ex = []
en = [int(i) for i in input().split()]
ex = [int(i) for i in input().split()]
cx = 0
ce = 0
while cx < n:
	if en[ce] != ex[cx] and en[ce] not in d:
		d[ex[cx]] = 1
		cx += 1
	elif en[ce] != ex[cx] and en[ce] in d:
		ce += 1
	elif en[ce] == ex[cx]:
		cx += 1
		ce += 1
print(len(d))


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [0] * (n + 1)
for (i, x) in enumerate(a):
	c[x] = i + 1
mn = n + 1
i = n - 1
ans = 0
while i >= 0:
	if c[b[i]] > mn:
		ans += 1
	mn = min(c[b[i]], mn)
	i -= 1
print(ans)


import sys
n = int(sys.stdin.readline())
arr = [0] + list(map(int, sys.stdin.readline().split()))
temp = [0] + list(map(int, sys.stdin.readline().split()))
leave = [0] * (n + 1)
for i in range(1, n + 1):
	leave[temp[i]] = i
res = [0] * (n + 1)
for i in range(1, n + 1):
	res[i] = leave[arr[i]]
val = res[1]
ans = 0
for i in range(2, n + 1):
	if res[i] < val:
		ans += 1
	val = max(val, res[i])
print(ans)


n = int(input())
A = [*map(int, input().split())]
B = [*map(int, input().split())]
D = [0 for _ in range(n + 1)]
i = j = 0
res = 0
while j < n:
	D[B[j]] = 1
	if A[i] != B[j]:
		if D[A[i]]:
			i += 1
		else:
			j += 1
			res += 1
	else:
		i += 1
		j += 1
print(res)


from sys import stdin
input = lambda : stdin.readline().rstrip('\r\n')
input_a = lambda fun: map(fun, stdin.readline().split())

def read():
	n = int(input())
	a = list(input_a(int))
	b = list(input_a(int))
	return (n, a, b)

def solve(n, a, b):
	finish: list = [None] * n
	finish2: list = [None] * n
	for (i, bi) in enumerate(b):
		finish[bi - 1] = i
	for i in range(n):
		finish2[i] = finish[a[i] - 1]
	mx = finish2[0]
	count = 0
	for i in range(1, n):
		if finish2[i] < mx:
			count += 1
		mx = max(mx, finish2[i])
	return count
result = solve(*read())
print(result)


n = int(input())
arr1 = list(map(int, input().split(' ')))
arr2 = list(map(int, input().split(' ')))
excluded = [False] * (n + 1)
sol = 0
i = 0
j = 0
while i < n and j < n:
	if excluded[arr1[i]]:
		i += 1
		continue
	if arr1[i] == arr2[j]:
		i += 1
		j += 1
	else:
		excluded[arr2[j]] = True
		j += 1
		sol += 1
print(sol)


import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = {}
for i in range(n):
	d[b[i]] = i + 1
x = 0
ans = n
for i in range(n):
	if d[a[i]] > x:
		x = d[a[i]]
		ans -= 1
print(ans)


import sys
input = lambda : sys.stdin.readline().rstrip()
N = int(input())
A = [int(a) - 1 for a in input().split()][::-1]
C = [-1] * N
for i in range(N):
	C[A[i]] = i
B = [C[int(a) - 1] for a in input().split()][::-1]
(ans, ma) = (0, -1)
for i in B:
	if ma > i:
		ans += 1
	ma = max(ma, i)
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
d = {}
i = 0
j = 0
for i in range(n):
	d[a[i]] = 0
c = 0
i = 0
while i < n and j < n:
	if i < n and d[a[i]] == 1:
		i += 1
		continue
	while j < n and a[i] != b[j]:
		d[b[j]] = 1
		j += 1
		c += 1
	i += 1
	j += 1
print(c)


import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

def li():
	return [int(i) for i in input().rstrip('\n').split(' ')]

def val():
	return int(input().rstrip('\n'))

def st():
	return input().rstrip('\n')

def sttoli():
	return [int(i) for i in input().rstrip('\n')]
n = val()
l1 = li()
l2 = li()
d = {}
t = 0
for i in l1:
	d[i] = t
	t += 1
s = set()
t = 0
tot = 0
for i in l2:
	if d[i] != t:
		tot += 1
		s.add(d[i])
	else:
		t += 1
		while t in s:
			t += 1
print(tot)


from bisect import *
from collections import *
from itertools import *
import functools
import sys
import math
from decimal import *
from copy import *
from heapq import *
from fractions import *
getcontext().prec = 30
MAX = sys.maxsize
MAXN = 1000010
MOD = 10 ** 9 + 7
spf = [i for i in range(MAXN)]

def sieve():
	for i in range(2, MAXN, 2):
		spf[i] = 2
	for i in range(3, int(MAXN ** 0.5) + 1):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def fib(n, m):
	if n == 0:
		return [0, 1]
	else:
		(a, b) = fib(n // 2)
		c = a % m * (b % m * 2 - a % m) % m
		d = a % m * (a % m) % m + b % m * b % m % m
		if n % 2 == 0:
			return [c, d]
		else:
			return [d, c + d]

def charIN(x=' '):
	return sys.stdin.readline().strip().split(x)

def arrIN(x=' '):
	return list(map(int, sys.stdin.readline().strip().split(x)))

def ncr(n, r):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % MOD
		den = den * (i + 1) % MOD
	return num * pow(den, MOD - 2, MOD) % MOD

def flush():
	return sys.stdout.flush()
n = int(input())
a = arrIN()
b = arrIN()
f = [0] * MAXN
i = 0
j = 0
ans = 0
while i < n and j < n:
	if not f[a[i]]:
		if b[j] == a[i]:
			f[b[j]] = 1
			i += 1
			j += 1
		else:
			ans += 1
			f[b[j]] = 1
			j += 1
	else:
		i += 1
print(ans)


n = int(input())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
fined = 0
i = 0
j = 0
d1 = {}
while i < n and j < n:
	if l1[i] in d1:
		i += 1
		continue
	if l1[i] == l2[j]:
		i += 1
		j += 1
	else:
		while j < n and l2[j] != l1[i]:
			d1[l2[j]] = 1
			j += 1
			fined += 1
print(fined)


n = int(input())
a = input().split(' ')
b = input().split(' ')
e = {}
for i in range(n):
	e[b[i]] = i + 1
p = 0
c = 0
for i in range(n):
	x = e[a[i]]
	if x > p:
		c += x - p - 1
		if x == n:
			break
		p = x
print(c)


n = int(input())
li = list(map(int, input().split()))
lf = list(map(int, input().split()))
d = {}
c = 0
i = 0
f = 0
while i < n and f < n:
	if li[i] == lf[f]:
		i += 1
		f += 1
	elif li[i] != lf[f] and d.get(li[i], 0) == 0:
		d[lf[f]] = 1
		c += 1
		f += 1
	else:
		i += 1
print(c)


import sys
import math
input = sys.stdin.readline
n = int(input())
alist = list(map(int, input().split()))
blist = list(map(int, input().split()))
done = [0] * n
count = 0
j = 0
i = 0
while i < n and j < n:
	if alist[i] == blist[j]:
		j += 1
		i += 1
	elif done[alist[i] - 1] == 1:
		i += 1
	else:
		done[blist[j] - 1] = 1
		count += 1
		j += 1
print(count)


n = int(input())
arr1 = list(map(int, input().strip().split()))
arr2 = list(map(int, input().strip().split()))
cnt = i = j = 0
d = {}
for x in arr2:
	d[x] = 0
while j < n:
	while j < n and arr1[i] != arr2[j]:
		if d[arr1[i]] == 1:
			i += 1
		else:
			d[arr2[j]] = 1
			cnt += 1
			j += 1
	d[arr2[j]] = 1
	i += 1
	j += 1
print(cnt)


n = int(input().strip())
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
d = dict()
for i in range(n):
	d[i + 1] = 0
if a == b:
	print(0)
else:
	b = b[::-1]
	a = a[::-1]
	while len(b) != 0:
		if d[a[-1]] == 1:
			a.pop()
		elif a[-1] == b[-1]:
			a.pop()
			b.pop()
		else:
			d[b[-1]] = 1
			b.pop()
	c = 0
	for i in range(n):
		if d[i + 1] == 1:
			c += 1
	print(c)


n = int(input())
enter = list(map(int, input().split()))
exit = list(map(int, input().split()))
i = 0
k = 0
check = [0] * 100001
result = 0
while i < n:
	count = 0
	while i < n and check[enter[k]] == 0 and (enter[k] != exit[i]):
		count += 1
		check[exit[i]] = 1
		i += 1
	if i < n and enter[k] == exit[i]:
		result += count
		i += 1
	k += 1
print(result)


import sys
input = sys.stdin.readline

def main():
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	b_x = [0] * (n + 1)
	for (i, v) in enumerate(b):
		b_x[v] = i
	c = [0] * n
	for (i, v) in enumerate(a):
		c[i] = b_x[v]
	ans = 0
	ma = 0
	for (i, v) in enumerate(c):
		if c[i] < ma:
			ans += 1
		ma = max(c[i], ma)
	print(ans)
main()


n = int(input())
lst1 = list(map(int, input().split()))
lst2 = list(map(int, input().split()))
Dict = {}
for i in range(n):
	ele = lst2[i]
	Dict[ele] = i
Max = Dict[lst1[0]]
ans = 0
for j in range(1, n):
	ele = lst1[j]
	index = Dict[ele]
	if index < Max:
		ans += 1
	else:
		Max = index
print(ans)


g = int(input())
before = [int(x) for x in input().split()]
after = [int(x) for x in input().split()]
before_dict = {x: y for (x, y) in enumerate(before, 1)}
after_dict = {y: x for (x, y) in enumerate(after, 1)}
ans = 0
tmp = 0
for i in before:
	if tmp >= after_dict[i]:
		ans += 1
	tmp = max(tmp, after_dict[i])
print(ans)


I = input
I()
a = I().split()
d = dict(zip(I().split(), range(len(a))))
r = m = 0
for x in a:
	r += d[x] < m
	m = max(m, d[x])
print(r)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
debt = {num: i for (i, num) in enumerate(a)}
ans = 0
cur = float('+inf')
for i in b[::-1]:
	if debt[i] > cur:
		ans += 1
	else:
		cur = debt[i]
print(ans)


n = int(input())
a = [0] + list(map(int, input().split()))
b = [0] + list(map(int, input().split()))
v = [0] * (n + 5)
i = 1
j = 1
while i <= n and j <= n:
	while v[a[i]] != 0:
		i += 1
	while v[b[j]] != 0:
		j += 1
	if a[i] == b[j]:
		v[a[i]] = 1
		i += 1
		j += 1
	else:
		v[b[j]] = 2
		j += 1
c = 0
for i in range(1, n + 1):
	if v[i] == 2:
		c += 1
print(c)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = 0
e = [False] * (n + 1)
j = 0
for i in range(n):
	while e[a[j]]:
		j += 1
	if a[j] == b[i]:
		j += 1
	else:
		c += 1
		e[b[i]] = True
print(c)


n = int(input())
A = list(map(int, input().split()))[::-1]
B = list(map(int, input().split()))[::-1]
cnt = 0
i = j = 0
fined = [0] * (n + 1)
while i < n:
	if A[i] == B[j]:
		i += 1
		j += 1
	elif fined[B[j]] == 0:
		while A[i] != B[j]:
			fined[A[i]] = 1
			i += 1
			cnt += 1
		else:
			i += 1
			j += 1
	else:
		j += 1
print(cnt)


from fractions import gcd
from datetime import date, timedelta
from heapq import *
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7

def main():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	if a == b:
		print(0)
		exit()
	ans = 0
	da = defaultdict(int)
	db = defaultdict(int)
	for i in range(n):
		da[a[i] - 1] = i
		db[b[i] - 1] = i
	ans = 0
	cnt = 0
	c = 0
	for i in range(n):
		if c <= db[a[i] - 1]:
			c = db[a[i] - 1]
			ans += 1
	print(n - ans)
main()


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
enter = list()
l = 0
j = 0
ans = 0
exit = [0 for i in range(n)]
for i in range(n):
	enter.append(a[i])
	while exit[enter[j] - 1] == 1:
		j += 1
	if enter[j] == b[i]:
		j += 1
	else:
		ans += 1
	exit[b[i] - 1] = 1
print(ans)