#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; struct S { ll x, y, t; }; vector a(n); for(auto& [x, y, t]: a) cin >> x >> y >> t; ll l = -1, r = 1LL << 40; auto f2 = [](ll x, ll y) { return x * x + y * y; }; while(r - l > 1) { ll c = (l + r) / 2; dsu d(n); rep(i, n) rep(j, i) { if (a[i].t == a[j].t) { if (f2(a[i].x - a[j].x, a[i].y - a[j].y) <= c) { d.merge(i, j); } } else { ll x2 = f2(a[i].x, a[i].y); ll y2 = f2(a[j].x, a[j].y); ll z = max(0LL, x2 + y2 - c); if (__int128(z) * z <= __int128(4) * x2 * y2) { d.merge(i, j); } } } if (d.same(0, n - 1)) { r = c; } else { l = c; } } cout << r << '\n'; }