import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int[] order = new int[n + 1]; for (int i = 0; i < n; i++) { order[sc.nextInt()] = i; } ArrayList list = new ArrayList<>(); list.add(-1); list.add(Integer.MAX_VALUE); int count = 0; for (int i = 0; i < n; i++) { int x = order[arr[i]]; int left = 0; int right = list.size(); while (right - left > 1) { int m = (left + right) / 2; if (list.get(m) < x) { left = m; } else { right = m; } } count += list.size() - right - 1; list.add(right, x); } count = 0; for (int i = n - 1; i > 0; i--) { for (int j = 0; j < i; j++) { if (order[arr[j]] > order[arr[j + 1]]) { int tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; count++; } } } System.out.println(count); } }