import sys # sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = (1 << 63)-1 # inf = (1 << 31)-1 md = 10**9+7 # md = 998244353 def binary_search(l, r, minimize): if minimize: l -= 1 else: r += 1 while l+1 < r: m = (l+r)//2 if ok(m) ^ minimize: l = m else: r = m if minimize: return r return l def ok(m): def move(i, j): x1, y1, t1 = xyt[i] x2, y2, t2 = xyt[j] if t1 == t2: if (x2-x1)**2+(y2-y1)**2 > m: return False else: R = x1**2+y1**2 r = x2**2+y2**2 if R+r-m < 0:return True if (R+r-m)**2 > 4*R*r:return False return True st = [0] vis = [0]*n vis[0] = 1 while st: i = st.pop() for j in range(n): if vis[j]: continue if not move(i, j): continue if j == n-1: return True vis[j] = 1 st.append(j) return False n = II() xyt = LLI(n) ans = binary_search(0, 3200000005, True) print(ans)