#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=20005,INF=1e18;
struct Node{
	int x,y,w,c;
	Node(int xx=0,int yy=0,int ww=0,int cc=0){
		x=xx;y=yy;w=ww;c=cc;
	}
} Edge[N*8];
int n,v1[N],v2[N],s,t,a[N],root1,root2,x,y,m;
int stot,nxt[N*8],fa1[N],fa2[N],fi1[N],ne1[N],zz1[N],fi2[N],ne2[N],zz2[N],tot1,tot2,head[N];
inline void jb(int x,int y,int w,int c){
	Edge[++stot]=Node(x,y,w,c);
	nxt[stot]=head[x];
	head[x]=stot;
}
inline void jb2(int x,int y,int w,int c){
	jb(x,y,w,c);
	jb(y,x,0,-c);
}
int d[N];
queue<int> q;
bool inq[N];
inline bool spfa(){
	for (int i=1;i<=t;i++) d[i]=INF,inq[i]=0;
	d[s]=0;q.push(s);inq[s]=1;
	while (!q.empty()){
		int x=q.front();q.pop();inq[x]=0;
		for (register int i=head[x];i;i=nxt[i])
		  	if (Edge[i].w>0&&d[x]+Edge[i].c<d[Edge[i].y]){
		  		d[Edge[i].y]=d[x]+Edge[i].c;
		  		if (!inq[Edge[i].y]){
		  			inq[Edge[i].y]=1;
		  			q.push(Edge[i].y);
		  		}
			}
	}
	return d[t]<INF;
}
int hd[N],Flag[N];
inline int anti(int x){
	return (x&1)? x+1:x-1;
}
bool vis[N];
inline int dfs(int now,int flow){
	if (now==t) return flow;
	int usd=0;	
	vis[now]=1;
	for (register int &i=hd[now];i;i=nxt[i])
	  	if (!vis[Edge[i].y]&&Edge[i].w>0&&d[Edge[i].y]==d[now]+Edge[i].c){
	  		int w=dfs(Edge[i].y,min(Edge[i].w,flow-usd));
	  		Edge[i].w-=w;Edge[anti(i)].w+=w;usd+=w;
	  		if (usd==flow) break;
	  	}
	vis[now]=0;
	if (!usd)d[now]=INF;
	return usd;
}
int answ,z,f[N],f1[N],f2[N],ansc;
inline void mcmf(){
	int tmp;
	while (spfa()){
		memcpy(hd,head,sizeof(int)*(t+2));
		tmp=dfs(s,INF),answ+=tmp,ansc+=tmp*d[t];
	}
}
signed main(){
	scanf("%lld",&n);
	s=0;t=n*10+5;
	for (int i=1;i<=n;i++){
		scanf("%lld%lld%lld",&x,&y,&z);
		jb2(s,i,z,0);
		jb2(i,i+n,z,x+y);jb2(i+n,10*n+1,z,0);
		jb2(i,i+2*n,z,x-y);jb2(i+2*n,10*n+2,z,0);
		jb2(i,i+3*n,z,-x+y);jb2(i+3*n,10*n+3,z,0);
		jb2(i,i+4*n,z,-x-y);jb2(i+4*n,10*n+4,z,0);
	}
	for (int i=1;i<=n;i++){
		scanf("%lld%lld%lld",&x,&y,&z);
		jb2(i+5*n,t,z,0);
		jb2(i+6*n,i+5*n,z,-x-y);jb2(10*n+1,i+6*n,z,0);
		jb2(i+7*n,i+5*n,z,-x+y);jb2(10*n+2,i+7*n,z,0);
		jb2(i+8*n,i+5*n,z,x-y);jb2(10*n+3,i+8*n,z,0);
		jb2(i+9*n,i+5*n,z,x+y);jb2(10*n+4,i+9*n,z,0);
	}
	mcmf();
	printf("%lld\n",-ansc);
	return 0;
}
### Prompt

Develop a solution in cpp to the problem described below:

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.

Examples

Input

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


Output

8


Input

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


Output

16


Input

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


Output

45152033546

### Response

```cpp
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=20005,INF=1e18;
struct Node{
	int x,y,w,c;
	Node(int xx=0,int yy=0,int ww=0,int cc=0){
		x=xx;y=yy;w=ww;c=cc;
	}
} Edge[N*8];
int n,v1[N],v2[N],s,t,a[N],root1,root2,x,y,m;
int stot,nxt[N*8],fa1[N],fa2[N],fi1[N],ne1[N],zz1[N],fi2[N],ne2[N],zz2[N],tot1,tot2,head[N];
inline void jb(int x,int y,int w,int c){
	Edge[++stot]=Node(x,y,w,c);
	nxt[stot]=head[x];
	head[x]=stot;
}
inline void jb2(int x,int y,int w,int c){
	jb(x,y,w,c);
	jb(y,x,0,-c);
}
int d[N];
queue<int> q;
bool inq[N];
inline bool spfa(){
	for (int i=1;i<=t;i++) d[i]=INF,inq[i]=0;
	d[s]=0;q.push(s);inq[s]=1;
	while (!q.empty()){
		int x=q.front();q.pop();inq[x]=0;
		for (register int i=head[x];i;i=nxt[i])
		  	if (Edge[i].w>0&&d[x]+Edge[i].c<d[Edge[i].y]){
		  		d[Edge[i].y]=d[x]+Edge[i].c;
		  		if (!inq[Edge[i].y]){
		  			inq[Edge[i].y]=1;
		  			q.push(Edge[i].y);
		  		}
			}
	}
	return d[t]<INF;
}
int hd[N],Flag[N];
inline int anti(int x){
	return (x&1)? x+1:x-1;
}
bool vis[N];
inline int dfs(int now,int flow){
	if (now==t) return flow;
	int usd=0;	
	vis[now]=1;
	for (register int &i=hd[now];i;i=nxt[i])
	  	if (!vis[Edge[i].y]&&Edge[i].w>0&&d[Edge[i].y]==d[now]+Edge[i].c){
	  		int w=dfs(Edge[i].y,min(Edge[i].w,flow-usd));
	  		Edge[i].w-=w;Edge[anti(i)].w+=w;usd+=w;
	  		if (usd==flow) break;
	  	}
	vis[now]=0;
	if (!usd)d[now]=INF;
	return usd;
}
int answ,z,f[N],f1[N],f2[N],ansc;
inline void mcmf(){
	int tmp;
	while (spfa()){
		memcpy(hd,head,sizeof(int)*(t+2));
		tmp=dfs(s,INF),answ+=tmp,ansc+=tmp*d[t];
	}
}
signed main(){
	scanf("%lld",&n);
	s=0;t=n*10+5;
	for (int i=1;i<=n;i++){
		scanf("%lld%lld%lld",&x,&y,&z);
		jb2(s,i,z,0);
		jb2(i,i+n,z,x+y);jb2(i+n,10*n+1,z,0);
		jb2(i,i+2*n,z,x-y);jb2(i+2*n,10*n+2,z,0);
		jb2(i,i+3*n,z,-x+y);jb2(i+3*n,10*n+3,z,0);
		jb2(i,i+4*n,z,-x-y);jb2(i+4*n,10*n+4,z,0);
	}
	for (int i=1;i<=n;i++){
		scanf("%lld%lld%lld",&x,&y,&z);
		jb2(i+5*n,t,z,0);
		jb2(i+6*n,i+5*n,z,-x-y);jb2(10*n+1,i+6*n,z,0);
		jb2(i+7*n,i+5*n,z,-x+y);jb2(10*n+2,i+7*n,z,0);
		jb2(i+8*n,i+5*n,z,x-y);jb2(10*n+3,i+8*n,z,0);
		jb2(i+9*n,i+5*n,z,x+y);jb2(10*n+4,i+9*n,z,0);
	}
	mcmf();
	printf("%lld\n",-ansc);
	return 0;
}
```