#P1269B. Modulo Equality
Modulo Equality
No submission language available for this problem.
Description
You are given a positive integer and two integer sequence: and . Both of these sequence have a length .
Permutation is a sequence of different positive integers from to . For example, these sequences are permutations: , , , . These are not: , , .
You need to find the non-negative integer , and increase all elements of by , modulo (i.e. you want to change to ), so it would be possible to rearrange elements of to make it equal , among them you need to find the smallest possible .
In other words, you need to find the smallest non-negative integer , for which it is possible to find some permutation , such that for all , , where — remainder of division of by .
For example, if , , you can choose , and will be equal to and you can rearrange it to make it equal , which is equal to .
The first line contains two integers (): number of elemens in arrays and .
The second line contains integers ().
The third line contains integers ().
It is guaranteed that there exists some non-negative integer , such that it would be possible to find some permutation such that .
Print one integer, the smallest non-negative integer , such that it would be possible to find some permutation such that for all .
Input
The first line contains two integers (): number of elemens in arrays and .
The second line contains integers ().
The third line contains integers ().
It is guaranteed that there exists some non-negative integer , such that it would be possible to find some permutation such that .
Output
Print one integer, the smallest non-negative integer , such that it would be possible to find some permutation such that for all .