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>
#define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef complex<double> Complex;
typedef pair<Complex, Complex> pcc;

pcc circles_intersect_points(Complex c1, double r1, Complex c2, double r2) {
  Complex u = c2 - c1;

  double d = abs(u);
  double d1 = (r1 * r1 - r2 * r2 + d * d) / (d * 2);
  double l = sqrt(r1 * r1 - d1 * d1);

  Complex v = Complex(u.imag(), -u.real()) / d;
  Complex g = c1 + u * (d1 / d);

  pcc ret;
  ret.fi = g + v * l;
  ret.se = g - v * l;
  return ret;
}

double det(Complex a, Complex b) {
  return a.real() * b.imag() - a.imag() * b.real();
}

bool line_segments_intersect(Complex a, Complex b, Complex c, Complex d) {
  double ta = det(a - c, d - c);
  double tb = det(b - c, d - c);
  double tc = det(c - a, b - a);
  double td = det(d - a, b - a);
  return ta * tb < 0 && tc * td < 0;
}

bool ok(const vector<pcc> &lines, Complex v1, Complex v2, ll p, ll q) {
  REP(i, p, q + 1) {
    if(!line_segments_intersect(lines[i].fi, lines[i].se, v1, v2)) {
      return false;
    }
  }
  return true;
}

int main(void) {
  ll N;
  while(cin >> N, N) {
    vector<Complex> P(N);
    vector<ll> R(N);
    REP(i, 0, N) {
      ll x, y;
      cin >> x >> y >> R[i];
      P[i] = Complex(x, y);
    }

    vector<pcc> lines(N - 1);
    REP(i, 0, N - 1) {
      lines[i] = circles_intersect_points(P[i], R[i], P[i + 1], R[i + 1]);
    }

    Complex S = P[0], G = P[N - 1];
    vector<Complex> points(N * 2);
    REP(i, 0, N - 1) {
      points[i * 2] = lines[i].fi;
      points[i * 2 + 1] = lines[i].se;
    }
    points[N * 2 - 2] = S;
    points[N * 2 - 1] = G;

#define add_edge(u, v) { \
  E[u].push_back(v); \
  E[v].push_back(u); \
}

    vector<vector<ll>> E(N * 2);
    if(ok(lines, S, G, 0, N - 2)) add_edge(N * 2 - 2, N * 2 - 1);
    REP(i, 0, N - 1) {
      if(ok(lines, S, lines[i].fi, 0, i - 1)) add_edge(N * 2 - 2, i * 2);
      if(ok(lines, S, lines[i].se, 0, i - 1)) add_edge(N * 2 - 2, i * 2 + 1);
      if(ok(lines, lines[i].fi, G, i + 1, N - 2)) add_edge(i * 2, N * 2 - 1);
      if(ok(lines, lines[i].se, G, i + 1, N - 2)) add_edge(i * 2 + 1, N * 2 - 1);
    }
    REP(i, 0, N - 1) REP(j, i + 1, N - 1) {
      if(ok(lines, lines[i].fi, lines[j].fi, i + 1, j - 1)) add_edge(i * 2, j * 2);
      if(ok(lines, lines[i].fi, lines[j].se, i + 1, j - 1)) add_edge(i * 2, j * 2 + 1);
      if(ok(lines, lines[i].se, lines[j].fi, i + 1, j - 1)) add_edge(i * 2 + 1, j * 2);
      if(ok(lines, lines[i].se, lines[j].se, i + 1, j - 1)) add_edge(i * 2 + 1, j * 2 + 1);
    }

    struct state {
      ll v;
      double cost;
      bool operator<(const state s) const { return cost > s.cost; }
    };

    vector<double> dp(N * 2, 1e20);
    priority_queue<state> q;
    q.push((state) { N * 2 - 2, 0 });

    while(q.size()) {
      ll v = q.top().v;
      double cost = q.top().cost;
      q.pop();
      if(dp[v] <= cost) continue;
      dp[v] = cost;
      for(ll nv : E[v]) {
        double ncost = cost + abs(points[v] - points[nv]);
        if(dp[nv] > ncost) q.push((state) { nv, ncost });
      }
    }

    printf("%.15lf\n", dp[N * 2 - 1]);
  }
}