#P1269B. Modulo Equality

    ID: 5102 Type: RemoteJudge 3000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>brute forcesortings*1500

Modulo Equality

No submission language available for this problem.

Description

You are given a positive integer mm and two integer sequence: a=[a1,a2,,an]a=[a_1, a_2, \ldots, a_n] and b=[b1,b2,,bn]b=[b_1, b_2, \ldots, b_n]. Both of these sequence have a length nn.

Permutation is a sequence of nn different positive integers from 11 to nn. For example, these sequences are permutations: [1][1], [1,2][1,2], [2,1][2,1], [6,7,3,4,1,2,5][6,7,3,4,1,2,5]. These are not: [0][0], [1,1][1,1], [2,3][2,3].

You need to find the non-negative integer xx, and increase all elements of aia_i by xx, modulo mm (i.e. you want to change aia_i to (ai+x)modm(a_i + x) \bmod m), so it would be possible to rearrange elements of aa to make it equal bb, among them you need to find the smallest possible xx.

In other words, you need to find the smallest non-negative integer xx, for which it is possible to find some permutation p=[p1,p2,,pn]p=[p_1, p_2, \ldots, p_n], such that for all 1in1 \leq i \leq n, (ai+x)modm=bpi(a_i + x) \bmod m = b_{p_i}, where ymodmy \bmod m — remainder of division of yy by mm.

For example, if m=3m=3, a=[0,0,2,1],b=[2,0,1,1]a = [0, 0, 2, 1], b = [2, 0, 1, 1], you can choose x=1x=1, and aa will be equal to [1,1,0,2][1, 1, 0, 2] and you can rearrange it to make it equal [2,0,1,1][2, 0, 1, 1], which is equal to bb.

The first line contains two integers n,mn,m (1n2000,1m1091 \leq n \leq 2000, 1 \leq m \leq 10^9): number of elemens in arrays and mm.

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (0ai<m0 \leq a_i < m).

The third line contains nn integers b1,b2,,bnb_1, b_2, \ldots, b_n (0bi<m0 \leq b_i < m).

It is guaranteed that there exists some non-negative integer xx, such that it would be possible to find some permutation p1,p2,,pnp_1, p_2, \ldots, p_n such that (ai+x)modm=bpi(a_i + x) \bmod m = b_{p_i}.

Print one integer, the smallest non-negative integer xx, such that it would be possible to find some permutation p1,p2,,pnp_1, p_2, \ldots, p_n such that (ai+x)modm=bpi(a_i + x) \bmod m = b_{p_i} for all 1in1 \leq i \leq n.

Input

The first line contains two integers n,mn,m (1n2000,1m1091 \leq n \leq 2000, 1 \leq m \leq 10^9): number of elemens in arrays and mm.

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (0ai<m0 \leq a_i < m).

The third line contains nn integers b1,b2,,bnb_1, b_2, \ldots, b_n (0bi<m0 \leq b_i < m).

It is guaranteed that there exists some non-negative integer xx, such that it would be possible to find some permutation p1,p2,,pnp_1, p_2, \ldots, p_n such that (ai+x)modm=bpi(a_i + x) \bmod m = b_{p_i}.

Output

Print one integer, the smallest non-negative integer xx, such that it would be possible to find some permutation p1,p2,,pnp_1, p_2, \ldots, p_n such that (ai+x)modm=bpi(a_i + x) \bmod m = b_{p_i} for all 1in1 \leq i \leq n.

Samples

Sample Input 1

4 3
0 0 2 1
2 0 1 1

Sample Output 1

1

Sample Input 2

3 2
0 0 0
1 1 1

Sample Output 2

1

Sample Input 3

5 10
0 0 0 1 2
2 1 0 0 0

Sample Output 3

0