import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } // 1-dimensional min cost matching (perfect for as) long solve(const(long[]) as, const(long[]) bs) { debug { writeln("solve ", as, " ", bs); } const asLen = cast(int)(as.length); const bsLen = cast(int)(bs.length); assert(asLen <= bsLen); if (asLen == 0) { return 0; } alias Entry = Tuple!(long, "pos", int, "typ"); auto es = new Entry[asLen + bsLen]; foreach (i; 0 .. asLen) es[i] = Entry(as[i], 0); foreach (i; 0 .. bsLen) es[asLen + i] = Entry(bs[i], 1); es.sort; enum inf = 1000; // const inf = es[asLen + bsLen - 1].pos - es[0].pos + 1; long f0; // list of slopes, ls: as surplus long offL, offR; DList!long ls, rs; ls.insertBack(-inf); rs.insertBack(+inf); foreach (i; 0 .. asLen + bsLen) { if (es[i].typ == 0) { // shift to left const t = rs.front + offR; rs.removeFront; if (rs.empty) rs.insertFront(+inf - offR); f0 += t; ls.insertBack(t - offL); } else { // shift to right, chmin for 0->1 if (rs.front < 0) { f0 += rs.front; ls.back += rs.front; rs.front = 0; } const t = ls.back + offL; ls.removeBack; if (ls.empty) ls.insertBack(-inf - offL); f0 -= t; rs.insertFront(t - offR); } debug { foreach (t; ls) write(t + offL, " "); write("; ", f0, "; "); foreach (t; rs) write(t + offR, " "); writeln; } if (i < asLen + bsLen - 1) { offL -= (es[i + 1].pos - es[i].pos); offR += (es[i + 1].pos - es[i].pos); } debug { foreach (t; ls) write(t + offL, " "); write("; ", f0, "; "); foreach (t; rs) write(t + offR, " "); writeln; } } return f0; } void main() { try { for (; ; ) { int M = readInt; int N = readInt; const K = readLong; auto A = new long[M]; foreach (i; 0 .. M) A[i] = readLong; auto B = new long[N]; foreach (i; 0 .. N) B[i] = readLong; A.sort; B.sort; if (M > N) { swap(M, N); swap(A, B); } long[][long] ass, bss; foreach (i; 0 .. M) ass[A[i] % K] ~= A[i] / K; foreach (i; 0 .. N) bss[B[i] % K] ~= B[i] / K; long ans; foreach (key, as; ass) { if (key !in bss) { ans = -1; break; } auto bs = bss[key]; if (as.length > bs.length) { ans = -1; break; } ans += solve(as, bs); } writeln(ans); } } catch (EOFException e) { } }