#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10;
const long long inf = 1e18;
long long rd() {
  long long x = 0, w = 1;
  char ch = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return x * w;
}
int fa[N], ch[N][2], rt, tt, st[N], tp;
long long vx[N], vy[N], lx[N], rx[N], m0[N], m1[N], tgx[N], tgy[N];
void psup(int x) {
  lx[x] = min(vx[x], lx[ch[x][0]]), rx[x] = max(vx[x], rx[ch[x][1]]);
  m0[x] = max(vy[x], max(m0[ch[x][0]], m0[ch[x][1]]));
  m1[x] = max(vy[x] - vx[x], max(m1[ch[x][0]], m1[ch[x][1]]));
}
void add(int x, long long dx, long long dy) {
  if (!x) return;
  vx[x] += dx, vy[x] += dy;
  lx[x] += dx, rx[x] += dx;
  m0[x] += dy, m1[x] += dy - dx;
  tgx[x] += dx, tgy[x] += dy;
}
void psdn(int x) {
  if (tgx[x] || tgy[x])
    add(ch[x][0], tgx[x], tgy[x]), add(ch[x][1], tgx[x], tgy[x]);
  tgx[x] = tgy[x] = 0;
}
void rot(int x) {
  int y = fa[x], z = fa[y], yy = ch[y][1] == x, w = ch[x][!yy];
  if (y != rt) ch[z][ch[z][1] == y] = x;
  ch[x][!yy] = y, ch[y][yy] = w;
  if (w) fa[w] = y;
  fa[y] = x, fa[x] = z;
  psup(y), psup(x);
}
void spl(int x, int en) {
  int xx = x;
  tp = 0;
  while (xx) st[++tp] = xx, xx = fa[xx];
  while (tp) xx = st[tp--], psdn(xx);
  while (fa[x] != en) {
    int y = fa[x], z = fa[y];
    if (fa[y] != en) ((ch[y][1] == x) ^ (ch[z][1] == y)) ? rot(x) : rot(y);
    rot(x);
  }
  if (!en) rt = x;
}
void inst(long long px) {
  long long ly = -inf, ry = -inf;
  int x = rt;
  while (1) {
    psdn(x);
    if (vx[x] == px) {
      ly = max(ly, m0[ch[x][0]]);
      ry = max(ry, m1[ch[x][1]]);
      vy[x] = max(vy[x], max(ly, ry + px));
      psup(x);
      break;
    }
    bool xx = px > vx[x];
    if (xx)
      ly = max(ly, max(m0[ch[x][0]], vy[x]));
    else
      ry = max(ry, max(m1[ch[x][1]], vy[x] - vx[x]));
    if (!ch[x][xx]) {
      int nx = ++tt;
      fa[nx] = x, ch[x][xx] = nx;
      vx[nx] = px, vy[nx] = max(ly, ry + px);
      psup(nx), x = nx;
      break;
    }
    x = ch[x][xx];
  }
  spl(x, 0);
}
void modif(int x, long long ll, long long rr, long long dy) {
  if (!x || rx[x] < ll || lx[x] > rr) return;
  if (ll <= lx[x] && rx[x] <= rr) {
    add(x, 0, dy);
    return;
  }
  if (ll <= vx[x] && vx[x] <= rr) vy[x] += dy;
  psdn(x);
  modif(ch[x][0], ll, rr, dy), modif(ch[x][1], ll, rr, dy);
  psup(x);
}
struct node {
  long long x, i;
  bool operator<(const node &bb) const { return x < bb.x; }
} s1[N], s2[N];
int n, m, t1, t2, c[2];
long long k, bk[N << 1], tb;
int main() {
  for (int i = 0; i <= N - 5; ++i)
    lx[i] = inf + inf, rx[i] = m0[i] = m1[i] = -inf - inf;
  rt = tt = 1, psup(1);
  n = rd(), m = rd(), k = rd();
  for (int i = 1; i <= n; ++i) {
    s1[++t1] = (node){rd(), 0}, s2[++t2] = (node){rd(), 0};
    bk[++tb] = s1[t1].x, bk[++tb] = s2[t2].x;
  }
  for (int i = 1; i <= m; ++i) {
    s1[++t1] = (node){rd(), 1}, s2[++t2] = (node){rd(), 1};
    bk[++tb] = s1[t1].x, bk[++tb] = s2[t2].x;
  }
  sort(s1 + 1, s1 + t1 + 1), sort(s2 + 1, s2 + t2 + 1);
  sort(bk + 1, bk + tb + 1), tb = unique(bk + 1, bk + tb + 1) - bk - 1;
  for (int h = 1, i = 1, j = 1; h < tb; ++h) {
    while (i <= t1 && s1[i].x == bk[h]) ++c[s1[i].i], ++i;
    while (j <= t2 && s2[j].x == bk[h]) --c[s2[j].i], ++j;
    long long ln = bk[h + 1] - bk[h];
    int ty = 1 * (bool)c[0] + 2 * (bool)c[1];
    if (!ty) continue;
    if (ty == 1)
      add(rt, ln, ln);
    else if (ty == 2)
      add(rt, -ln, 0);
    else {
      inst(-k), inst(k);
      modif(rt, -inf * 3, -k - 1, ln);
      modif(rt, -k, k, ln * 2);
      modif(rt, k + 1, inf * 3, ln);
    }
  }
  printf("%lld\n", m0[rt]);
  return 0;
}
