This page is still under construction.

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

Revenge of the ants 2

Time limit5sMemory limit256 MB

Summary
Labeled ants walk both ways on a circular rail and bounce on collision; compute when each ant is back at its start with its initial direction.
Level

Hard8 of 10

Topics
String matching, Sorting, Number theory, Math
Solved
No attempts yet

Problem

A circular rail has circumference LL. Gyeonggeun split the rail into LL equal parts and numbered the points from 00 to L−1L-1 in the clockwise direction. He picked some of those points and placed NN ants that walk clockwise and MM ants that walk counterclockwise. No point holds two or more ants.

Every ant on the rail moves a distance of 1 per second. The rail is narrow enough that only one ant fits across it, so when two ants moving in opposite directions meet at a point, both reverse direction at that instant. An ant is a point with no size, so two ants collide only when their positions become exactly equal.

Gyeonggeun tells all the ants apart. Write a program that finds the minimum number of seconds until every ant is back at its starting point and moving in its starting direction.

Input

The first line has the circumference LL of the rail, the number NN of ants that walk clockwise, and the number MM of ants that walk counterclockwise, separated by spaces. (1≤L≤10121 \le L \le 10^{12}, 1≤N1 \le N, 1≤M1 \le M, N+M≤106N + M \le 10^6)

The second line has the NN points that hold the clockwise ants, separated by spaces.

The third line has the MM points that hold the counterclockwise ants, separated by spaces.

Every point is an integer between 00 and L−1L-1, and the N+MN + M given points are distinct. The points are not necessarily sorted.

Output

Print the minimum time in seconds until every ant is back at its starting point and moving in its starting direction.

Note

In the first example the two ants collide at 0.5 seconds and at 1.5 seconds, and at 2 seconds the state matches the start.

Examples3

  1. Example 1

    Input
    2 1 1
    0
    1
    
    Expected output
    2
  2. Example 2

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

    Input
    12 3 3
    0 4 8
    2 6 10
    
    Expected output
    4