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:
//
// 円と円の交点
//
// verified:
//   AOJ 1183 鎖中経路
//     http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1183&lang=jp
//

#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
#include <algorithm>
using namespace std;



// debug stream of pair, vector 
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }



////////////////////////////
// 基本要素 (点, 線分, 円)
////////////////////////////

using DD = double;
const DD INF = 1LL << 60;      // to be set appropriately
const DD EPS = 1e-6;        // to be set appropriately
const DD PI = acos(-1.0);
DD torad(int deg) { return (DD)(deg)* PI / 180; }
DD todeg(DD ang) { return ang * 180 / PI; }

/* Point */
struct Point {
	DD x, y;
	Point(DD x = 0.0, DD y = 0.0) : x(x), y(y) {}
	friend ostream& operator << (ostream &s, const Point &p) { return s << '(' << p.x << ", " << p.y << ')'; }
};
inline Point operator + (const Point &p, const Point &q) { return Point(p.x + q.x, p.y + q.y); }
inline Point operator - (const Point &p, const Point &q) { return Point(p.x - q.x, p.y - q.y); }
inline Point operator * (const Point &p, DD a) { return Point(p.x * a, p.y * a); }
inline Point operator * (DD a, const Point &p) { return Point(a * p.x, a * p.y); }
inline Point operator * (const Point &p, const Point &q) { return Point(p.x * q.x - p.y * q.y, p.x * q.y + p.y * q.x); }
inline Point operator / (const Point &p, DD a) { return Point(p.x / a, p.y / a); }
inline Point conj(const Point &p) { return Point(p.x, -p.y); }
inline Point rot(const Point &p, DD ang) { return Point(cos(ang) * p.x - sin(ang) * p.y, sin(ang) * p.x + cos(ang) * p.y); }
inline Point rot90(const Point &p) { return Point(-p.y, p.x); }
inline DD cross(const Point &p, const Point &q) { return p.x * q.y - p.y * q.x; }
inline DD dot(const Point &p, const Point &q) { return p.x * q.x + p.y * q.y; }
inline DD norm(const Point &p) { return dot(p, p); }
inline DD abs(const Point &p) { return sqrt(dot(p, p)); }
inline DD amp(const Point &p) { DD res = atan2(p.y, p.x); if (res < 0) res += PI * 2; return res; }
inline bool eq(const Point &p, const Point &q) { return abs(p - q) < EPS; }
inline bool operator < (const Point &p, const Point &q) { return (abs(p.x - q.x) > EPS ? p.x < q.x : p.y < q.y); }
inline bool operator >(const Point &p, const Point &q) { return (abs(p.x - q.x) > EPS ? p.x > q.x : p.y > q.y); }
inline Point operator / (const Point &p, const Point &q) { return p * conj(q) / norm(q); }

/* Line */
struct Line : vector<Point> {
	Line(Point a = Point(0.0, 0.0), Point b = Point(0.0, 0.0)) {
		this->push_back(a);
		this->push_back(b);
	}
	friend ostream& operator << (ostream &s, const Line &l) { return s << '{' << l[0] << ", " << l[1] << '}'; }
};

/* Circle */
struct Circle : Point {
	DD r;
	Circle(Point p = Point(0.0, 0.0), DD r = 0.0) : Point(p), r(r) {}
	friend ostream& operator << (ostream &s, const Circle &c) { return s << '(' << c.x << ", " << c.y << ", " << c.r << ')'; }
};



////////////////////////////
// 円や直線の交差判定, 距離
////////////////////////////

/*
 ccw を用いている
 
 P: point
 L: Line
 S: Segment
 
 distancePL は、「点」と「直線」の距離
 distancePS は、「点」と「線分」の距離
 */

int ccw_for_dis(const Point &a, const Point &b, const Point &c) {
    if (cross(b-a, c-a) > EPS) return 1;
    if (cross(b-a, c-a) < -EPS) return -1;
    if (sqrt(dot(b-a, c-a)) < -EPS) return 2;
    if (abs(b-a) < abs(c-a) - EPS) return -2;
    return 0;
}
Point proj(const Point &p, const Line &l) {
    DD t = dot(p - l[0], l[1] - l[0]) / norm(l[1] - l[0]);
    return l[0] + (l[1] - l[0]) * t;
}
Point refl(const Point &p, const Line &l) {
    return p + (proj(p, l) - p) * 2;
}
bool isinterPL(const Point &p, const Line &l) {
    return (abs(p - proj(p, l)) < EPS);
}
bool isinterPS(const Point &p, const Line &s) {
    return (ccw_for_dis(s[0], s[1], p) == 0);
}
bool isinterLL(const Line &l, const Line &m) {
    return (abs(cross(l[1] - l[0], m[1] - m[0])) > EPS ||
            abs(cross(l[1] - l[0], m[0] - l[0])) < EPS);
}
bool isinterSS(const Line &s, const Line &t) {
    if (eq(s[0], s[1])) return isinterPS(s[0], t);
    if (eq(t[0], t[1])) return isinterPS(t[0], s);
    return (ccw_for_dis(s[0], s[1], t[0]) * ccw_for_dis(s[0], s[1], t[1]) <= 0 &&
            ccw_for_dis(t[0], t[1], s[0]) * ccw_for_dis(t[0], t[1], s[1]) <= 0);
}
DD distancePL(const Point &p, const Line &l) {
    return abs(p - proj(p, l));
}
DD distancePS(const Point &p, const Line &s) {
    Point h = proj(p, s);
    if (isinterPS(h, s)) return abs(p - h);
    return min(abs(p - s[0]), abs(p - s[1]));
}
DD distanceLL(const Line &l, const Line &m) {
    if (isinterLL(l, m)) return 0;
    else return distancePL(m[0], l);
}
DD distanceSS(const Line &s, const Line &t) {
    if (isinterSS(s, t)) return 0;
    else return min(min(distancePS(s[0], t), distancePS(s[1], t)), min(distancePS(t[0], s), distancePS(t[1], s)));
}



////////////////////////////
// 円や直線の交点
////////////////////////////

Point proj_for_crosspoint(const Point &p, const Line &l) {
	DD t = dot(p - l[0], l[1] - l[0]) / norm(l[1] - l[0]);
	return l[0] + (l[1] - l[0]) * t;
}
vector<Point> crosspoint(const Line &l, const Line &m) {
	vector<Point> res;
	DD d = cross(m[1] - m[0], l[1] - l[0]);
	if (abs(d) < EPS) return vector<Point>();
	res.push_back(l[0] + (l[1] - l[0]) * cross(m[1] - m[0], m[1] - l[0]) / d);
	return res;
}
vector<Point> crosspoint(const Circle &e, const Circle &f) {
	vector<Point> res;
	DD d = abs(e - f);
	if (d < EPS) return vector<Point>();
	if (d > e.r + f.r + EPS) return vector<Point>();
	if (d < abs(e.r - f.r) - EPS) return vector<Point>();
	DD rcos = (d * d + e.r * e.r - f.r * f.r) / (2.0 * d), rsin;
	if (e.r - abs(rcos) < EPS) rsin = 0;
	else rsin = sqrt(e.r * e.r - rcos * rcos);
	Point dir = (f - e) / d;
	Point p1 = e + dir * Point(rcos, rsin);
	Point p2 = e + dir * Point(rcos, -rsin);
	res.push_back(p1);
	if (!eq(p1, p2)) res.push_back(p2);
	return res;
}
vector<Point> crosspoint(const Circle &e, const Line &l) {
	vector<Point> res;
	Point p = proj_for_crosspoint(e, l);
	DD rcos = abs(e - p), rsin;
	if (rcos > e.r + EPS) return vector<Point>();
	else if (e.r - rcos < EPS) rsin = 0;
	else rsin = sqrt(e.r * e.r - rcos * rcos);
	Point dir = (l[1] - l[0]) / abs(l[1] - l[0]);
	Point p1 = p + dir * rsin;
	Point p2 = p - dir * rsin;
	res.push_back(p1);
	if (!eq(p1, p2)) res.push_back(p2);
	return res;
}


// 円と線分の交点
int ccw_for_crosspoint_cs(const Point &a, const Point &b, const Point &c) {
    if (cross(b-a, c-a) > EPS) return 1;
    if (cross(b-a, c-a) < -EPS) return -1;
    if (dot(b-a, c-a) < -EPS) return 2;
    if (norm(b-a) < norm(c-a) - EPS) return -2;
    return 0;
}
bool isinterPS_crosspoint_cs(const Point &p, const Line &s) {
    return (ccw_for_crosspoint_cs(s[0], s[1], p) == 0);
}
vector<Point> crosspoint_CS(const Circle &e, const Line &s) {
    vector<Point> res;
    Point p = proj_for_crosspoint(e, s);
    DD rcos = abs(e - p), rsin;
    if (rcos > e.r + EPS) return vector<Point>();
    else if (e.r - rcos < EPS) rsin = 0;
    else rsin = sqrt(e.r * e.r - rcos * rcos);
    Point dir = (s[1] - s[0]) / abs(s[1] - s[0]);
    Point p1 = p + dir * rsin;
    Point p2 = p - dir * rsin;
    if (isinterPS_crosspoint_cs(p1, s)) res.push_back(p1);
    if (isinterPS_crosspoint_cs(p2, s) && !eq(p1, p2)) res.push_back(p2);
    return res;
}



////////////////////////////
// 円や直線の交点
////////////////////////////

bool isin(const vector<Circle> &cs, const Line &l) {
    vector<Point> vps;
    for (auto c : cs) {
        auto cps = crosspoint_CS(c, l);
        for (auto p : cps) vps.push_back(p);
    }
    sort(vps.begin(), vps.end());
    for (int i = 0; i+1 < vps.size(); ++i) {
        Point p = (vps[i] + vps[i+1]) / 2;
        bool exist = false;
        for (auto c : cs) {
            if (abs(c - p) < c.r + EPS) exist = true;
        }
        if (!exist) return false;
    }
    return true;
}


int main() {
	int N;
    while (cin >> N, N) {
        vector<Circle> cs(N);
        for (int i = 0; i < N; ++i) cin >> cs[i].x >> cs[i].y >> cs[i].r;

        vector<Point> ips;
        ips.push_back(Point(cs[0].x, cs[0].y));
        for (int i = 0; i+1 < N; ++i) {
            auto cp = crosspoint(cs[i], cs[i+1]);
            for (auto p : cp) ips.push_back(p);
        }
        ips.push_back(Point(cs[N-1].x, cs[N-1].y));

        vector<DD> dp((int)ips.size() + 1, INF);
        dp[0] = 0;
        for (int i = 0; i < ips.size(); ++i) {
            for (int j = i+1; j < ips.size(); ++j) {
                Line l(ips[i], ips[j]);
                if (isin(cs, l)) dp[j] = min(dp[j], dp[i] + abs(l[0] - l[1]));
            }
            //cout << i << ": " << dp[i] << endl;
        }
        cout << fixed << setprecision(10) << dp[(int)ips.size()-1] << endl;
    }
}