import sys import math import heapq as hq input = sys.stdin.readline INF = 4611686018427387903 def isqrt(n): rn = math.sqrt(n) ok = max(0, int(rn - 2)) ng = int(rn + 2) while(abs(ok - ng) > 1): mid = (ok + ng) // 2 if(mid ** 2 <= n): ok = mid else: ng = mid return ok ''' Main Code ''' n = int(input()) planets = [list(map(int, input().split())) for _ in [0] * n] graph = [[] for _ in [0] * n] for i in range(n - 1): for j in range(i + 1, n): x1, y1, t1 = planets[i] x2, y2, t2 = planets[j] d = (x1 - x2) ** 2 + (y1 - y2) ** 2 if(t1 != t2): r1 = x1 ** 2 + y1 ** 2 r2 = x2 ** 2 + y2 ** 2 d = r1 + r2 - isqrt(4 * r1 * r2) graph[i].append((j, d)) graph[j].append((i, d)) dp = [INF] * n que = [(0, 0)] while(que): c, v = hq.heappop(que) if(dp[v] <= c): continue dp[v] = c if(v == n - 1): break for nv, nd in graph[v]: hq.heappush(que, (max(c, nd), nv)) ans = dp[n - 1] print(ans)