VK Cup 2012 Round 3 (Unofficial Div. 2 Edition)
E
Ancient Berland Hieroglyphs
Codeforces
未评级
原题
← D→
翻译中…

Polycarpus enjoys studying Berland hieroglyphs. Once Polycarp got hold of two ancient Berland pictures, on each of which was drawn a circle of hieroglyphs. We know that no hieroglyph occurs twice in either the first or the second circle (but in can occur once in each of them).

翻译中…

Polycarpus wants to save these pictures on his laptop, but the problem is, laptops do not allow to write hieroglyphs circles. So Polycarp had to break each circle and write down all of its hieroglyphs in a clockwise order in one line. A line obtained from the first circle will be called $*a*$, and the line obtained from the second one will be called $*b*$.

翻译中…

There are quite many ways to break hieroglyphic circles, so Polycarpus chooses the method, that makes the length of the largest substring of string $*a*$, which occurs as a subsequence in string $*b*$, maximum.

翻译中…

Help Polycarpus — find the maximum possible length of the desired substring (subsequence) if the first and the second circles are broken optimally.

翻译中…

The length of string $*s*$ is the number of characters in it. If we denote the length of string $*s*$ as $|*s*|$, we can write the string as $*s* = *s*_{1}*s*_{2}... *s*_{|*s*|}$.

翻译中…

A substring of $*s*$ is a non-empty string $*x* = *s*[*a*... *b*] = *s*_{*a*}*s*_{*a* + 1}... *s*_{*b*}$ ($1 ≤ *a* ≤ *b* ≤ |*s*|$). For example, "code" and "force" are substrings of "codeforces", while "coders" is not.

翻译中…

A subsequence of $*s*$ is a non-empty string $*y* = *s*[*p*_{1}*p*_{2}... *p*_{|*y*|}] = *s*_{*p*_{1}}*s*_{*p*_{2}}... *s*_{*p*_{|*y*|}}$ ($1 ≤ *p*_{1} < *p*_{2} < ... < *p*_{|*y*|} ≤ |*s*|$). For example, "coders" is a subsequence of "codeforces".

翻译中…

Input

翻译中…

The first line contains two integers $*l*_{*a*}$ and $*l*_{*b*}$ ($1 ≤ *l*_{*a*}, *l*_{*b*} ≤ 1000000$) — the number of hieroglyphs in the first and second circles, respectively.

翻译中…

Below, due to difficulties with encoding of Berland hieroglyphs, they are given as integers from $1$ to $10^{6}$.

翻译中…

The second line contains $*l*_{*a*}$ integers — the hieroglyphs in the first picture, in the clockwise order, starting with one of them.

翻译中…

The third line contains $*l*_{*b*}$ integers — the hieroglyphs in the second picture, in the clockwise order, starting with one of them.

翻译中…

It is guaranteed that the first circle doesn't contain a hieroglyph, which occurs twice. The second circle also has this property.

翻译中…

Output

翻译中…

Print a single number — the maximum length of the common substring and subsequence. If at any way of breaking the circles it does not exist, print 0.

翻译中…

Note

翻译中…

In the first test Polycarpus picks a string that consists of hieroglyphs 5 and 1, and in the second sample — from hieroglyphs 1, 3 and 5.

翻译中…

样例

Input #1
5 4
1 2 3 4 5
1 3 5 6
Output #1
2
InputCopy OutputCopy
Input #2
4 6
1 3 5 2
1 2 3 4 5 6
Output #2
3
InputCopy OutputCopy
Input #3
3 3
1 2 3
3 2 1
Output #3
2
InputCopy OutputCopy
题目信息
题号
174E
时间限制
1.5 s
内存限制
256 MB

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。