#include #include #include

using namespace std;

struct seg { int id; seg *ls, *rs; }; struct block { int b, w, p, l, r, a; int lid, rid, mid; inline void read() { scanf("%d%d%d%d%d%d", &a, &b, &w, &l, &r, &p); } }; struct edge { int t, c; edge *next, *rv; };

const int maxn = 5009; const int maxnd = 90009; const int maxe = 400009; const int inf = 0x3f3f3f3f;

int n, tnid, st, te; int va[maxn * 3], tv, ans; block a[maxn]; seg sbuf_arr[maxn * 41], *sbuf(sbuf_arr), *rt; edge ebuf_arr[maxe], *ebuf(ebuf_arr), *head[maxnd];

inline edge* allocEdge(int u, int v, int c) { ebuf-> t = v; ebuf-> c = c; ebuf-> next = head[u]; return head[u] = ebuf ++; } inline void addEdge(int u, int v, int c) { edge *a(allocEdge(u, v, c)); edge *b(allocEdge(v, u, 0)); a-> rv = b; b-> rv = a; }

#define midp ((pl+pr)»1) #define lson(p) ((p)?(p->ls):0) #define rson(p) ((p)?(p->rs):0) inline void sgtQry(seg* p, int oid, int l, int r, int pl, int pr) { if (!p) return; else if (pl == l && pr == r) addEdge(oid, p-> id, inf); else if (r <= midp) sgtQry(p-> ls, oid, l, r, pl, midp); else if (l >= midp) sgtQry(p-> rs, oid, l, r, midp, pr); else sgtQry(p-> ls, oid, l, midp, pl, midp), sgtQry(p-> rs, oid, midp, r, midp, pr); } inline seg* sgtChg(seg* q, int po, int pi, int pl, int pr) { seg *p(sbuf ++); p-> id = ++ tnid; if (pl + 1 == pr) { addEdge(p-> id, a[pi]. rid, inf); if (q) addEdge(p-> id, q-> id, inf); } else { p-> ls = lson(q), p-> rs = rson(q); if (po < midp) p-> ls = sgtChg(lson(q), po, pi, pl, midp); else p-> rs = sgtChg(rson(q), po, pi, midp, pr); if (p-> ls) addEdge(p-> id, p-> ls-> id, inf); if (p-> rs) addEdge(p-> id, p-> rs-> id, inf); } return p; }

int d[maxnd]; bool bfsBuild() { static int q[maxnd]; int hd(0), tl(1); memset(d, 0, sizeof(d)); d[q[0] = st] = 1; while (hd < tl && !d[te]) { int p(q[hd ++]); for (edge* e = head[p]; e; e = e-> next) if (e-> c && !d[e-> t]) { d[e-> t] = d[p] + 1; q[tl ++] = e-> t; } } return d[te]; } int dfsFind(int p, int c) { if (p == te) return c; int s(0); edge *e; for (e = head[p]; e && c; e = e-> next) if (e-> c && d[e-> t] == d[p] + 1) { int x(dfsFind(e-> t, min(c, e-> c))); c -= x; s += x; e-> c -= x; e-> rv-> c += x; } if (!e) d[p] = 0; return s; }

int main() { #ifndef ONLINE_JUDGE freopen(".in", “r”, stdin); #endif

scanf("%d", &n);
rt = 0;
tnid = tv = 0;
ans = 0;
st = ++ tnid, te = ++ tnid;
for (int i = 0; i < n; ++ i) {
	a[i]. read();
	va[tv ++] = a[i]. l;
	va[tv ++] = a[i]. r;
	va[tv ++] = a[i]. a;
	a[i]. lid = a[i]. rid = ++ tnid;
	a[i]. mid = ++ tnid;
	//a[i]. rid = ++ tnid;
	ans += a[i]. b + a[i]. w;
}
sort(va, va + tv);
tv = unique(va, va + tv) - va;
for (int i = 0; i < n; ++ i) {
	a[i]. l = lower_bound(va, va + tv, a[i]. l) - va;
	a[i]. r = lower_bound(va, va + tv, a[i]. r) - va;
	a[i]. a = lower_bound(va, va + tv, a[i]. a) - va;
}
for (int i = 0; i < n; ++ i) {
	addEdge(st, a[i]. lid, a[i]. b);
	//addEdge(a[i]. lid, a[i]. rid, inf);
	addEdge(a[i]. rid, te, a[i]. w);
	addEdge(a[i]. lid, a[i]. mid, a[i]. p);
	sgtQry(rt, a[i]. mid, a[i]. l, a[i]. r + 1, 0, tv);
	rt = sgtChg(rt, a[i]. a, i, 0, tv);
}
while (bfsBuild())
	ans -= dfsFind(st, inf);
printf("%d\n", ans);

}