import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
// int[] input = new int[n];
//直接用队列
Deque<Integer> dq = new LinkedList<>();
for(int i =0;i < n;i++){
// input[i] = sc.nextInt();
dq.offer(sc.nextInt());
}
int[] output = new int[n];
for(int j = 0;j < n;j++){
output[j] = sc.nextInt();
}
//数组构建完成
int count = 0;
//建一个set存不匹配的元素,说明它加速了,后面遇到给它弹出队列
Set<Integer> set = new HashSet<>();
for(int k = 0;k < n;k++){
if(!dq.isEmpty() && output[k] != dq.peekFirst()){
set.add(output[k]);
}else if(!dq.isEmpty() && output[k] == dq.peekFirst()){
//说明同步了,顺序是对的,队首元素弹出
dq.pollFirst();
//然后判断,此时栈顶的元素是否在set中
while(set.contains(dq.peekFirst())){
count++;
dq.pollFirst();
}
}
}
System.out.println(count);
}
}