#include using LL = long long; const int N = 2e5 + 7; LL ans; int n, m, k, cnt, ok; std::vector> a[N]; std::vector b[N * 2]; std::map mp; void solve(const std::vector> &a) { int now = N, lo = N, hi = N; for(auto &&[x, y]: a) if(y) { b[++now].push_back(x); hi = std::max(hi, now); } else { b[now--].push_back(x); lo = std::min(lo, now); } if(now < N) ok = 0; for(int i = lo; i <= hi; ++i) { LL sum = 0; for(int j = 0; j < b[i].size(); ++j) sum += (j & 1 ? b[i][j] : -b[i][j]); if(b[i].size() & 1) ans += std::min(sum + b[i].back(), -(sum + b[i][0])); else ans += sum; b[i].resize(0); } } int main() { scanf("%d%d%d", &n, &m, &k); for(int i = 1, x; i <= n; ++i) { scanf("%d", &x); if(!mp.count(x % k)) mp[x % k] = ++cnt; a[mp[x % k]].push_back({x / k, n >= m}); } for(int i = 1, x; i <= m; ++i) { scanf("%d", &x); if(!mp.count(x % k)) mp[x % k] = ++cnt; a[mp[x % k]].push_back({x / k, n < m}); } ok = 1; for(auto &&[_, c]: mp) { std::sort(a[c].begin(), a[c].end()); solve(a[c]); } printf("%lld\n", ok ? ans : -1LL); return 0; }