#include <bits/stdc++.h>
using namespace std;
int n, f[200005], cnt, Rt, nod;
long long len[200005], Ans;
int ID[6000005], vis[6000005], ls[6000005], rs[6000005];
long long sum[6000005];
struct T {
  int x1, y1, y2, id, tp;
} a[200005 * 2];
bool cmp(T a, T b) { return a.x1 == b.x1 ? a.tp < b.tp : a.x1 < b.x1; }
int Get(int x) { return f[x] == x ? x : f[x] = Get(f[x]); }
void add(int &k, int l, int r, int u, int id) {
  if (!k) k = ++nod;
  vis[k] = 0;
  if (l == r) {
    ID[k] = id;
    sum[k] = 1;
    return;
  }
  int mid = l + (r - l) / 2;
  if (u <= mid)
    add(ls[k], l, mid, u, id);
  else
    add(rs[k], mid + 1, r, u, id);
  sum[k] = sum[ls[k]] + sum[rs[k]];
  ID[k] = max(ID[ls[k]], ID[rs[k]]);
}
void del(int &k, int l, int r, int u, int id) {
  if (!k) k = ++nod;
  vis[k] = 0;
  if (l == r) {
    ID[k] = 0;
    sum[k] = 0;
    return;
  }
  int mid = l + (r - l) / 2;
  if (u <= mid)
    del(ls[k], l, mid, u, id);
  else
    del(rs[k], mid + 1, r, u, id);
  sum[k] = sum[ls[k]] + sum[rs[k]];
  ID[k] = max(ID[ls[k]], ID[rs[k]]);
}
void work(int k) {
  if (!k || vis[k]) return;
  vis[k] = 1;
  if (ID[ls[k]] && ID[rs[k]]) f[Get(ID[ls[k]])] = Get(ID[rs[k]]);
  work(ls[k]);
  work(rs[k]);
}
long long qry(int k, int l, int r, int u, int v, int id) {
  if ((!k) || (!ID[k])) return 0;
  if (u <= l && r <= v) {
    f[Get(ID[k])] = Get(id);
    work(k);
    return sum[k];
  }
  int mid = l + (r - l) / 2;
  long long ans = 0;
  if (u <= mid) ans += qry(ls[k], l, mid, u, v, id);
  if (v > mid) ans += qry(rs[k], mid + 1, r, u, v, id);
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    if (x1 == x2) {
      a[++cnt] = (T){x1, y1, y2, i, 2};
    } else {
      len[i] = x2 - x1 + 1;
      a[++cnt] = (T){x1, y1, 0, i, 1};
      a[++cnt] = (T){x2, y1, 0, i, 3};
    }
  }
  for (int i = 1; i <= n; i++) f[i] = i;
  sort(a + 1, a + cnt + 1, cmp);
  for (int i = 1; i <= cnt; i++) {
    if (a[i].tp == 2)
      len[a[i].id] =
          a[i].y2 - a[i].y1 + 1 -
          qry(Rt, -1000000005, 1000000005, a[i].y1, a[i].y2, a[i].id);
    else if (a[i].tp == 1)
      add(Rt, -1000000005, 1000000005, a[i].y1, a[i].id);
    else
      del(Rt, -1000000005, 1000000005, a[i].y1, a[i].id);
  }
  for (int i = 1; i <= n; i++)
    if (Get(i) != i) len[Get(i)] += len[i];
  for (int i = 1; i <= n; i++) Ans = max(Ans, len[i]);
  printf("%lld\n", Ans - 1);
}
