Problem:
Score : 1200 points

Problem Statement

Snuke is playing with red and blue balls, placing them on a two-dimensional plane.

First, he performed N operations to place red balls. In the i-th of these operations, he placed RC_i red balls at coordinates (RX_i,RY_i).
Then, he performed another N operations to place blue balls. In the i-th of these operations, he placed BC_i blue balls at coordinates (BX_i,BY_i).
The total number of red balls placed and the total number of blue balls placed are equal, that is, \sum_{i=1}^{N} RC_i = \sum_{i=1}^{N} BC_i. Let this value be S.

Snuke will now form S pairs of red and blue balls so that every ball belongs to exactly one pair.
Let us define the score of a pair of a red ball at coordinates (rx, ry) and a blue ball at coordinates (bx, by) as |rx-bx| + |ry-by|.

Snuke wants to maximize the sum of the scores of the pairs.
Help him by finding the maximum possible sum of the scores of the pairs.

Constraints

1 \leq N \leq 1000

0 \leq RX_i,RY_i,BX_i,BY_i \leq 10^9

1 \leq RC_i,BC_i \leq 10

\sum_{i=1}^{N} RC_i = \sum_{i=1}^{N} BC_i

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
RX_1 RY_1 RC_1
RX_2 RY_2 RC_2
\vdots
RX_N RY_N RC_N
BX_1 BY_1 BC_1
BX_2 BY_2 BC_2
\vdots
BX_N BY_N BC_N

Output

Print the maximum possible sum of the scores of the pairs.

Sample Input 1

2
0 0 1
3 2 1
2 2 1
5 0 1

Sample Output 1

8

If we pair the red ball at coordinates (0,0) and the blue ball at coordinates (2,2), the score of this pair is |0-2| + |0-2|=4.
Then, if we pair the red ball at coordinates (3,2) and the blue ball at coordinates (5,0), the score of this pair is |3-5| + |2-0|=4.
Making these two pairs results in the total score of 8, which is the maximum result.

Sample Input 2

3
0 0 1
2 2 1
0 0 2
1 1 1
1 1 1
3 3 2

Sample Output 2

16

Snuke may have performed multiple operations at the same coordinates.

Sample Input 3

10
582463373 690528069 8
621230322 318051944 4
356524296 974059503 6
372751381 111542460 9
392867214 581476334 6
606955458 513028121 5
882201596 791660614 9
250465517 91918758 3
618624774 406956634 6
426294747 736401096 5
974896051 888765942 5
726682138 336960821 3
715144179 82444709 6
599055841 501257806 6
390484433 962747856 4
912334580 219343832 8
570458984 648862300 6
638017635 572157978 10
435958984 585073520 7
445612658 234265014 6

Sample Output 3

45152033546

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
#define rep(i, n) for (rint i = 1; i <= (n); i ++)
#define re0(i, n) for (rint i = 0; i < (int) n; i ++)
#define travel(i, u) for (rint i = head[u]; i; i = e[i].nxt)
#define rint register int
using namespace std;
 
typedef long long lo;
 
template<typename tp> inline void read(tp &x) {
    x = 0; char c = getchar(); int f = 0;
    for (; c < '0' || c > '9'; f |= c == '-', c = getchar());
    for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar());
    if (f) x = -x;
}

namespace mf {
	const int N = 1e4 + 233;
	const int M = 2e5 + 233;
	const lo MAXFLOW = 1e9;
	const lo MAXCOST = 1e15;
	struct E {
		int nxt, to; lo fl, w;
	} e[M];
	int head[N], e_cnt = 1, cur[N];
	lo d[N]; bool book[N];
	int all;
	inline void init(int n) {
		all = n;
		re0 (i, all + 1) head[i] = 0;
		e_cnt = 1;
	}
	inline void adde(int x, int y, lo fl, lo w) {
		e[++e_cnt] = (E) { head[x], y, fl, w }; head[x] = e_cnt;
	}
	inline void link(int x, int y, lo fl, lo w) {
		adde(x, y, fl, w); adde(y, x, 0, -w);
	}
	inline bool spfa(int S, int T) {
		re0 (i, all + 1) d[i] = MAXCOST, book[i] = 0;
		queue <int> Q;
		book[S] = true; d[S] = 0; Q.push(S);
		while (!Q.empty()) {
			int u = Q.front(); Q.pop(); book[u] = false;
			for (int i = head[u]; i; i = e[i].nxt)
				if (e[i ^ 1].fl) {
					int v = e[i].to;
					if (d[v] > d[u] + e[i ^ 1].w) {
						d[v] = d[u] + e[i ^ 1].w;
						if (!book[v]) Q.push(v), book[v] = true;
					}
				}
		}
		return d[T] != MAXCOST;
	}
	inline int dfs(int u, lo f, int T) {
		book[u] = true;
		if (u == T || !f) return f;
		lo used = 0, t;
		for (int &i = cur[u]; i; i = e[i].nxt)
			if (e[i].fl) {
				int v = e[i].to;
				if (book[v] || d[u] != d[v] + e[i].w) continue;
				t = dfs(v, min(f - used, e[i].fl), T);
				e[i].fl -= t; e[i ^ 1].fl += t; used += t;
				if (used == f) break;
			}
		if (used == 0) d[u] = MAXCOST;
		return used;
	}
	pair <lo, lo> doit(int S, int T) {
		int maxflow = 0; lo mincost = 0;
		while (spfa(T, S)) {
			re0 (i, all + 1) cur[i] = head[i], book[i] = 0;
			lo t = dfs(S, MAXFLOW, T);
			maxflow += t;
			mincost += t * d[S];
		}
		return make_pair(maxflow, mincost);
	}
}
 
using mf::link;

int dx[] = {1, 1, -1, -1};
int dy[] = {1, -1, 1, -1};
 
int n, id[4], S, T, tot;
 
signed main(void) {
	read(n);
	for (int k = 0; k < 4; k++) id[k] = ++tot;
	S = ++tot; T = ++tot;
	int sum = 0;
	for (int i = 1; i <= n; i++) {
		int x, y, c;
		read(x); read(y); read(c);
		sum += c;
		int u = ++tot;
		link(S, u, c, 0);
		vector <int> cid(4, 0);
		for (int k = 0; k < 4; k++) {
			int nx = x * dx[k];
			int ny = y * dy[k];
			cid[k] = ++tot;
			link(u, cid[k], c, nx + ny);
			link(cid[k], id[3 - k], c, 0);
			// cout << k << " " << nx << " " << ny << " " << 3 - k << "\n";
		}
	}
	for (int i = 1; i <= n; i++) {
		int x, y, c;
		read(x); read(y); read(c);
		int u = ++tot;
		link(u, T, c, 0);
		vector <int> cid(4, 0);
		for (int k = 0; k < 4; k++) {
			int nx = x * dx[k];
			int ny = y * dy[k];
			cid[k] = ++tot;
			link(cid[k], u, c, nx + ny);
			link(id[k], cid[k], c, 0);
			// cout << k << " " << nx << " " << ny << " " << 3 - k << "\n";
		}
	}
	mf::all = tot;
	auto it = mf::doit(S, T);
	cout << -it.second << "\n";
	assert(it.first == sum);
}