#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#include<ctime>
#include<set>
#include<map>
#include<queue>
#include<stack>
#define sqr(x) ((x)*(x))
#define fz1(i,n) for ((i)=1;(i)<=(n);(i)++)
#define fd1(i,n) for ((i)=(n);(i)>=1;(i)--)
#define fz0g(i,n) for ((i)=0;(i)<=(n);(i)++)
#define fd0g(i,n) for ((i)=(n);(i)>=0;(i)--)
#define fz0k(i,n) for ((i)=0;(i)<(n);(i)++)
#define fd0k(i,n) for ((i)=(long long)((n)-1);(i)>=0;(i)--)
#define fz(i,x,y) for ((i)=(x);(i)<=(y);(i)++)
#define fd(i,y,x) for ((i)=(y);(i)>=(x);(i)--)
#define fzin fz1(i,n)
#define fzim fz1(i,m)
#define fzjn fz1(j,n)
#define fzjm fz1(j,m)
#define ff(c,itr) for (__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define rdst(st,len){static char ss[len];scanf(" %s",ss);(st)=ss;}
#define inc(x,y) {x+=(y);if(x>=mod)x-=mod;}
#define dec(x,y) {x-=(y);if(x<0)x+=mod;}
using namespace std;
long long n,m,i,j,s=0,t=2005,vis[2018],inq[2018],x,y,c,cnt;
long long cst,dis[2018];
struct ii
{
	long long y,z,d;
}e[1000005];
vector<long long> bi[2018];
void add(long long x,long long y,long long z,long long d)
{
	cnt++;
	bi[x].push_back(cnt+cnt-2);
	bi[y].push_back(cnt+cnt-1);
	e[cnt+cnt-2]=(ii){y,z,d};
	e[cnt+cnt-1]=(ii){x,0,-d}; 
}
bool bfs(long long s,long long t)
{
	queue<long long> qx;
	qx.push(s);
	memset(dis,0x3f,sizeof(dis));
	dis[s]=0;qx.push(s);
	while(!qx.empty()){
		long long x=qx.front();qx.pop();inq[x]=0;
		ff(bi[x],it){
			if(e[*it].z){
				long long y=e[*it].y,z=e[*it].d+dis[x];
				if(dis[y]>z){
					dis[y]=z;
					if(!inq[y]){
						inq[y]=1;
						qx.push(y);
					}
				}
			}
		}
	}
	return dis[t]<dis[t+1];
}
long long dfs(long long x,long long tt,long long flw)
{
	if(!flw)return 0;
	if(x==tt){
		vis[x]=1;
		return flw;
	}
	if(vis[x])return 0;
	vis[x]=1;
	long long ans=0;
	ff(bi[x],it){
		if(dis[x]+e[*it].d==dis[e[*it].y]&&e[*it].z){
			long long t=dfs(e[*it].y,tt,min(flw,e[*it].z));
			ans+=t;flw-=t;e[*it].z-=t;e[(*it)^1].z+=t;
			cst+=e[*it].d*t;
		}
		if(!flw)break;
	}
	return ans;
}
void dinic(long long s,long long t)
{
	while(bfs(s,t)){
		do{
			memset(vis,0,sizeof(vis));
			dfs(s,t,0x3f3f3f3f);
		}while(vis[t]);
	}
}
int main()
{
	scanf("%lld",&n);
	fz1(i,n){
		scanf("%lld%lld%lld",&x,&y,&c);
		add(s,i,c,0);
		add(i,2001,0x3f3f3f3f,x+y);
		add(i,2002,0x3f3f3f3f,-x+y);
		add(i,2003,0x3f3f3f3f,x-y);
		add(i,2004,0x3f3f3f3f,-x-y);
	}
	fz1(i,n){
		scanf("%lld%lld%lld",&x,&y,&c);
		add(i+n,t,c,0);
		add(2001,i+n,0x3f3f3f3f,-x-y);
		add(2002,i+n,0x3f3f3f3f,x-y);
		add(2003,i+n,0x3f3f3f3f,-x+y);
		add(2004,i+n,0x3f3f3f3f,x+y);
	}
	dinic(s,t);
	cout<<-cst<<endl;
	return 0;
}
### Prompt

Please create a solution in Cpp to the following problem:

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 <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#include<ctime>
#include<set>
#include<map>
#include<queue>
#include<stack>
#define sqr(x) ((x)*(x))
#define fz1(i,n) for ((i)=1;(i)<=(n);(i)++)
#define fd1(i,n) for ((i)=(n);(i)>=1;(i)--)
#define fz0g(i,n) for ((i)=0;(i)<=(n);(i)++)
#define fd0g(i,n) for ((i)=(n);(i)>=0;(i)--)
#define fz0k(i,n) for ((i)=0;(i)<(n);(i)++)
#define fd0k(i,n) for ((i)=(long long)((n)-1);(i)>=0;(i)--)
#define fz(i,x,y) for ((i)=(x);(i)<=(y);(i)++)
#define fd(i,y,x) for ((i)=(y);(i)>=(x);(i)--)
#define fzin fz1(i,n)
#define fzim fz1(i,m)
#define fzjn fz1(j,n)
#define fzjm fz1(j,m)
#define ff(c,itr) for (__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define rdst(st,len){static char ss[len];scanf(" %s",ss);(st)=ss;}
#define inc(x,y) {x+=(y);if(x>=mod)x-=mod;}
#define dec(x,y) {x-=(y);if(x<0)x+=mod;}
using namespace std;
long long n,m,i,j,s=0,t=2005,vis[2018],inq[2018],x,y,c,cnt;
long long cst,dis[2018];
struct ii
{
	long long y,z,d;
}e[1000005];
vector<long long> bi[2018];
void add(long long x,long long y,long long z,long long d)
{
	cnt++;
	bi[x].push_back(cnt+cnt-2);
	bi[y].push_back(cnt+cnt-1);
	e[cnt+cnt-2]=(ii){y,z,d};
	e[cnt+cnt-1]=(ii){x,0,-d}; 
}
bool bfs(long long s,long long t)
{
	queue<long long> qx;
	qx.push(s);
	memset(dis,0x3f,sizeof(dis));
	dis[s]=0;qx.push(s);
	while(!qx.empty()){
		long long x=qx.front();qx.pop();inq[x]=0;
		ff(bi[x],it){
			if(e[*it].z){
				long long y=e[*it].y,z=e[*it].d+dis[x];
				if(dis[y]>z){
					dis[y]=z;
					if(!inq[y]){
						inq[y]=1;
						qx.push(y);
					}
				}
			}
		}
	}
	return dis[t]<dis[t+1];
}
long long dfs(long long x,long long tt,long long flw)
{
	if(!flw)return 0;
	if(x==tt){
		vis[x]=1;
		return flw;
	}
	if(vis[x])return 0;
	vis[x]=1;
	long long ans=0;
	ff(bi[x],it){
		if(dis[x]+e[*it].d==dis[e[*it].y]&&e[*it].z){
			long long t=dfs(e[*it].y,tt,min(flw,e[*it].z));
			ans+=t;flw-=t;e[*it].z-=t;e[(*it)^1].z+=t;
			cst+=e[*it].d*t;
		}
		if(!flw)break;
	}
	return ans;
}
void dinic(long long s,long long t)
{
	while(bfs(s,t)){
		do{
			memset(vis,0,sizeof(vis));
			dfs(s,t,0x3f3f3f3f);
		}while(vis[t]);
	}
}
int main()
{
	scanf("%lld",&n);
	fz1(i,n){
		scanf("%lld%lld%lld",&x,&y,&c);
		add(s,i,c,0);
		add(i,2001,0x3f3f3f3f,x+y);
		add(i,2002,0x3f3f3f3f,-x+y);
		add(i,2003,0x3f3f3f3f,x-y);
		add(i,2004,0x3f3f3f3f,-x-y);
	}
	fz1(i,n){
		scanf("%lld%lld%lld",&x,&y,&c);
		add(i+n,t,c,0);
		add(2001,i+n,0x3f3f3f3f,-x-y);
		add(2002,i+n,0x3f3f3f3f,x-y);
		add(2003,i+n,0x3f3f3f3f,-x+y);
		add(2004,i+n,0x3f3f3f3f,x+y);
	}
	dinic(s,t);
	cout<<-cst<<endl;
	return 0;
}
```