#include<bits/stdc++.h>
#define inf 1e9+8
#define eps 1e-6
#define mp make_pair
#define pb push_back
#define fr first
#define sd second
#define pa pair<ll,ll>
#define FOR(i,a,b) for(ll i=a;i<=b;i++)
#define REP(i,a,b) for(ll i=a;i>=b;i--)
#define MEM(a) memset(a,0,sizeof(a))
#define M 2000010
#define N 5010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
inline ll read()
{
	char ch=getchar();
	ll s=0,w=1;
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}
	return s*w;
}
inline ll lowbit(ll x){return x&(-x);}
struct edge
{
	ll nxt,to,fl;
}e[M<<1];
ll head[N],cur[N],cnt=1;
ll n;
ll x[N],y[N],w[N],b[2][2];
ll ans,S,T;
ll dep[N];queue<ll>Q;
map<pa,ll>ma;
inline void add(ll x,ll y,ll fl)
{
	e[++cnt]=(edge){head[x],y,fl};head[x]=cnt;
	e[++cnt]=(edge){head[y],x,+0};head[y]=cnt;
}
inline ll bfs()
{
	FOR(i,S,T)dep[i]=0,cur[i]=head[i];while(!Q.empty())Q.pop();
	Q.push(S);dep[S]=1;
	while(!Q.empty())
	{
		ll x=Q.front();Q.pop();
		for(ll i=head[x];i;i=e[i].nxt)if(e[i].fl&&!dep[e[i].to])
		{
			ll y=e[i].to;dep[y]=dep[x]+1;Q.push(y);
			if(y==T)return 1;
		}
	}
	return 0;
}
ll dfs(ll x,ll flow)
{
	if(x==T)return flow;
	ll res=0;
	for(ll &i=cur[x];i;i=e[i].nxt)if(dep[e[i].to]==dep[x]+1&&e[i].fl)
	{
		if(res==flow)return res;
		ll y=e[i].to,fl=dfs(y,min(e[i].fl,flow-res));
		res+=fl;e[i].fl-=fl,e[i^1].fl+=fl;
	}
	if(!res)dep[x]=0;
	return res;
}
inline ll Dinic()
{
	ll ans=0;
	while(bfs()){ans+=dfs(S,inf);}
	return ans;
}
int main()
{
	//ios::sync_with_stdio(false);
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	b[0][0]=1,b[1][0]=0,b[0][1]=2,b[1][1]=3;
	n=read();FOR(i,1,n)x[i]=read(),y[i]=read(),w[i]=read();
	FOR(i,1,n)ma[mp(x[i],y[i])]=i;
	FOR(i,1,n)ans+=w[i];S=0;T=n*2+1;
	FOR(i,1,n)add(i,i+n,w[i]);
	FOR(i,1,n)
	{
		ll t=b[x[i]&1][y[i]&1];
		if(t==0)add(S,i,inf);
		if(t==3)add(i+n,T,inf);
		FOR(j,1,n)if(abs(x[i]-x[j])+abs(y[i]-y[j])<=1&&b[x[j]&1][y[j]&1]==t+1)add(i+n,j,inf),add(i+n,j,inf),add(i+n,j,inf);
	}
	//cout<<"GG:"<<Dinic()<<'\n';
	cout<<ans-Dinic()<<'\n';
	return 0;
}
/*
9
2 2 100
1 1 1
1 2 1
1 3 1
2 1 1
2 3 1
3 1 1
3 2 1
3 3 1
*/
 