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;

#define MAX_N 110
#define INF 1e9
#define EPS 1e-10
#define equal(a,b) (fabs(a-b) < EPS)
#define not_equal(a,b) (!equal(a,b))
#define lt(a,b) (a-b < -EPS)

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 &k)const{ return Point(x*k,y*k); }
  Point operator / (const double &k)const{ return Point(x/k,y/k); }
  bool operator < (const Point &p)const{ return x != p.x ? x < p.x : y < p.y; }
};

Point operator * (const Point &a,const Point &b){ return Point(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x); }

double dot(const Point &a,const Point &b){ return a.x*b.x+a.y*b.y; }
double cross(const Point &a,const Point &b){ return a.x*b.y - b.x*a.y; }
double norm(const Point &p){ return dot(p,p); }
double abs(const Point &p){ return sqrt(norm(p)); }
double getDistance(const Point &a,const Point &b){
  return sqrt(pow(a.x-b.x,2) + pow(a.y-b.y,2));
}

istream &operator >> (istream &is,Point &p){ 
  return is >> p.x >> p.y;
}

typedef Point Vector;

#define COUNTER_CLOCKWISE 1
#define CLOCKWISE -1
#define ONLINE_BACK 2
#define ONLINE_FRONT -2
#define ON_SEGMENT 0
typedef Point Vector;

int ccw(Point p0,Point p1,Point p2){
  Vector a = p1 - p0;
  Vector b = p2 - p0;
  if(cross(a,b) > EPS){ return COUNTER_CLOCKWISE; }
  if(cross(a,b) < -EPS){ return CLOCKWISE; }
  if(dot(a,b) < -EPS){ return ONLINE_BACK; }
  if(norm(a) < norm(b)){ return ONLINE_FRONT; }
  return ON_SEGMENT;
}

struct Segment{
  Point s,t;
  Segment(){}
  Segment(Point s,Point t) : s(s),t(t) {}
};

struct Circle{
  Point p;
  double r;
  Circle(){}
  Circle(Point p,double r) : p(p),r(r) {}
};

bool isIntersectSS(const Segment &a,const Segment &b){
  Point s[2] = {a.s,a.t}, t[2] = {b.s,b.t};
  return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0
    && ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;
}

vector<Point> crosspointCC(const Circle &a,const Circle &b){
  vector<Point> res;
  double d = abs(a.p-b.p);
  double rc = (a.r*a.r-b.r*b.r+d*d)/(2.0*d);
  double rs = sqrt(a.r*a.r-rc*rc);
  Point p = (b.p-a.p)/d;
  res.push_back(Point(a.p+p*Point(rc,-rs)));
  res.push_back(Point(a.p+p*Point(rc,rs)));
  return res;
}

struct P{
  double d;
  int n,p;
  P(double d,int n,int p) : d(d),n(n),p(p) {}
  bool operator < (const P &p)const{
    if(d != p.d){
      return d > p.d;
    }
  }
};

double dist[MAX_N][2][MAX_N][2];

double solve(int N){
  double d[MAX_N][2];
  fill(d[0],d[0]+MAX_N*2,INF);
  d[0][0] = 0.0;

  priority_queue<P> Q;
  Q.push(P(0,0,0));

  while(!Q.empty()){
    P p = Q.top(); Q.pop();
    int idx = p.n,pos = p.p;
    if(lt(d[idx][pos],p.d)){ continue; }
    if(idx == N){ return p.d; }
    for(int i = idx+1 ; i <= N ; i++){
      for(int j = 0 ; j < 2 ; j++){
        if(dist[idx][pos][i][j] == INF){ continue; }
        if(p.d+dist[idx][pos][i][j] < d[i][j]){
          d[i][j] = p.d+dist[idx][pos][i][j];
          Q.push(P(d[i][j],i,j));
        }
      }
    }
  }
  return -1;
}

void init(int N){
  for(int i = 0 ; i < N ; i++){
    for(int j = 0 ; j < 2 ; j++){
      for(int k = 0 ; k <= N ; k++){
        for(int l = 0 ; l < 2 ; l++){
          dist[i][j][k][l] = INF;
        }
      }
    }
  }
}

int main(){
  int N;
  while(cin >> N,N){
    vector<Point> G[MAX_N];
    vector<Circle> cirs(N);
    init(N);
    for(int i = 0 ; i < N ; i++){
      cin >> cirs[i].p >> cirs[i].r;
      if(i == 0){
        G[i].push_back(cirs[0].p);
        G[i].push_back(cirs[0].p);
      }else{
        vector<Point> cp = crosspointCC(cirs[i-1],cirs[i]);
        G[i].push_back(cp[0]); G[i].push_back(cp[1]);
      }
    }
    G[N].push_back(cirs[N-1].p);
    G[N].push_back(cirs[N-1].p);

    for(int i = 0 ; i < N ; i++){
      for(int j = i+1 ; j <= N ; j++){
        Point p1[2] = {G[i][0],G[i][1]};
        Point p2[2] = {G[j][0],G[j][1]};
        for(int k = 0 ; k < 2 ; k++){
          for(int l = 0 ; l < 2 ; l++){
            Segment a = Segment(p1[k],p2[l]);
            bool ok = true;
            for(int m = i ; m < j ; m++){
              Segment b = Segment(G[m][0],G[m][1]);
              if(!isIntersectSS(a,b)){
                ok = false;
                break;
              }
            }
            if(ok){
              dist[i][k][j][l] = getDistance(p1[k],p2[l]);
            }
          }
        }
      }
    }
    printf("%.8f\n",solve(N));
  }
  return 0;
}