from math import ceil from heapq import * N = int(input()) RXYT = [] for i in range(N): x, y, t = map(int, input().split()) r = (x ** 2 + y ** 2) ** 0.5 RXYT.append((r, x, y, t)) E = [[] for _ in range(N)] for i in range(N - 1): r1, x1, y1, t1 = RXYT[i] for j in range(i + 1, N): r2, x2, y2, t2 = RXYT[j] if t1 == t2: tmp = (x1 - x2) ** 2 + (y1 - y2) ** 2 E[i].append((j, tmp)) E[j].append((i, tmp)) else: tmp = (r1 - r2) ** 2 E[i].append((j, tmp)) E[j].append((i, tmp)) D = [float('inf')] * N D[0] = 0 q = [(0, 0)] while len(q) > 0: d, u = heappop(q) for i in E[u]: a, b = i #if d > D[u]: continue tmp = max(D[u], b) if D[a] > tmp: D[a] = tmp heappush(q, (D[a], a)) #print(D) print(ceil(D[N - 1]))