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;
/*{{{*/  //template
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<29
#define LINF LLONG_MAX/3
#define mp make_pair
#define pb push_back
#define EB emplace_back
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define sz(x) (int)(x).size()
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
constexpr double eps = 1e-14; 
//constexpr double inf = numeric_limits<double>::max() / 10; 
constexpr double inf = 30000;
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/

namespace Geometry{
    struct Point{
        double x,y;
        Point(){}
        Point(double a,double b) : x(a),y(b){}
        Point(const Point& p) : x(p.x),y(p.y){}
        Point operator + (Point p){
            return Point(x+p.x,y+p.y);
        }
        Point operator - (Point p){
            return Point(x-p.x,y-p.y);
        }
        Point operator * (double k){
            return Point(x*k,y*k);
        }
    };
    using Vector = Point;
    struct Circle{
        Point c;
        double r;
        Circle(){}
        Circle(Point p,double c) : c(p),r(c){}
        Circle(double a,double b,double c) : c(Point(a,b)) , r(c){}
    };

    // for debug
    ostream& operator<<(ostream& os,const Point& p){
        os << "{" << p.x <<"," << p.y <<"}";
        return os;
    }
    ostream& operator<<(ostream& os,const Circle& c){
        os << "{(" << c.c.x << "," << c.c.y <<")," << c.r << "}";
        return os;
    }

    // ?????????????????£??????
    Vector normalize(Vector v){
        double t = sqrt(v.x*v.x+v.y*v.y);
        return Vector(v.x/t,v.y/t);
    }

    // ?????¨???????????¢
    inline double distance(Point a,Point b){
        double x=a.x-b.x;
        double y=a.y-b.y;
        return sqrt(x*x+y*y);
    }

    // ????????????
    pair<Point,Point> getCrossPoint(Circle c1,Circle c2){
        double d = distance(c1.c,c2.c);
        Vector v = normalize(c2.c - c1.c);  // ??£????????????????????????????????????
        Vector dualv = Vector(-v.y,v.x);    // v??¨?????´???????????????
        double Cos = (c1.r*c1.r+d*d-c2.r*c2.r) / (2*c1.r*d);
        double Sin = sqrt(1-Cos*Cos);
        Point a = c1.c + v*Cos*c1.r + dualv*Sin*c1.r;
        Point b = c1.c + v*Cos*c1.r - dualv*Sin*c1.r;
        return make_pair(a,b);
    }
};

using namespace Geometry;

int N;
Circle cir[128];
double x[128],y[128],r[128];
vector<pair<Point,Point>> cross;
vector<Point> poi;
int NN;

void solve(){
    cross.assign(0,pair<Point,Point>());
    poi.assign(0,Point());
    rep(i,N){
        cin >> x[i] >> y[i] >> r[i];
        cir[i] = Circle(x[i],y[i],r[i]);
    }
    for(int i=0;i<N-1;i++){
        cross.pb(getCrossPoint(cir[i],cir[i+1]));
    }

    NN = cross.size() * 2 + 2;
    poi.push_back(Point(x[0],y[0]));
    for(int i=0;i<cross.size();i++){
        poi.push_back(cross[i].fi);
        poi.push_back(cross[i].se);
    }
    poi.push_back(Point(x[N-1],y[N-1]));

    vector<vector<double>> d(NN,vector<double>(NN,inf));
    for(int i=0;i<NN;i++) d[i][i] = 0;
    for(int i=0;i<NN;i++){
        for(int j=i+1;j<NN;j++){
            d[i][j] = d[j][i] = distance(poi[i],poi[j]);
        }
    }

    //cerr << "distance " << endl;
    //for(int i=0;i<NN;i++){
    //    for(int j=0;j<NN;j++){
    //        if(d[i][j]==inf) cerr << "inf  " << " ";
    //        else printf("%-5.3f ",d[i][j]);
    //    }
    //    cerr << endl;
    //}

    for(int i=0;i<NN;i++){
        for(int j=i+1;j<NN;j++){
            bool ok = true;
            int idx1,idx2;
            idx1 = (i%2==0) ? i+1 : i+2;
            idx2 = (j%2==1) ? j-1 : j-2;
            Vector a(poi[i]-poi[j]);
            //cout << i << " " << j << " " << idx1 <<" " << idx2 << endl;
            for(int k=idx1;k<=idx2;k+=2){
                if(k+1>idx2) break;
                Vector b(poi[k]-poi[i]);
                Vector c(poi[k+1]-poi[i]);
                double t1 = a.x*b.y - a.y*b.x;
                double t2 = a.x*c.y - a.y*c.x;
                //if(i==0 and j==7){
                //    //cout << k << " " << k+1 << " " << t1 << " " << t2 << endl;
                //}
                if(t1*t2 >= 0){
                    //cout << "NG  between " << k << " " << k+1 << endl;
                    ok=false;
                    break;
                }
            }
            if(!ok){
                d[i][j] = d[j][i] = inf;
            }
            //cerr << i << " " << j << " " << (ok? "true" : "false") << endl;
        }
    }

    /*
    cerr << "Point : " << endl;
    for(int i=0;i<NN;i++){
        cerr << i << " " << poi[i] << endl;
    }

    cerr << "distance " << endl;
    for(int i=0;i<NN;i++){
        for(int j=0;j<NN;j++){
            if(d[i][j]==inf) cerr << "inf  " << " ";
            else printf("%-5.3f ",d[i][j]);
        }
        cerr << endl;
    }
    */


    //cout << Double(d[0][5]) << " " << Double(d[5][11]) << " " << Double(d[11][13]) << " " << Double(d[13][19]) << endl; 
    //cout << d[0][11] << endl;
    /*
    cout << "0,3 : " << Double(d[0][3]) << endl;
    cout << "0,5 : " << Double(d[0][5]) << endl;
    cout << "3,5 : " << Double(d[3][5]) << endl;
    cout << "3,7 : " << Double(d[3][7]) << endl;
    cout << "5,7 : " << Double(d[5][7]) << endl;
    */



    for(int k=0;k<NN;k++){
        for(int i=0;i<NN;i++){
            for(int j=0;j<NN;j++){
                //if(abs(inf-d[i][k])<eps or abs(inf-d[k][j])<eps) continue;
                double tmp = d[i][j];
                d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
                //if(i==0 and j==11 and abs(tmp-d[i][j]) > eps){
                //    cout << "check :: " << k << "   "  << Double(tmp)<< " -> " << Double(d[i][j]) << endl;
                //    cout << Double(d[i][k]) << " " << Double(d[k][j]) << endl;
                //}
                //if(i==0 and j==7){
                //    cout << "0 and 7" << endl;
                //    cout <<  k << "   "  << Double(tmp)<< " -> " << Double(d[i][j]) << endl;
                //    cout << Double(d[i][k]) << " " << Double(d[k][j]) << endl;
                //}
            }
        }
    }

    //cerr << "distance " << endl;
    //for(int i=0;i<NN;i++){
    //    for(int j=0;j<NN;j++){
    //        if(abs(d[i][j]-inf) < eps) cerr << "inf  " << " ";
    //        else printf("%-5.3f ",d[i][j]);
    //    }
    //    cerr << endl;
    //}


    //cout << "0 .. 7 : " << d[0][7] << endl;
    //cout << "0..5 + 5..7 : " << d[0][5] + d[5][7] << endl;


    //cout << Double(d[0][5]) << " " << Double(d[5][11]) << " " << Double(d[11][13]) << " " << Double(d[13][19]) << endl; 

    //cout << Double(d[0][5]) << " " << Double(d[5][11]) << " " << Double(d[0][11]) << endl;
    //cout << Double(d[0][5] + d[5][11]) << endl;

    //cout << Double(d[0][5] + d[5][11] + d[11][13] + d[13][19] ) << endl;

    cout << Double(d[0][NN-1]) << endl;
}

int main(){
    while(1){
        cin >> N;
        if(N==0) break;
        solve();
    }
}