Genome Evolution

No attempts yetTime limit1sMemory limit128 MB

Problem

Xi is a developmental biologist who studies developmental distances between chromosomes. In Xi's simplified view a chromosome is a permutation of nn genes numbered 11 to nn. Xi is building a measure of evolutionary distance between two chromosomes. In Xi's theory of evolution, any set of genes that lies together in both chromosomes is a positive witness that the two chromosomes are similar.

A positive witness is a pair of sequences AA and AA' of the same length, where AA is a consecutive subsequence of the first chromosome, AA' is a consecutive subsequence of the second chromosome, and AA is a permutation of AA'. Each chromosome contains every gene exactly once, so once AA is fixed there is at most one matching AA'. Count the positive witnesses of the two given chromosomes whose length is greater than one.

Input

The input contains several test cases. Each test case starts with a line containing the number of genes nn (2n30002 \le n \le 3000). The next two lines contain the two chromosomes, one per line, each as a list of positive integers that uses every number from 11 to nn exactly once. A line containing a single 00 ends the input and is not a test case.

Output

For each test case, print one line with the number of positive witnesses for the two chromosomes.