QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 128 MB Total points: 10

#6067. One-armed bandit [B]

Statistics

Bajtek came to a casino, where he immediately became interested in a slot machine. The most important part of the machine is its three reels. Each of them is divided into $n$ equal fields, on which different symbols are painted. There are $n$ possible symbols, and each of them appears on each reel exactly once. For simplicity, let us number the symbols with integers from 1 to $n$. The figure below shows an example of a slot machine with three reels divided into $n = 5$ fields:

After pulling the lever, each of the reels shifts cyclically by a certain number of positions. The player's winnings depend on the number of horizontal rows that contain three identical symbols.

Bajtek knows that the slot machine could take all his money, so he would prefer to first determine what his maximum possible winnings could be. Help him and determine the number of rows in which three identical symbols can be found with the most favorable arrangement of the reels.

Input

The first line of input contains a single integer $n$ ($1 \le n \le 300\,000$), representing the size of the reels. The next three lines describe the arrangements of symbols on the individual reels.

The description of a reel consists of $n$ pairwise distinct integers $a_{1}, a_{2}, \cdots, a_{n}$ ($1 \le a_{i} \le n$), where $a_{i}$ denotes the symbol located at position $i$.

Output

The first and only line of output should contain a single integer equal to the maximum number of rows in which three identical symbols can be found simultaneously.

Examples

Input 1

5
1 5 4 3 2
1 3 2 4 5
2 1 5 4 3

Output 1

3

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.