This page is still under construction.

Parts of this page are still being built. What you see may change.

Genome Evolution

Time limit1sMemory limit128 MB

Summary
Count the shared gene blocks of length above one that appear as consecutive runs in both chromosomes.
Level

Medium6 of 10

Topics
Intervals, Array
Solved
No attempts yet

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 A′A' of the same length, where AA is a consecutive subsequence of the first chromosome, A′A' is a consecutive subsequence of the second chromosome, and AA is a permutation of A′A'. Each chromosome contains every gene exactly once, so once AA is fixed there is at most one matching A′A'. 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 (2≤n≤30002 \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.

Examples2

  1. Example 1

    Input
    4
    3 2 1 4
    1 2 4 3
    5
    3 2 1 5 4
    3 2 1 5 4
    0
    
    Expected output
    3
    10
    
  2. Example 2

    Input
    2
    1 2
    2 1
    3
    1 2 3
    3 2 1
    0
    
    Expected output
    1
    3