#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1E9;
int N;
int xs[(200005 * 2)], ys[(200005 * 2)], X, Y;
int x[200005][2], y[200005][2];
int rnk[200005], rep[200005];
bool sv[200005];
long long sumlen[200005], emn[200005], cnt[200005];
int find(int k) {
  if (rep[k] == k) return k;
  return rep[k] = find(rep[k]);
}
void unionset(int a, int b) {
  int x = find(a), y = find(b);
  if (x == y) return;
  if (rnk[x] > rnk[y]) swap(x, y);
  if (rnk[x] == rnk[y]) rnk[y]++;
  rep[x] = y;
  sumlen[y] += sumlen[x];
  emn[y] += emn[x];
  cnt[y] += cnt[x];
}
int val[1 << 20], lz[1 << 20];
set<int> comp[1 << 20];
void add(int k, int nl, int nr, int x, int v) {
  if (nl == nr) {
    val[k] += v;
    return;
  }
  int nm = (nl + nr) >> 1;
  if (x <= nm)
    add(2 * k + 1, nl, nm, x, v);
  else
    add(2 * k + 2, nm + 1, nr, x, v);
  val[k] = val[2 * k + 1] + val[2 * k + 2];
}
int get(int k, int nl, int nr, int l, int r) {
  if (l <= nl && r >= nr) return val[k];
  int nm = (nl + nr) >> 1;
  int a1 = 0, a2 = 0;
  if (l <= nm) a1 = get(2 * k + 1, nl, nm, l, r);
  if (r > nm) a2 = get(2 * k + 2, nm + 1, nr, l, r);
  return a1 + a2;
}
int getkth(int k, int nl, int nr, int v) {
  if (nl == nr) {
    assert(v == 1 && val[k] == 1);
    return nl;
  }
  int nm = (nl + nr) >> 1;
  if (v <= val[2 * k + 1])
    return getkth(2 * k + 1, nl, nm, v);
  else
    return getkth(2 * k + 2, nm + 1, nr, v - val[2 * k + 1]);
}
int E;
struct eve {
  int tp, id, y;
  eve() {}
  eve(int _tp, int _id, int _y) {
    tp = _tp;
    id = _id;
    y = _y;
  }
  bool operator<(const eve &ano) const {
    if (y == ano.y) return tp < ano.tp;
    return y < ano.y;
  }
} es[(200005 * 2)];
map<pair<int, int>, int> gs;
int main() {
  cin >> N;
  for (int i = (0); i < (N); i++) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    xs[X++] = x1;
    xs[X++] = x2;
    ys[Y++] = y1;
    ys[Y++] = y2;
    x[i][0] = x1;
    x[i][1] = x2;
    y[i][0] = y1;
    y[i][1] = y2;
  }
  sort(xs, xs + X);
  X = unique(xs, xs + X) - xs;
  sort(ys, ys + Y);
  Y = unique(ys, ys + Y) - ys;
  for (int i = (0); i < (N); i++) {
    x[i][0] = lower_bound(xs, xs + X, x[i][0]) - xs;
    x[i][1] = lower_bound(xs, xs + X, x[i][1]) - xs;
    y[i][0] = lower_bound(ys, ys + Y, y[i][0]) - ys;
    y[i][1] = lower_bound(ys, ys + Y, y[i][1]) - ys;
  }
  for (int i = (0); i < (N); i++) {
    rnk[i] = 0;
    rep[i] = i;
    cnt[i] = 1;
    sumlen[i] = (long long)(ys[y[i][1]] - ys[y[i][0]]) +
                (long long)(xs[x[i][1]] - xs[x[i][0]]);
  }
  for (int i = (0); i < (N); i++) {
    if (x[i][0] == x[i][1]) {
      es[E++] = eve(0, i, y[i][0]);
      es[E++] = eve(2, i, y[i][1]);
    } else {
      es[E++] = eve(1, i, y[i][0]);
    }
  }
  sort(es, es + E);
  map<pair<int, int>, int>::iterator gr;
  for (int i = (0); i < (E); i++) {
    eve &e = es[i];
    int id = e.id;
    if (e.tp == 0) {
      if (!gs.empty()) {
        gr = gs.upper_bound(pair<int, int>(x[id][0], INF));
        if (gr != gs.begin()) {
          gr--;
          int l = gr->first.first, r = gr->first.second, g = gr->second;
          if (l <= x[id][0] && x[id][0] <= r) {
            assert(x[id][0] - 1 >= l && x[id][0] + 1 <= r);
            assert(g != id);
            gs.erase(gr);
            int k, e;
            k = get(0, 0, X - 1, 0, x[id][0]);
            e = getkth(0, 0, X - 1, k);
            gs[pair<int, int>(l, e)] = g;
            k = get(0, 0, X - 1, 0, x[id][0]);
            e = getkth(0, 0, X - 1, k + 1);
            gs[pair<int, int>(e, r)] = g;
          }
        }
      }
      add(0, 0, X - 1, x[id][0], 1);
      gs[pair<int, int>(x[id][0], x[id][0])] = id;
    } else if (e.tp == 2) {
      gr = --gs.upper_bound(pair<int, int>(x[id][0], INF));
      int l = gr->first.first, r = gr->first.second, g = gr->second;
      assert(l <= x[id][0] && x[id][0] <= r);
      if (l == r) {
        gs.erase(gr);
      } else if (l == x[id][0] || r == x[id][0]) {
        int eles = get(0, 0, X - 1, l, r);
        assert(eles > 1);
        gs.erase(gr);
        if (l == x[id][0]) {
          int k = get(0, 0, X - 1, 0, l);
          int e = getkth(0, 0, X - 1, k + 1);
          gs[pair<int, int>(e, r)] = g;
        } else if (r == x[id][0]) {
          int k = get(0, 0, X - 1, 0, r);
          int e = getkth(0, 0, X - 1, k - 1);
          gs[pair<int, int>(l, e)] = g;
        }
      }
      add(0, 0, X - 1, x[id][0], -1);
    } else {
      int n = get(0, 0, X - 1, x[id][0], x[id][1]);
      emn[id] += n;
      if (!gs.empty()) {
        gr = gs.upper_bound(pair<int, int>(x[id][1], INF));
        if (gr != gs.begin()) {
          gr--;
          int sl = INF, sr = -INF;
          while (1) {
            bool b = 0;
            int l = gr->first.first, r = gr->first.second, g = gr->second;
            if (l <= x[id][1] && r >= x[id][0] &&
                get(0, 0, X - 1, x[id][0], x[id][1]) > 0) {
              unionset(g, id);
              sl = min(l, sl);
              sr = max(r, sr);
              if (gr == gs.begin()) b = 1;
              gs.erase(gr--);
              if (b) break;
            } else
              break;
          }
          if (sl != INF) gs[pair<int, int>(sl, sr)] = find(id);
        }
      }
    }
  }
  long long ans = 0;
  for (int i = (0); i < (N); i++) {
    int j = find(i);
    if (!sv[j]) {
      sv[j] = 1;
      long long f = 2 + emn[j] - cnt[j];
      long long sol = sumlen[j] - (f - 1);
      ans = max(ans, sol);
    }
  }
  cout << ans << endl;
  return 0;
}
