#include #include using namespace std; using namespace atcoder; void fast_io() { ios::sync_with_stdio(false); std::cin.tie(nullptr); } long long sq_floor(long long x) { long long lo = 0, hi = 9e9; while (lo + 1 < hi) { long long mid = (lo + hi) / 2; if (mid * mid <= x) { lo = mid; } else { hi = mid; } } return lo; } int main() { fast_io(); int n; cin >> n; vector x(n), y(n), t(n); for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> t[i]; } vector r(n); for (int i = 0; i < n; i++) { r[i] = x[i] * x[i] + y[i] * y[i]; } vector> edges; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (t[i] == t[j]) { edges.push_back({(x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]), i, j}); } else { edges.push_back( {r[i] + r[j] - 2 * sq_floor((long long)r[i] * r[j]), i, j}); } } } sort(edges.begin(), edges.end()); dsu uf(n); for (auto [w, u, v] : edges) { uf.merge(u, v); if (uf.same(0, n - 1)) { cout << w << endl; return 0; } } }