#include #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define rep(i,n) for(int i=0;i<(int)(n);i++) #define codefor int test;cin>>test;while(test--) #define INT(...) int __VA_ARGS__;in(__VA_ARGS__) #define LL(...) ll __VA_ARGS__;in(__VA_ARGS__) #define vector2d(type,name,h,...) vector>name(h,vector(__VA_ARGS__)) #define vector3d(type,name,h,w,...) vector>>name(h,vector>(w,vector(__VA_ARGS__))) using namespace std; using ll = long long; template using rpriority_queue = priority_queue, greater>; template istream& operator>>(istream& is, vector& vec) {for(T& x : vec)is >> x;return is;} template ostream& operator<<(ostream& os, const vector& vec) {if(vec.empty())return os;os << vec[0];for(auto it = vec.begin(); ++it!= vec.end();)os << ' ' << *it;return os;} void in(){} template void in(Head& head, Tail&... tail){cin >> head;in(tail...);} void out(){cout << '\n';} templatevoid out(const T& a){cout << a << '\n';} template void out(const Head& head,const Tail&... tail){cout << head << ' ';out(tail...);} const int INF = 1 << 30; const long long INF2 = 1ll << 60; template void chmax(T &a,const T b){if(b>a)a=b;} template void chmin(T &a,const T b){if(b> groups() { std::vector leader_buf(_n), group_size(_n); for (int i = 0; i < _n; i++) { leader_buf[i] = leader(i); group_size[leader_buf[i]]++; } std::vector> result(_n); for (int i = 0; i < _n; i++) { result[i].reserve(group_size[i]); } for (int i = 0; i < _n; i++) { result[leader_buf[i]].push_back(i); } result.erase( std::remove_if(result.begin(), result.end(), [&](const std::vector& v) { return v.empty(); }), result.end()); return result; } private: int _n; // root node: -1 * component size // otherwise: parent std::vector parent_or_size; }; int main(){ ios::sync_with_stdio(false); cin.tie(0); INT(n); vector> a(n); ll x, y, t, x1, y1, t1; for(int i = 0; i < n; i++){ cin >> x >> y >> t; a[i] = make_tuple(x, y, t); } auto g = [&](ll s, ll x, ll y, ll x1, ll y1){ ll c1 = x * x + y * y, c2 = x1 * x1 + y1 * y1; ll c = c1 + c2 - s; if(c1 == c2) return true; if(c1 < c2)swap(c1, c2); ll ok = 0, ng = 1ll << 30, mid; while(ok + 1 < ng){ mid = (ok + ng) / 2; if(mid * mid <= c1 * c2)ok = mid; else ng = mid; } return (c1 + c2 - 2 * ok <= s); }; auto f = [&](ll s){ dsu uf(n); for(int i = 0; i < n; i++){ tie(x, y, t) = a[i]; for(int j = i + 1; j < n; j++){ tie(x1, y1, t1) = a[j]; if(t != t1){ if(g(s, x, y, x1, y1))uf.merge(i, j); }else{ ll dx = x - x1, dy = y - y1; if(dx * dx + dy * dy <= s)uf.merge(i, j); } } } return uf.same(0, n - 1); }; ll ng = -1, ok = 1ll << 60, mid; while(ng + 1 < ok){ mid = (ng + ok) / 2; if(f(mid))ok = mid; else ng = mid; } out(ok); }