using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var n = NN; var map = NArr(n); var dp = new long[n][]; for (var i = 0; i < n; ++i) dp[i] = new long[n]; var tree = new List<(int to, long len)>[n]; for (var i = 0; i < tree.Length; ++i) tree[i] = new List<(int to, long len)>(); for (var i = 0; i < n; ++i) for (var j = i + 1; j < n; ++j) { if (map[i][2] == map[j][2]) { var len = (long)(map[i][0] - map[j][0]) * (map[i][0] - map[j][0]) + (long)(map[i][1] - map[j][1]) * (map[i][1] - map[j][1]); dp[i][j] = len; dp[j][i] = len; } else { var a = (long)map[i][0] * map[i][0] + (long)map[i][1] * map[i][1]; var b = (long)map[j][0] * map[j][0] + (long)map[j][1] * map[j][1]; var len = a + b - SqrtFloor(4 * a * b); dp[i][j] = len; dp[j][i] = len; } } var ok = 3200000001L; var ng = -1L; while (ok - ng > 1) { var mid = (ok + ng) / 2; var q = new Queue(); q.Enqueue(0); var visited = new bool[n]; visited[0] = true; while (q.Count > 0) { var cur = q.Dequeue(); for (var i = 0; i < n; ++i) { if (visited[i]) continue; if (dp[cur][i] > mid) continue; visited[i] = true; q.Enqueue(i); } } if (visited[n - 1]) ok = mid; else ng = mid; } WriteLine(ok); } static long SqrtFloor(long a) { var sq = Math.Sqrt(a); for (var i = Math.Max(0, (long)sq - 10); i < sq + 10; ++i) { if (i * i <= a && a < (i + 1) * (i + 1)) return i; } return 0; } }