PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000061. Mức độ đảo trật tự lời nói của Yoda (Yodaness Level)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Mức độ đảo trật tự lời nói của Yoda (Yodaness Level)

Yodaness Level

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Yoda often speaks sentences whose word order differs from the normal order. A sentence consists of nn distinct words, so their ordering can be compared unambiguously.

You receive two permutations of the same set of words: Yoda's order and the normal order. A pair contributes 11 to the yodaness level if its relative order is reversed between the sentences. For example, word uu appears before word vv normally but after vv in Yoda's sentence.

Count all such pairs of distinct words, counting each pair once.

Input

The first line contains nn, the number of words.

The second line contains the nn words in Yoda's order.

The third line contains exactly those nn words in normal order. Each word uses only lowercase letters a–z and contains no whitespace.

Output

Print one integer: the number of word pairs whose relative order is reversed.

Subtasks

  • Subtask 1 (20 points): 1≤n≤601\le n\le 60; 1≤∣wi∣≤201\le |w_i|\le 20; all words are distinct, contain only a–z and both lines are permutations of the same words.

  • Subtask 2 (30 points): 1≤n≤30001\le n\le 3000; 1≤∣wi∣≤201\le |w_i|\le 20; all words are distinct, contain only a–z and both lines are permutations of the same words.

  • Subtask 3 (50 points): 1≤n≤300001\le n\le 30000; 1≤∣wi∣≤201\le |w_i|\le 20; all words are distinct, contain only a–z and both lines are permutations of the same words.

Examples

Input

6
in the force strong you are
you are strong in the force

Output

11

Explanation

In the normal sentence, you, are, strong, in, the, and force have positions 1,2,3,4,5,61,2,3,4,5,6 respectively.

Replacing Yoda's words with their normal positions yields [4,5,6,3,1,2][4,5,6,3,1,2]. The numbers of smaller later positions are 3,3,3,2,0,03,3,3,2,0,0.

Each of in, the, and force precedes the three words strong, you, and are in Yoda's sentence while following them normally. strong precedes you and are as well. The total is 3+3+3+2=113+3+3+2=11.