#include using namespace std; struct UnionFind { vector par; vector size; UnionFind(int n) { par.resize(n); size.resize(n,1); for(int i = 0; i < n; i++) { par[i] = i; } } int find(int x) { if(par[x] == x) { return x; } return par[x] = find(par[x]); } bool same(int x, int y) { return find(x) == find(y); } int consize(int x) { return size[find(x)]; } bool unite(int x, int y) { x = find(x); y = find(y); if(x == y) { return false; } if(size[x] > size[y]) { swap(x,y); } par[x] = y; size[y] += size[x]; return true; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vectorX(N),Y(N),T(N); for(int i = 0; i < N; i++) { cin >> X[i] >> Y[i] >> T[i]; } vector>tmp; for(int i = 0; i < N; i++) { for(int j = i+1; j < N; j++) { int a = X[i]*X[i]+Y[i]*Y[i]; int b = X[j]*X[j]+Y[j]*Y[j]; if(T[i] == T[j]) { long long c = (X[i]-X[j])*(X[i]-X[j])+(Y[i]-Y[j])*(Y[i]-Y[j]); tmp.push_back({c,i,j}); } else { if(a < b) swap(a,b); long long c = floor(a+b-2.0*sqrt(1ll*a*b)); tmp.push_back({c,i,j}); } } } sort(tmp.begin(),tmp.end()); UnionFind uf(N); for(int i = 0; i < tmp.size(); i++) { uf.unite(tmp[i][1],tmp[i][2]); if(uf.same(0,N-1)) { cout << tmp[i][0] << endl; return 0; } } }