#include int main () { int n = 0; long long x[800] = {}; long long y[800] = {}; int t[800] = {}; int res = 0; long long e[800][800] = {}; long long d[800] = {}; int v = 0; int used[800] = {}; res = scanf("%d", &n); for (int i = 0; i < n; i++) { res = scanf("%lld", x+i); res = scanf("%lld", y+i); res = scanf("%d", t+i); } for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (t[i] == t[j]) { long long diff_x = x[i]-x[j]; long long diff_y = y[i]-y[j]; long long dist = diff_x*diff_x+diff_y*diff_y; e[j][i] = dist; e[i][j] = dist; } else { long long a = x[i]*x[i]+y[i]*y[i]; long long b = x[j]*x[j]+y[j]*y[j]; long long s[2] = { -1LL, a+b }; while (s[1]-s[0] > 1LL) { long long nxt = (s[0]+s[1])/2LL; long long tmp = a+b-nxt; if (tmp*tmp > 4LL*a*b) { s[0] = nxt; } else { s[1] = nxt; } } e[j][i] = s[1]; e[i][j] = s[1]; } } } for (int i = 0; i < n; i++) { d[i] = 1000000000000000000LL; } d[0] = 0LL; v = 0; while (v >= 0) { long long min = 1000000000000000000LL; used[v] = 1; for (int i = 0; i < n; i++) { long long max = d[v]; if (max < e[v][i]) { max = e[v][i]; } if (d[i] > max) { d[i] = max; } } v = -1; for (int i = 0; i < n; i++) { if (used[i] <= 0 && d[i] < min) { v = i; min = d[i]; } } } printf("%lld\n", d[n-1]); return 0; }