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<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<complex>

using namespace std;

#define reps(i,f,n) for(int i=f;i<int(n);i++)
#define rep(i,n) reps(i,0,n)

#define X real()
#define Y imag()


typedef complex<double> Point;
typedef pair<int,double> pid;

class Line{
	public:
	double A,B,C;
	Line(double A,double B,double C):A(A),B(B),C(C){}
	Line(){}
};
class Circle : public Point{
	public:
	double R;
	Circle(Point pt, double R):Point(pt),R(R){}
	Circle(){}
};
double Dot(Point p1, Point p2){return p1.X*p2.X + p1.Y*p2.Y;}
double Cross(Point p1, Point p2){return p1.X*p2.Y - p1.Y*p2.X;}

bool CircleHit(Circle a, Circle b){return abs(a-b) <= a.R + b.R;}

Line CircleCommonLine(Circle a,Circle b){
	double C = pow(b.R, 2)-pow(a.R, 2)+norm(a)-norm(b);
	return Line(2*(b.X-a.X), 2*(b.Y-a.Y), C);
}

pair<Point,Point> Suisen(Line line, Point pt){
	Point v(line.A, line.B);  v/=Point(abs(v),0);
	Point toPoint;
	
	if(line.B!=0){
		Point slide = pt-Point(0, -line.C/line.B);
		toPoint = v*Point(Dot(v,slide),0);
	}else{
		toPoint = Point(pt.X + line.C/line.A, 0);
	}
	return pair<Point,Point>(pt-toPoint, toPoint);
}

pair<Point,Point> CircleCross(Circle a, Circle b){
	Line commonLine = CircleCommonLine(a,b);
	pair<Point,Point> s = Suisen(commonLine, a);
	
	double d = abs(s.second);
	if(d!=0){
		Point e = s.second*Point(0,1)/d;
		if(a.R<d)d=a.R;
		double dist = sqrt(a.R*a.R - d*d);
		return pair<Point,Point>(s.first+e*Point(dist,0), s.first+e*Point(-dist,0));
	}
	return pair<Point,Point>(s.first-Point(0,a.R), s.first+Point(0,a.R));
}

bool ccw(Point p1, Point p2, Point p3){
	double ret = Cross(p2-p1, p3-p1);
	//cout<<"ccw "<<(p2-p1)<<" "<<(p3-p1)<<" "<<ret<<endl;
	return ret>=0;
}

const int N = 222;
typedef pair<int,double> pid;

int n;
Circle cir[N];

vector<pid> edge[N];
pair<Point,Point> cross[N];

void init(){
	rep(i,N){
		edge[i].clear();
		cir[i] = Circle();
		cross[i] = pair<Point,Point>(Point(),Point());
	}
}

void setCross(){
	rep(i,n-1){
		cross[i] = CircleCross(cir[i],cir[i+1]);
	}
	rep(i,n-1){
		if(!ccw(cir[i], cross[i].first, cross[i].second)){
			swap(cross[i].first, cross[i].second);
		}
	}
	/*
	rep(i,n-1){
		cout<<"cross "<<cross[i].first<<" "<<cross[i].second<<endl;
	}*/
}

bool canAdd(Point pt, Point pt2, int from, int to){
	if(from<0)from=0;
	reps(i,from,to){
		if(!ccw(pt, pt2, cross[i].second))return false;
	}
	reps(i,from,to){
		if(ccw(pt, pt2, cross[i].first))return false;
	}
	return true;
}

void addEdge(Point pt,int num, int now){
	reps(i,now+1,n){
		if(i!=n-1){
			if(canAdd(pt, cross[i].second, now+1, i)){
				edge[num].push_back(pid(i*2, abs(cross[i].second-pt)));
			}
			if(canAdd(pt, cross[i].first, now+1, i)){
				edge[num].push_back(pid(i*2+1, abs(cross[i].first-pt)));
			}
		}else{
			if(canAdd(pt, cir[n-1], now+1, n-1)){
				edge[num].push_back(pid(n*2, abs(cir[n-1]-pt)));
			}
		}
	}
}

void makeEdge(){
	
	reps(i,-1,n-1){
		if(i==-1){
			addEdge(cir[0],n*2-1,i);
		}else{
			addEdge(cross[i].first,i*2+1, i);
			addEdge(cross[i].second,i*2, i);
		}
	}
}

class P{
	public:
	int now;
	double val;
	P(int now,double val):now(now),val(val){}
	bool operator<(const P& a)const{
		return val>a.val;
	}
};
double dijkstra(){
	
	/*
	rep(i,n*2+2){
		printf("i(%d) :: ",i);
		rep(j,edge[i].size()){
			printf("(%d,%lf)",edge[i][j].first, edge[i][j].second);
		}puts("");
	}*/
	
	priority_queue<P> que;
	que.push(P(n*2-1, 0));
	
	int visit[N]={0};
	while(1){
		if(que.empty())break;
		
		P u = que.top();
		que.pop();
		
		if(u.now==n*2)return u.val;
		if(visit[u.now]==1)continue;
		visit[u.now]=1;
		
		//printf("ijk %d %lf\n",u.now,u.val);
		rep(i,edge[u.now].size()){
			pid e = edge[u.now][i];
			que.push(P(e.first, u.val+e.second));
		}
	}
	return -1;
}

double solve(){
	init();
	
	rep(i,n){
		double a,b,c;
		cin>>a>>b>>c;
		cir[i] = Circle(Point(a,b),c);
	}
	
	setCross();
	makeEdge();
	
	return dijkstra();
}

int main(){
	
	while(1){
		cin>>n;
		if(n==0)break;
		printf("%.9lf\n",solve());
		//break;
	}
}