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:
#define _USE_MATH_DEFINES
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<string>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstring>
#include<stack>
using namespace std;
#define EPS 1e-8
#define INF 1000000
 
struct Point{
    double x,y;
    Point(){}
    Point(double _x,double _y){
        x=_x; y=_y;
    }
    Point operator +(const Point p)const{
        return Point(x+p.x,y+p.y);
    }
    Point operator -(const Point p)const{
        return Point(x-p.x,y-p.y);
    }
    Point operator *(const double d)const{
        return Point(x*d,y*d);
    }
    bool operator <(const Point &p)const{
        if(x==p.x) return y<p.y;
        return x<p.x;
    }
    double norm(){
        return x*x+y*y;
    }
	bool input(){
		if(cin>>x>>y) return true;
		return false;
	}
};
 
struct Line{
    Point a,b;
    Line(){}
    Line(Point _a,Point _b){
        a=_a; b=_b;
    }
	bool input(){
		if(a.input() && b.input()) return true;
		return false;
	}
};
 
struct Circle{
    Point c;
    double r;
    Circle(){}
    Circle(Point _c,double _r){
        c=_c; r=_r;
    }
};

typedef vector<Point> Polygon;

double dot(Point p,Point q){
    return p.x*q.x+p.y*q.y;
}
 
double cross(Point p,Point q){
    return p.x*q.y-q.x*p.y;
}
 
int ccw(Point a,Point b,Point c){ 
	Point v1 = b-a;
	Point v2 = c-a;
    if(cross(v1,v2)>EPS) return +1; 
    if(cross(v1,v2)<-EPS) return -1; 
	if(dot(v1,v2)<-EPS) return +2; 
	if(v1.norm()<v2.norm()) return -2; 
    return 0; 
}
 
bool intersect_ss(Line l,Line m){
	return ccw(l.a,l.b,m.a)*ccw(l.a,l.b,m.b)<=0 && ccw(m.a,m.b,l.a)*ccw(m.a,m.b,l.b)<=0;
}
 
bool crosspoint_ll(Line l,Line m,Point &p){
    Point a1,a2,b1,b2;
    a1 = l.b-l.a; a2 = m.b-m.a;
    b1 = m.a-l.a; b2 = l.a-m.b;
    double s1,s2;
    s1 = cross(a1,b1)/2; s2 = cross(a1,b2)/2;
    if(s1+s2<EPS) return false; 
    p = Point(m.a.x+a2.x*s1/(s1+s2),m.a.y+a2.y*s1/(s1+s2));
    return true;
}
 
int crosspoint_ss(Line l,Line m,Point &p){
    if(intersect_ss(l,m)==false) return 0; 
    if(crosspoint_ll(l,m,p)==true) return 1;
	return -1; 
}
 
int crosspoint_cc(Circle c1,Circle c2,Point &p1,Point &p2){
    double d,a,t;
    d = sqrt((c2.c-c1.c).norm());
	if(abs(c1.c.x-c2.c.x)<EPS && abs(c1.c.y-c2.c.y)<EPS && abs(c1.r-c2.r)<EPS)
		return -1; 
    if(d<abs(c1.r-c2.r) || c1.r+c2.r<d) return 0; 
    a = acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));
    t = atan2(c2.c.y-c1.c.y,c2.c.x-c1.c.x);
    p1 = Point(c1.c.x+c1.r*cos(t+a),c1.c.y+c1.r*sin(t+a));
    p2 = Point(c1.c.x+c1.r*cos(t-a),c1.c.y+c1.r*sin(t-a));
    if(abs(p1.x-p2.x)<EPS && abs(p1.y-p2.y)<EPS) return 1; 
    return 2; 
}

int contains(Polygon g,Point p){
	Line l = Line(p,Point(INF,p.y));
	int cnt = 0, n = g.size();
	for(int i=0;i<n;i++){
		Point a = g[i]-p;
		Point b = g[(i+1)%n]-p;
		if(ccw(g[i],g[(i+1)%n],p)==0) return 1; 
		if(a.y>b.y) swap(a,b);
		if(a.y<=EPS && EPS<b.y && cross(a,b)>EPS) cnt++;
	}
	if((cnt&1)==1) return 2; 
	return 0; 
}

Polygon andrewScan(Polygon s){
	if(s.size()<=2) return s;
	sort(s.begin(),s.end());
	Polygon g;
	for(int i=0;i<s.size();i++){
		for(int n=g.size(); n>=2 && ccw(g[n-2],g[n-1],s[i])!=-1; n--){
			g.pop_back();
		}
		g.push_back(s[i]);
	}
	int upper_n = g.size();
	for(int i=s.size()-2;i>=0;i--){
		for(int n=g.size(); n>upper_n && ccw(g[n-2],g[n-1],s[i])!=-1; n--){
			g.pop_back();
		}
		g.push_back(s[i]);
	}
	reverse(g.begin(),g.end());
	g.pop_back();
	return g;
}

int main(){
	int N;
	Circle C[100];
	while(cin>>N,N){
		for(int i=0;i<N;i++){
			cin>>C[i].c.x>>C[i].c.y>>C[i].r;
		}
		Polygon way[2];
		way[0].push_back(C[0].c); way[1].push_back(C[1].c);
		for(int i=0;i<N-1;i++){
			Point p1,p2;
			crosspoint_cc(C[i],C[i+1],p1,p2);
			way[0].push_back(p1); way[1].push_back(p2);
		}
		way[0].push_back(C[N-1].c); way[1].push_back(C[N-1].c);
		double dp[101][2];
		fill_n((double*)dp,101*2,INF); dp[0][0] = 0;
		for(int i=0;i<N;i++){
			for(int j=0;j<2;j++){
				for(int k=i+1;k<=N;k++){
					for(int l=0;l<2;l++){
						Line l1(way[j][i],way[l][k]);
						bool flag = true;
						for(int m=i+1;m<k;m++){
							if(intersect_ss(l1,Line(way[0][m],way[1][m]))==false)
								flag = false;
						}
						if(flag==true){
							double d = sqrt((way[l][k]-way[j][i]).norm());
							dp[k][l] = min(dp[k][l],dp[i][j]+d);
						}
					}
				}
			}
		}
		printf("%.4lf\n",dp[N][0]);
	}
}