Problem: There is a chain consisting of multiple circles on a plane. The first (last) circle of the chain only intersects with the next (previous) circle, and each intermediate circle intersects only with the two neighboring circles.

Your task is to find the shortest path that satisfies the following conditions.

* The path connects the centers of the first circle and the last circle.
* The path is confined in the chain, that is, all the points on the path are located within or on at least one of the circles.

Figure E-1 shows an example of such a chain and the corresponding shortest path.

<image>

Figure E-1: An example chain and the corresponding shortest path


Input

The input consists of multiple datasets. Each dataset represents the shape of a chain in the following format.

> n
>  x1 y1 r1
>  x2 y2 r2
>  ...
>  xn yn rn
>

The first line of a dataset contains an integer n (3 ≤ n ≤ 100) representing the number of the circles. Each of the following n lines contains three integers separated by a single space. (xi, yi) and ri represent the center position and the radius of the i-th circle Ci. You can assume that 0 ≤ xi ≤ 1000, 0 ≤ yi ≤ 1000, and 1 ≤ ri ≤ 25.

You can assume that Ci and Ci+1 (1 ≤ i ≤ n−1) intersect at two separate points. When j ≥ i+2, Ci and Cj are apart and either of them does not contain the other. In addition, you can assume that any circle does not contain the center of any other circle.

The end of the input is indicated by a line containing a zero.

Figure E-1 corresponds to the first dataset of Sample Input below. Figure E-2 shows the shortest paths for the subsequent datasets of Sample Input.

<image>

Figure E-2: Example chains and the corresponding shortest paths


Output

For each dataset, output a single line containing the length of the shortest chain-confined path between the centers of the first circle and the last circle. The value should not have an error greater than 0.001. No extra characters should appear in the output.

Sample Input


10
802 0 10
814 0 4
820 1 4
826 1 4
832 3 5
838 5 5
845 7 3
849 10 3
853 14 4
857 18 3
3
0 0 5
8 0 5
8 8 5
3
0 0 5
7 3 6
16 0 5
9
0 3 5
8 0 8
19 2 8
23 14 6
23 21 6
23 28 6
19 40 8
8 42 8
0 39 5
11
0 0 5
8 0 5
18 8 10
8 16 5
0 16 5
0 24 5
3 32 5
10 32 5
17 28 8
27 25 3
30 18 5
0


Output for the Sample Input


58.953437
11.414214
16.0
61.874812
63.195179






Example

Input

10
802 0 10
814 0 4
820 1 4
826 1 4
832 3 5
838 5 5
845 7 3
849 10 3
853 14 4
857 18 3
3
0 0 5
8 0 5
8 8 5
3
0 0 5
7 3 6
16 0 5
9
0 3 5
8 0 8
19 2 8
23 14 6
23 21 6
23 28 6
19 40 8
8 42 8
0 39 5
11
0 0 5
8 0 5
18 8 10
8 16 5
0 16 5
0 24 5
3 32 5
10 32 5
17 28 8
27 25 3
30 18 5
0


Output

58.953437
11.414214
16.0
61.874812
63.195179

Solution (in CPP):

Solution:
#include <iostream>
#include <vector>
#include <array>
#include <complex>
#include <cmath>
#include <iomanip>

using Comp = std::complex<double>;

constexpr double Eps = 0.00000001;
constexpr double Inf = 1000000000;

double cross(const Comp& a, const Comp& b) {
	return a.real() * b.imag() - a.imag() * b.real();
}

bool intersects(const Comp& s1, const Comp& l1, const Comp& s2, const Comp& l2) {
	double c12 = cross(l1, l2);
	if (std::abs(c12) <= Eps)
		return false;

	const Comp l = s2 - s1;
	double t1 = cross(l, l1) / c12;
	double t2 = cross(l, l2) / c12;
	return 0 <= t1 && t1 <= 1 && 0 <= t2 && t2 <= 1;
}

struct Circle {
	Comp center;
	double r;

	std::array<Comp, 2> intersections(const Circle& c) const {
		double l = std::abs(c.center - center);
		Comp n_l = (c.center - center) / l;

		double theta = std::acos((r*r + l * l - c.r * c.r)/(2*r*l));

		return {
			center + std::polar(r, theta) * n_l,
			center + std::polar(r, -theta)* n_l
		};
	}
};

int main() {
	int n;
	while (std::cin >> n, n != 0) {
		std::vector<Circle> circles(n);
		for (auto&& c : circles) {
			int x, y;
			std::cin >> x >> y >> c.r;
			c.center = Comp(x, y);
		}

		std::vector<std::array<Comp, 2>> iss(n - 1);
		for (int i = 0; i < n - 1; ++i)
			iss[i] = circles[i].intersections(circles[i + 1]);

		auto isValid = [&](const Comp& a, const Comp b, int s, int t) {
			for (int i = s; i < t; ++i)
				if (!intersects(a, b - a, iss[i][0], iss[i][1] - iss[i][0]))
					return false;

			return true;
		};

		std::vector<std::array<double, 2>> dp(n - 1, {Inf, Inf});

		dp[0][0] = std::abs(iss[0][0] - circles[0].center);
		dp[0][1] = std::abs(iss[0][1] - circles[0].center);
		for (int i = 2; i / 2 < n - 1; ++i) {
			if (isValid(circles[0].center, iss[i / 2][i % 2], 0, i / 2))
				dp[i / 2][i % 2] = std::abs(iss[i / 2][i % 2] - circles[0].center);

			for (int j = 0; j / 2 < i / 2; ++j) {
				if (isValid(iss[j / 2][j % 2], iss[i / 2][i % 2], j / 2 + 1, i / 2)) {
					dp[i / 2][i % 2] = std::min(
						dp[i / 2][i % 2],
						dp[j / 2][j % 2] + std::abs(iss[i / 2][i % 2] - iss[j / 2][j % 2])
					);
				}
			}
		}

		double ans = Inf;
		if (isValid(circles[0].center, circles[n - 1].center, 0, n - 1))
			ans = std::abs(circles[n - 1].center - circles[0].center);

		for (int i = 0; i / 2 < n - 1; ++i) {
			if (isValid(iss[i / 2][i % 2], circles[n - 1].center, i / 2 + 1, n - 1)) {
				ans = std::min(
					ans,
					dp[i / 2][i % 2] + std::abs(circles[n - 1].center - iss[i / 2][i % 2])
				);
			}
		}

		std::cout << std::fixed << std::setprecision(6) << ans << std::endl;
	}
}