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"

using namespace std;

const int INF = numeric_limits<int>::max()/2;

typedef long double ld;
typedef complex<ld> Point;
typedef pair<ld,ld> P;

const ld eps = 1e-12, pi = acos(-1.0);

namespace std {
	bool operator<(const Point &lhs, const Point &rhs) {
		if (lhs.real() < rhs.real() - eps) return true;
		if (lhs.real() > rhs.real() + eps) return false;
		return lhs.imag() < rhs.imag();
	}
}

bool eq(ld a, ld b) {
	return (abs(a - b) < eps);
}

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

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

class Line {
public:
	Point a, b;
	Line() : a(Point(0, 0)), b(Point(0, 0)) {}
	Line(Point a, Point b) : a(a), b(b) {}
};

class Circle {
public:
	Point p;
	ld r;
	Circle() : p(Point(0, 0)), r(0) {}
	Circle(Point p, ld r) : p(p), r(r) {}
};

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

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;
}

vector<Point> is_cc(Circle c1, Circle c2) {
	vector<Point> res;
	ld d = abs(c1.p - c2.p);
	ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);
	ld dfr = c1.r * c1.r - rc * rc;
	if (abs(dfr) < eps) dfr = 0.0;
	else if (dfr < 0.0) return res; // no intersection
	ld 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;
}

struct Edge{
    int to;
    ld cost;
    Edge(int to,ld cost): to(to), cost(cost){}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

bool operator < (const Edge &e, const Edge &f){
    return e.cost > f.cost;
}

ld dijkstra(const Graph &g, int s){
    vector<ld> d(g.size(),INF);
    vector<int> p(g.size(),-1);
    d[s] = 0;
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(P(0,s)); // P(dis, pos)
    while(!que.empty()){
        ld dist = que.top().first;
        int v = que.top().second;
        que.pop();
        if(d[v]<dist) continue;
        for(const auto &e: g[v]){
            if(d[e.to]<=d[v]+e.cost) continue;
            d[e.to] = d[v] + e.cost;
            que.push(P(d[e.to], e.to));
            p[e.to]=v;
        }
    }
    int tmpp=g.size()-1;
    vector<int> path;
    while(tmpp!=-1){
        path.push_back(tmpp);
        tmpp=p[tmpp];
    }
    reverse(path.begin(),path.end());
    //for(int i=0;i<path.size();++i)cout<<path[i]<<" ";
    //cout<<endl;
    return d[g.size()-1];
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    while(cin>>n,n){
        vector<Circle> c;
        for(int i=0;i<n;i++){
            ld x,y,r;cin>>x>>y>>r;
            c.push_back(Circle(Point(x,y),r));
        }
        Point st=c[0].p, gt=c[n-1].p;
        vector<Point> ps;
        vector<Line> ls;
        ps.push_back(st);
        for(int i=1;i<n;i++){
            vector<Point> res=is_cc(c[i],c[i-1]);
            ps.push_back(res[0]);
            ps.push_back(res[1]);
            ls.push_back(Line(res[0],res[1]));
        }
        ps.push_back(gt);
        
        int sz=ps.size();
        vector<vector<ld>> dis(sz,vector<ld>(sz,INF));
        Graph g(sz);
        for(int i=0;i<sz;i++) dis[i][i]=0;

        // st->mid
        for(int i=1;i<sz-1;i++){
            int idx=(i+1)/2;
            ld d=abs(st-ps[i]);
            bool ftmp=true;
            for(int j=0;j<idx-1;j++){
                if(isis_ss(ls[j],Line(st,ps[i]))) continue;
                ftmp=false;
            }
            if(ftmp){
                dis[0][i]=dis[i][0]=d;
                g[0].push_back(Edge(i,d));
                g[i].push_back(Edge(0,d));
            }
        }
        // st->gt
        bool f=true;
        for(int i=0;i<(int)ls.size();i++){
            if(isis_ss(ls[i],Line(st,gt))) continue;
            f=false;
        }
        if(f){
            dis[0][sz-1]=dis[sz-1][0]=abs(st-gt);
            g[0].push_back(Edge(sz-1,abs(st-gt)));
            g[sz-1].push_back(Edge(0,abs(st-gt)));
        }

        // mid->mid
        for(int i=1;i<sz-1;i++){
            int idx=(i+1)/2;
            for(int j=i+1;j<sz-1;j++){
                int jdx=(j+1)/2;
                if(idx==jdx){
                    dis[i][j]=dis[j][i]=abs(ps[i]-ps[j]);
                    g[i].push_back(Edge(j,abs(ps[i]-ps[j])));
                    g[j].push_back(Edge(i,abs(ps[i]-ps[j])));
                    continue;
                }
                ld d=abs(ps[i]-ps[j]);
                bool ftmp=true;
                for(int k=idx;k<jdx-1;k++){
                    if(isis_ss(ls[k],Line(ps[i],ps[j]))) continue;
                    ftmp=false;
                }
                if(ftmp){
                    dis[i][j]=dis[j][i]=d;
                    g[i].push_back(Edge(j,d));
                    g[j].push_back(Edge(i,d));
                }
            }
        }
        // mid->gt
        for(int i=1;i<sz-1;i++){
            int idx=(i+1)/2;
            ld d=abs(ps[i]-gt);
            bool ftmp=true;
            for(int j=idx;j<(int)ls.size();j++){
                if(isis_ss(ls[j],Line(ps[i],gt))) continue;
                ftmp=false;
            }
            if(ftmp){
                dis[i][sz-1]=dis[sz-1][i]=d;
                g[i].push_back(Edge(sz-1,d));
                g[sz-1].push_back(Edge(i,d));
            }
        }

        //cout<<fixed<<setprecision(10)<<dis[1][10]<<endl;
        for(int k=0;k<sz;k++){
            for(int i=0;i<sz;i++){
                for(int j=0;j<sz;j++){
                    dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
                }
            }
        }
        //cout<<fixed<<setprecision(10)<<dis[0][sz-1]<<endl;
        cout<<fixed<<setprecision(10)<<dijkstra(g,0)<<endl;
    }
}