#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ld, ll>;
const ld pi = acosl(-1);
const ld eps = 1e-13;

template <typename T>
class SegmentTree {
	using func_t = function<T(T, T)>;
	const int n;
	const T id;
	func_t merge;
	vector<T> data;
	int size(int n) {
		int res;
		for (res = 1; res < n; res <<= 1);
		return res;
	}
	T sub(int l, int r, int node, int lb, int ub) {
		if (ub <= l || r <= lb) return id;
		if (l <= lb && ub <= r) return data[node];
		return merge(sub(l, r, node * 2, lb, (lb + ub) / 2), sub(l, r, node * 2 + 1, (lb + ub) / 2, ub));
	}
public:
	SegmentTree() : n(0), id(0) {}
	SegmentTree(const SegmentTree& st) :
		n(st.n), id(st.id), merge(st.merge), data(st.data) {}
	SegmentTree(SegmentTree&& st) :
		n(st.n), id(st.id), merge(st.merge), data(st.data) {}
	SegmentTree(int n_, T id_, func_t merge_) :
		n(size(n_)), id(id_), merge(merge_), data(size(n_) * 2, id_) {}
	SegmentTree(const vector<T>& data_, T id_, func_t merge_) :
		n(size(data_.size())), id(id_), merge(merge_), data(n * 2, id_) {
		for (int i = 0; i < (int)data_.size(); i++)
			data[i + n] = data_[i];
		for (int i = n - 1; i >= 0; i--)
			data[i] = merge(data[i * 2], data[i * 2 + 1]);
	}
	SegmentTree(vector<T>&& data_, T id_, func_t merge_) :
		n(size(data_.size())), id(id_), merge(merge_), data(n * 2, id_) {
		for (int i = 0; i < (int)data_.size(); i++)
			data[i + n] = data_[i];
		for (int i = n - 1; i >= 0; i--)
			data[i] = merge(data[i * 2], data[i * 2 + 1]);
	}
	void Update(int p, T val) {
		p += n;
		data[p] = val;
		while (p >>= 1) data[p] = merge(data[p * 2], data[p * 2 + 1]);
	}
	void Add(int p, T val) {
		p += n;
		data[p] += val;
		while (p >>= 1) data[p] = merge(data[p * 2], data[p * 2 + 1]);
	}
	T Find(int i) {
		return data[i + n];
	}
	T Find(int l, int r) {
		return sub(l, r + 1, 1, 0, n);
	}
};

ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}

int main()
{
	ll n;
	cin >> n;
	map<pair<ll, ll>, ll> mp;
	vector<ll> a(n), b(n), p(n), kouho(1);
	vector<P> s(n * 3, P(0.0, 10000000000ll));
	for (int i = 0; i < n; i++) {
		cin >> a[i] >> b[i] >> p[i];
		int g = gcd(abs(a[i]), abs(b[i]));
		if (g) {
			a[i] /= g;
			b[i] /= g;
		}
		if (a[i] | b[i]) {
			if (mp[pair<ll, ll>(a[i], b[i])]) {
				mp[pair<ll, ll>(a[i], b[i])] = min(mp[pair<ll, ll>(a[i], b[i])], p[i]);
			}
			else {
				mp[pair<ll, ll>(a[i], b[i])] = p[i];
			}
			if (p[i] < p[kouho[0]]) {
				kouho[0] = i;
			}
			s[i * 3] = P(atan2l(b[i], a[i]), p[i]);
			s[i * 3 + 1] = P(atan2l(b[i], a[i]) + pi * 2.0, p[i]);
			s[i * 3 + 2] = P(atan2l(b[i], a[i]) + pi * 4.0, p[i]);
		}
	}
	ll id = -1, mi = 10000000000ll;
	for (int i = 0; i < n; i++) {
		if (a[i] == -a[kouho[0]] && b[i] == -b[kouho[0]] && mi > p[i]) {
			mi = p[i];
			id = i;
		}
	}
	if (id != -1) {
		kouho.push_back(id);
	}
	sort(s.begin(), s.end());
	vector<ll> ba(n * 3);
	for (int i = 0; i < n * 3; i++) {
		ba[i] = s[i].second;
	}
	SegmentTree<ll> st(ba, 10000000000ll, [](ll l, ll r) { return min(l, r); });
	ll res = 10000000000000ll;
	for (auto v : kouho) {
		int ss = upper_bound(s.begin(), s.end(), P(atan2l(b[v], a[v]), 10000000000ll)) - s.begin();
		int ee = lower_bound(s.begin(), s.end(), P(atan2l(b[v], a[v]) + pi - eps, 0)) - s.begin();
		int cc = upper_bound(s.begin(), s.end(), P(atan2l(b[v], a[v]) + pi + eps, 10000000000ll)) - s.begin();
		for (int i = ss; i < ee; i++) {
			int ce = lower_bound(s.begin(), s.end(), P(s[i].first + pi - eps, 0)) - s.begin();
			if (cc < ce) {
				res = min(res, p[v] + s[i].second + st.Find(cc, ce - 1));
			}
		}
	}
	for (int i = 0; i < n; i++) {
		if (mp[pair<ll, ll>(-b[i], a[i])] && mp[pair<ll, ll>(b[i], -a[i])] && mp[pair<ll, ll>(-a[i], -b[i])]) {
			res = min(res, mp[pair<ll, ll>(a[i], b[i])] + mp[pair<ll, ll>(-b[i], a[i])] + mp[pair<ll, ll>(b[i], -a[i])] + mp[pair<ll, ll>(-a[i], -b[i])]);
		}
	}
	cout << (res >= 10000000000ll ? -1 : res) << endl;
	return 0;
}

Measured memory_bytes: 72360