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 <bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
const ll INF = 114514810;
const ll MOD = 1000000007;
const double EPS = 1e-12;
struct edge
{
	int to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
//int dx[] = { -1, 0, 0, 1 }; int dy[] = { 0, -1, 1, 0 };
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
bool valid(int x, int y, int h, int w) { return (x >= 0 && y >= 0 && x < h&&y < w); }
//int index(int x, int y, int w) { return w*x + y; }
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///


const double PI = acos(-1.0);
bool eq(double a, double b) { return fabs(a - b) < EPS; }
typedef complex<double> Point;
typedef vector<Point> Polygon;

namespace std
{
	bool operator < (const Point& a, const Point& b)
	{
		return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
	}
}

struct Line
{
	Point a, b;
	Line(Point p, Point q) :a(p), b(q) {};
};

struct Circle
{
	Point p; double r;
	Circle(Point a, double b) :p(a), r(b) {};
};

double dot(Point a, Point b)
{
	return real(conj(a)*b);
}

double cross(Point a, Point b)
{
	return imag(conj(a)*b);
}

int ccw(Point a, Point b, Point c)
{
	b -= a; c -= a;
	if (cross(b, c) > EPS) return 1;	//counter cloclwise
	if (cross(b, c) < -EPS) return -1;  //cloclwise
	if (dot(b, c) < 0) return 2;		//c--a--b on line 
	if (norm(b) < norm(c)) return -2;   //a--b--c on line
	return 0;							//a--c--b on line
}

bool isis_ll(Line l, Line m)
{
	return abs(cross(l.b - l.a, m.b - m.a)) > EPS;
}

bool isis_ls(Line l, Line s)
{
	return cross(l.b - l.a, s.a - l.a)*cross(l.b - l.a, s.b - s.a) < EPS;
}

bool isis_ss(Line s, Line t)
{
	return (ccw(s.a, s.b, t.a)*ccw(s.a, s.b, t.b) <= 0 &&
		ccw(t.a, t.b, s.a)*ccw(t.a, t.b, s.b) <= 0);
}

bool isis_lp(Line l, Point p)
{
	return (abs(cross(l.b - p, l.a - p)) < EPS);
}

bool isis_sp(Line s, Point p)
{
	return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a)) < EPS;
}

Point projection(Line l, Point p)
{
	Point base = l.b - l.a;
	double r = dot(p - l.a, base) / norm(base);
	return l.a + base*r;
}

Point mirror(Line l, Point p)
{
	return 2.0*projection(l, p) - p;
}

double dist_lp(Line l, Point p)
{
	return abs(p - projection(l, p));
}

double dist_ll(Line l, Line m)
{
	return isis_ll(l, m) ? 0 : dist_lp(l, m.a);
}

double dist_ls(Line l, Line s)
{
	if (isis_ls(l, s)) return 0;
	return min(dist_lp(l, s.a), dist_lp(l, s.b));
}

double dist_sp(Line s, Point p)
{
	Point r = projection(s, p);
	if (isis_sp(s, r)) return abs(r - p);
	return min(abs(s.a - p), abs(s.b - p));
}

double dist_ss(Line s, Line t)
{
	if (isis_ss(s, t)) return 0;
	return min(min(dist_sp(s, t.a), dist_sp(s, t.b)), min(dist_sp(t, s.a), dist_sp(t, s.b)));
}

Point is_ll(Line s, Line t)
{
	double a = cross(s.b - s.a, t.b - t.a);
	double b = cross(s.b - s.a, s.b - t.a);
	return t.a + b / a*(t.b - t.a);
}


bool isis_cl(Circle c, Line l)
{
	double d = dist_lp(l, c.p);
	return d - c.r < -EPS;
}



vector<Point> is_cc(Circle c1, Circle c2)
{
	vector<Point> res;
	double d = abs(c1.p - c2.p);
	double rc = (d*d + pow(c1.r, 2) - pow(c2.r, 2)) / (2 * d);
	double dfr = pow(c1.r, 2) - rc*rc;
	if (abs(dfr) < EPS) dfr = 0;
	if (dfr < 0.0) return res;
	double rs = sqrt(dfr);
	Point diff = (c2.p - c1.p) / d;
	res.push_back(c1.p + diff*Point(rc, rs));
	if (dfr != 0.0) res.push_back(c1.p + diff*Point(rc, -rs));
	return res;
}


int main()
{
	int n;;
	while (cin >> n, n)
	{
		vector<Circle> cs;
		REP(i, n)
		{
			double x, y, r;
			cin >> x >> y >> r;
			cs.push_back(Circle(Point(x, y), r));
		}
		vector<vector<Point>> ps(n + 1);
		ps[0].push_back(cs[0].p);
		ps[n].push_back(cs.back().p);
		FOR(i,1, n)
		{
			ps[i] = is_cc(cs[i - 1], cs[i]);
		}
		double wf[250][250];
		REP(i, n * 2)REP(j, n * 2) wf[i][j] = (i == j ? 0 : INF);
		REP(i, ps.size())FOR(j, i + 1, ps.size())REP(k, ps[i].size())REP(l, ps[j].size())
		{
			Line tmp(ps[i][k], ps[j][l]);
			bool f = true;
			FOR(m, i + 1, j)
			{
				Line tmpl(ps[m][0], ps[m][1]);
				if (!isis_ss(tmp, tmpl)) f = false;
			}
			int L = i * 2 - k;
			int R = j * 2 - l;
			if (i == 0) L = 0;
			if (j == ps.size() - 1) R = 2 * n - 1;

			if (f) wf[L][R] = wf[R][L] = abs(ps[i][k] - ps[j][l]);
		}
		REP(k, 2 * n)REP(i, 2 * n)REP(j, 2 * n)
			chmin(wf[i][j], wf[i][k] + wf[k][j]);
		cout << D10 << wf[0][2 * n - 1] << endl;
	}
	return 0;
}