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<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; i++ )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;
int dx[4]={1,0,-1,0} , dy[4]={0,1,0,-1} ;

const double eps=1e-6;
double add(double a,double b){
    if(abs(a+b)<eps)return 0;
    return a+b;
}

typedef pair<double,double> P;
P operator+(const P& x,const P& y){
    return {add(x.first,y.first),add(x.second,y.second)};
}

P operator-(const P& x,const P& y){
    return {add(x.first,-y.first),add(x.second,-y.second)};

}

P operator*(const P& x,const double& y){
    return {x.first*y,x.second*y};
}
double dot(P x,P y){
    return add(x.first*y.first,x.second*y.second);


}

double det(P x,P y){
    return add(x.first*y.second,-x.second*y.first);
}

bool onseg(P p1,P p2,P q){
    return det(p1-q,p2-q)==0 &&dot(p1-q,p2-q)<=0; 
}

P intersection(P p1,P p2,P q1,P q2){
    return p1+(p2-p1)*(det(q2-q1,q1-p1)/det(q2-q1,p2-p1));
}

bool cross(P p1,P p2,P q1,P q2){
    if(det(p1-p2,q1-q2)==0)return onseg(p1,p2,q1)||onseg(p1,p2,q2)||onseg(q1,q2,p1)||onseg(q1,q2,p2);
    P ret=intersection(p1,p2,q1,q2);
    return onseg(p1,p2,ret)&&onseg(q1,q2,ret);
}

double dist(P p1,P p2,P q){
    if(dot(q-p1,p2-p1)<=0)return sqrt(dot(q-p1,q-p1));
    if(dot(q-p2,p1-p2)<=0)return sqrt(dot(q-p2,q-p2));
    P d=p1-p2;
    P ret=q+P{d.second,-d.first};
    P h=intersection(p1, p2, q, ret);
    return sqrt(dot(q-h,q-h));
}
double d(P p,P q){
    return sqrt(dot(p-q,p-q));
}

int main(){
    int n;
    vector<int>ans;
    while(cin>>n,n!=0){
        P a[n];
        double r[n];
        rep(i,n)cin>>a[i].first>>a[i].second>>r[i];
        P b[n-1][2];
        rep(i,n-1){
            P v=(a[i+1]-a[i])*(1.0/d(a[i+1],a[i]));
            P h={v.second,-v.first};
            double l=(r[i]*r[i]+d(a[i],a[i+1])*d(a[i],a[i+1])-r[i+1]*r[i+1])/2/d(a[i],a[i+1]);
            b[i][0]=a[i]+v*l+h*sqrt(r[i]*r[i]-l*l);
            b[i][1]=a[i]+v*l-h*sqrt(r[i]*r[i]-l*l);
        }
        double x[2*n][2*n];
        rep(i,2*n)rep(j,2*n)x[i][j]=inf;
        rep(i,n-1)rep(j,2){
            bool ok=true;
            rep(k,i)if(!cross(a[0],b[i][j],b[k][0],b[k][1]))ok=false;
            if(ok)x[0][2*i+j+1]=d(a[0],b[i][j]);
        }
        rep(i,n-1)rep(j,2){
            bool ok=true;
            REP(k,i+1,n-1)if(!cross(a[n-1],b[i][j],b[k][0],b[k][1]))ok=false;
            if(ok)x[2*n-1][2*i+j+1]=d(a[n-1],b[i][j]);
        }
        rep(i,n-1)REP(j,i+1,n-1)rep(k,2)rep(l,2){
            bool ok=true;
            REP(m,i+1,j)if(!cross(b[i][k],b[j][l],b[m][0],b[m][1]))ok=false;
            if(ok)x[2*i+k+1][2*j+l+1]=d(b[i][k],b[j][l]);
        }
        bool ok=true;
        rep(i,n-1)if(!cross(a[0],a[n-1],b[i][0],b[i][1]))ok=false;
        if(ok)x[0][2*n-1]=d(a[0],a[n-1]);
        rep(i,2*n)x[i][i]=0;
        rep(i,n)x[2*i+1][2*i+2]=d(b[i][0],b[i][1]);
        rep(i,2*n)rep(j,2*n)x[i][j]=min(x[i][j],x[j][i]);
        rep(i,2*n)rep(j,2*n)rep(k,2*n)x[j][k]=min(x[i][j]+x[i][k],x[j][k]);
        cout<<setprecision(10)<<x[0][2*n-1]<<endl;
    }
    return 0;
}