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 <algorithm>
#include <limits>
#include <complex>
#include <vector>
#include <queue>
#include <cmath>

//#define _DEBUG
//#ifdef _DEBUG
//#endif

using namespace std;

typedef complex< double > Point;
typedef complex< double > Vector;
const double kEps = 1e-8;
const double kInf = numeric_limits< double >::max() / 2;

struct Circle {
  Point cntr;
  double r;

  friend istream & operator >> (istream &is, Circle &rc) {
    double x, y;
    is >> x >> y >> rc.r, rc.cntr = Point(x, y);
    return is;
  }
};

struct PQNode {
  int u;
  double cst;

  PQNode(int _u, double _cst) { u = _u, cst = _cst; }
  bool operator > (const PQNode &rpq) const { return cst > rpq.cst + kEps; }
};

typedef priority_queue< PQNode, vector< PQNode >, greater< PQNode > > PQue;

struct Edge {
  int v, nxt;
  double ln;
} edges[100000];

int N, S, T;
Circle crc[105];
Point p[210];
int gph[210], edge_cnt;
double f[210];

void read_circle(void);
void build_graph(void);
void solve(void);
bool is_intr(Point, Point, Point, Point);
void add_edge(int, int, double);

inline bool fle(double a, double b) { return a < b + kEps; }
inline bool flt(double a, double b) { return a + kEps < b; }
inline double cross(Vector u, Vector v) { return imag(conj(u) * v); }

int main() {
  cout.setf(ios::fixed), cout.precision(4);
  while (cin >> N, N) {
    read_circle();
    build_graph();
    solve();
  }

  return 0;
}

void read_circle(void) {
  for (int i = 0; i < N; ++i)
    cin >> crc[i];
}

void build_graph(void) {
  for (int i = 0; i < N - 1; ++i) {
    Point &c1 = crc[i].cntr, &c2 = crc[i + 1].cntr;
    double r1 = crc[i].r, r2 = crc[i + 1].r, d, cos_tht;

    d = abs(c2 - c1);
    cos_tht = (r1 * r1 + d * d - r2 * r2) / (2 * r1 * d);

    Vector rv1, rv2, u1, u2;

    rv1 = Vector(cos_tht, sqrt(1 - cos_tht * cos_tht));
    rv2 = Vector(cos_tht, -sqrt(1 - cos_tht * cos_tht));
    u1 = u2 = c2 - c1;
    u1 = u1 * rv1 * r1 / d, u2 = u2 * rv2 * r1 / d;

    p[2 * i] = c1 + u1, p[2 * i + 1] = c1 + u2;

#ifdef _DEBUG
  cout << 2 * i << ": " << p[2 * i] << endl;
  cout << 2 * i + 1 << ": " << p[2 * i + 1] << endl;
#endif
  }
  p[S = 2 * (N - 1)] = crc[0].cntr, p[T = 2 * (N - 1) + 1] = crc[N - 1].cntr;

  bool is_valid;

  fill(&gph[0], &gph[2 * N], -1), edge_cnt = 0;
  for (int i = 0; i < 2 * (N - 1); ++i) {
    is_valid = true;
    for (int j = 0; j < i / 2 && is_valid; ++j)
      if (!is_intr(p[S], p[i], p[2 * j], p[2 * j + 1]))
        is_valid = false;
    if (is_valid)
      add_edge(S, i, abs(p[i] - p[S]));
  }

  for (int i = 0; i < 2 * (N - 1); ++i) {
    is_valid = true;
    for (int j = i / 2 + 1; j < N - 1 && is_valid; ++j)
      if (!is_intr(p[i], p[T], p[2 * j], p[2 * j + 1]))
        is_valid = false;
    if (is_valid)
      add_edge(i, T, abs(p[T] - p[i]));
  }

  for (int i = 0; i < 2 * (N - 1); ++i)
    for (int j = i + 1; j < 2 * (N - 1); ++j) {
      is_valid = true;
      for (int k = i / 2 + 1; k < j / 2 && is_valid; ++k)
        if (!is_intr(p[i], p[j], p[2 * k], p[2 * k + 1]))
          is_valid = false;
      if (is_valid)
        add_edge(i, j, abs(p[j] - p[i]));
    }

  is_valid = true;
  for (int i = 0; i < N - 1 && is_valid; ++i)
    if (!is_intr(p[S], p[T], p[2 * i], p[2 * i + 1]))
      is_valid = false;
  if (is_valid)
    add_edge(S, T, abs(p[T] - p[S]));

#ifdef _DEBUG
  for (int u = 0; u < 2 * N; ++u) {
    cout << "u = " << u << endl;
    for (int eid = gph[u]; eid != -1; eid = edges[eid].nxt) {
      int v = edges[eid].v;
      double ln = edges[eid].ln;
      cout << "  v = " << v << ", ln = " << ln << endl;
    }
  }
#endif
}

void solve(void) {
  PQue pq;
  double res = 0;

  fill(&f[0], &f[2 * N], kInf), f[S] = 0, pq.push(PQNode(S, 0));
  while (!pq.empty()) {
    PQNode tp(pq.top());  pq.pop();
    int u = tp.u;
    double cst = tp.cst;

    if (flt(f[u], cst)) continue;
    if (u == T) { res = cst;  break; }

    for (int eid = gph[u]; eid != -1; eid = edges[eid].nxt) {
      int v = edges[eid].v;
      double ln = edges[eid].ln;

      if (flt(f[u] + ln, f[v]))
        f[v] = f[u] + ln, pq.push(PQNode(v, f[v]));
    }
  }

  cout << res << endl;
}

bool is_intr(Point p1, Point p2, Point p3, Point p4) {
  return (fle(min(real(p3), real(p4)), max(real(p1), real(p2))) &&
          fle(min(real(p1), real(p2)), max(real(p3), real(p4))) &&
          fle(min(imag(p3), imag(p4)), max(imag(p1), imag(p2))) &&
          fle(min(imag(p1), imag(p2)), max(imag(p3), imag(p4))) &&
          fle(cross(p3 - p1, p3 - p4) * cross(p3 - p2, p3 - p4), 0) &&
          fle(cross(p1 - p3, p1 - p2) * cross(p1 - p4, p1 - p2), 0)
          );
}

void add_edge(int u, int v, double ln) {
  Edge &r1 = edges[edge_cnt], &r2 = edges[edge_cnt + 1];
  r1.v = v, r1.ln = ln, r1.nxt = gph[u], gph[u] = edge_cnt;
  r2.v = u, r2.ln = ln, r2.nxt = gph[v], gph[v] = edge_cnt + 1;
  edge_cnt += 2;
}