Time limit
1s
Memory limit
128 MB
Hyeon's family has three members: father, mother, and Hyeon. Every weekend they visit their favorite Chinese restaurant. The restaurant places N dishes at equal intervals on a rotating dining table. The plates are numbered 1 through N in clockwise order, and N is always a multiple of 3.
Let N = 3k. At the beginning of the meal, dish 1 is in front of the father, dish k + 1 is in front of the mother, and dish 2k + 1 is in front of Hyeon.

Each person has an ordered list of dishes they want to eat. The same dish may appear more than once, but each person must eat their dishes in the given order. A person can eat a dish only when that plate is directly in front of them. Every dish has enough food to be eaten multiple times.
One rotation moves every plate to the neighboring position. If the plate immediately to a person's right moves in front of them, the table has been rotated one step clockwise. If the plate immediately to a person's left moves in front of them, the table has been rotated one step counterclockwise.
For example, if the father alone wants to eat in the order (3, 7, 1), he can rotate 2 steps to reach dish 3, then 4 more steps to reach dish 7, and finally 3 more steps to reach dish 1, for a total of 9 rotation steps.
The three family members may eat in any interleaving, as long as each person's own order is preserved. When the table reaches a position, multiple people may eat their next dishes at that same position if possible.
Find the minimum total number of table rotation steps needed for all three family members to finish their meals.
The first line contains the number of dishes N. It satisfies 3 <= N <= 300000, and N is always a multiple of 3.
The next three lines describe the father, the mother, and Hyeon, in that order. Each line starts with the length p_i of that person's desired sequence, followed by p_i dish numbers separated by spaces. Here i = 1, 2, 3.
Each p_i is between 1 and 100, inclusive. Every dish number is between 1 and N, inclusive. The same dish number may appear multiple times in one person's sequence.
Print the minimum total number of table rotation steps needed for everyone to finish eating.