#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#define rep(i,n) for(int i = 0; i < n; ++i)
using namespace std;
int a[210000];
int b[210000];
int p[210000];
double PI=acos(-1.0);
double eps=1e-12;
struct wolf{
	int bb;int bs;
	int t;
	wolf(){
	}
	wolf(int x,int y,int z){
		bb=x;bs=y;t=z;
	}
	bool operator<(const wolf& b)const{
		if(t!=b.t)return t<b.t;
		return (long long)bs*b.bb<(long long)b.bs*bb;
	}
	bool operator !=(const wolf &b)const{
		return t!=b.t||(long long)bs*b.bb!=(long long)b.bs*bb;
	}
};
pair<wolf,int> v[630000];
int segtree[1048576];
double ABS(double a){return max(a,-a);}
int query(int a,int b,int c,int d,int e){
	if(c>d)return 1010101010;
	if(d<a||b<c)return 1010101010;
	if(c<=a&&b<=d)return segtree[e];
	return min(query(a,(a+b)/2,c,d,e*2),query((a+b)/2+1,b,c,d,e*2+1));
}
void update(int a,int b){
	a+=524288;
	while(a){
		segtree[a]=min(segtree[a],b);
		a/=2;
	}
}
int main() {
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)scanf("%d%d%d",a+i,b+i,p+i);
	for(int i=0;i<n;i++){
		if(a[i]==0&&b[i]==0){
			v[i]=make_pair(wolf(0,0,9999999),1010101010);
			v[i+n]=make_pair(wolf(0,0,9999999),1010101010);
			continue;
		}
		int t=0;
		if(a[i]<0)t=1;
		else if(a[i]==0&&b[i]<0)t=1;
		v[i]=make_pair(wolf(a[i],b[i],t),p[i]);
		v[i+n]=make_pair(wolf(a[i],b[i],2+t),p[i]);
	}
	std::sort(v,v+2*n);
	long long ret=100000000000000LL;
	for(int i=0;i<1048576;i++)segtree[i]=1010101010;
	for(int i=0;i<2*n;i++)update(i,v[i].second);
	for(int i=0;i<n;i++){
		wolf now=v[i].first;
		if(v[i].first.t>=2)continue;
		wolf Llim=wolf(-now.bs,now.bb,now.t);
		if(now.bb>=0&&now.bs>0)Llim.t++;
		if(now.bb<=0&&now.bs<0)Llim.t++;
		wolf Mlim=wolf(-now.bb,-now.bs,now.t+1);
		wolf Rlim=wolf(now.bs,-now.bb,now.t+1);
		if(now.bs<0&&now.bb<=0)Rlim.t++;
		if(now.bs>0&&now.bb>=0)Rlim.t++;
	//	printf("(%d %d %d), (%d %d %d), (%d %d %d), (%d %d %d)\n",now.bb,now.bs,now.t,Llim.bb,Llim.bs,Llim.t,Mlim.bb,Mlim.bs,Mlim.t,Rlim.bb,Rlim.bs,Rlim.t);
		long long L=query(0,524287,upper_bound(v,v+n*2,make_pair(Llim,1010101010))-v,lower_bound(v,v+n*2,make_pair(Mlim,0))-v-1,1);
		long long R=query(0,524287,upper_bound(v,v+n*2,make_pair(Mlim,1010101010))-v,lower_bound(v,v+n*2,make_pair(Rlim,0))-v-1,1);
	//	printf("%lld %lld\n",L,R);
		if(L<1010101010&&R<1010101010)ret=min(ret,(long long)v[i].second+L+R);
	}
	long long m1=1000000000000000LL;wolf at1(123,123,111);
	long long m2=1000000000000000LL;wolf at2(123,123,222);
	for(int i=0;i<n;i++){
		if(v[i].first.t>1)continue;
 
	//	printf("%d %d %d\n",v[i].first.bb,v[i].first.bs,v[i].first.t);
		long long val=v[i].second+query(0,524287,lower_bound(v,v+n*2,make_pair(wolf(-v[i].first.bb,-v[i].first.bs,v[i].first.t+1),0))-v,upper_bound(v,v+n*2,make_pair(wolf(-v[i].first.bb,-v[i].first.bs,v[i].first.t+1),1010101010))-v-1,1);
		if(v[i].second<1010101010&&query(0,524287,lower_bound(v,v+n*2,make_pair(wolf(-v[i].first.bb,-v[i].first.bs,v[i].first.t+1),0))-v,upper_bound(v,v+n*2,make_pair(wolf(-v[i].first.bb,-v[i].first.bs,v[i].first.t+1),1010101010))-v-1,1)<1010101010){
			if(m1>val){
				if(at1!=v[i].first&&at1!=wolf(-v[i].first.bb,-v[i].first.bs,v[i].first.t-1)){
					m2=m1;at2=at1;
				}
				m1=val;
				at1=v[i].first;
			}else if(m2>val&&at1!=v[i].first&&at1!=wolf(-v[i].first.bb,-v[i].first.bs,v[i].first.t-1)){
				m2=val;at2=v[i].first;
			}
		}
	}
	//printf("%lld\n",ret);
	ret=min(ret,m1+m2);
	if(ret>1000000000000LL)printf("-1\n");
	else printf("%lld\n",ret);
}

Measured memory_bytes: 17568