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:
#define  _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-10L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define seg_size 262144LL
#define MOD 998244353LL
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

typedef complex<long double> Point;
typedef pair<complex<long double>, complex<long double>> Line;

typedef struct Circle {
    complex<long double> center;
    long double r;
}Circle;

long double dot(Point a, Point b) {
    return (a.real() * b.real() + a.imag() * b.imag());
}
long double cross(Point a, Point b) {
    return (a.real() * b.imag() - a.imag() * b.real());
}

long double Dist_Line_Point(Line a, Point b) {
    if (dot(a.second - a.first, b - a.first) < eps) return abs(b - a.first);
    if (dot(a.first - a.second, b - a.second) < eps) return abs(b - a.second);
    return abs(cross(a.second - a.first, b - a.first)) / abs(a.second - a.first);
}

int is_intersected_ls(Line a, Line b) {
    return (cross(a.second - a.first, b.first - a.first) * cross(a.second - a.first, b.second - a.first) < 0) &&
        (cross(b.second - b.first, a.first - b.first) * cross(b.second - b.first, a.second - b.first) < 0);
}

Point intersection_l(Line a, Line b) {
    Point da = a.second - a.first;
    Point db = b.second - b.first;
    return a.first + da * cross(db, b.first - a.first) / cross(db, da);
}

long double Dist_Line_Line(Line a, Line b) {
    if (is_intersected_ls(a, b) == 1) {
        return 0;
    }
    return min({ Dist_Line_Point(a,b.first), Dist_Line_Point(a,b.second),Dist_Line_Point(b,a.first),Dist_Line_Point(b,a.second) });
}

pair<Point, Point> intersection_Circle_Circle(Circle a, Circle b) {
    long double dist = abs(a.center - b.center);
    assert(dist <= eps + a.r + b.r);
    assert(dist+eps >= abs(a.r - b.r));
    Point target = b.center - a.center;
    long double pointer = target.real() * target.real() + target.imag() * target.imag();
    long double aa = pointer + a.r * a.r - b.r * b.r;
    aa /= 2.0L;
    Point l{ (aa * target.real() + target.imag() * sqrt(pointer * a.r * a.r - aa * aa))/pointer,
            (aa* target.imag() - target.real() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer};
    Point r{ (aa * target.real() - target.imag() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer,
        (aa * target.imag() + target.real() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer };
    r = r + a.center;
    l = l + a.center;
    return mp(l, r);
}
void init() {
    iostream::sync_with_stdio(false);
    cout << fixed << setprecision(20);
}

unsigned long xor128() {
    static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    unsigned long t = (x ^ (x << 11));
    x = y; y = z; z = w;
    return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

#define int ll
void solve(){
    vector<long double> answ;
    while (true) {
        int n;
        cin >> n;
        if (n == 0) {
            REP(i, answ.size()) {
                cout << answ[i] << endl;
            }
            return;
        }
        vector<Circle> inputs;
        REP(i, n) {
            long double a, b, c;
            cin >> a >> b >> c;
            inputs.push_back(Circle{ Point{a,b},c });
        }
        vector<Line> barriers;
        REP(i, n - 1) {
            barriers.push_back(intersection_Circle_Circle(inputs[i], inputs[i + 1]));
        }
        long double dp[101][2] = {};
        REP(i, n-1) {
            dp[i][0] = 1e9;
            dp[i][1] = 1e9;
            for (int j = 0; j < i; ++j) {
                {
                    int ok = 1;
                    for (int t = j+1; t < i; ++t) {
                        if (is_intersected_ls(barriers[t], Line{ barriers[j].first,barriers[i].first }) == 0) {
                            ok = 0;
                            break;
                        }
                    }
                    if (ok == 1) {
                        dp[i][0] = min(dp[i][0], dp[j][0] + abs(barriers[i].first - barriers[j].first));
                    }
                }
                {
                    int ok = 1;
                    for (int t = j + 1; t < i; ++t) {
                        if (is_intersected_ls(barriers[t], Line{ barriers[j].second,barriers[i].first }) == 0) {
                            ok = 0;
                            break;
                        }
                    }
                    if (ok == 1) {
                        dp[i][0] = min(dp[i][0], dp[j][1] + abs(barriers[i].first - barriers[j].second));
                    }
                }
                {
                    int ok = 1;
                    for (int t = j + 1; t < i; ++t) {
                        if (is_intersected_ls(barriers[t], Line{ barriers[j].second,barriers[i].second }) == 0) {
                            ok = 0;
                            break;
                        }
                    }
                    if (ok == 1) {
                        dp[i][1] = min(dp[i][1], dp[j][1] + abs(barriers[i].second - barriers[j].second));
                    }
                }
                {
                    int ok = 1;
                    for (int t = j + 1; t < i; ++t) {
                        if (is_intersected_ls(barriers[t], Line{ barriers[j].first,barriers[i].second }) == 0) {
                            ok = 0;
                            break;
                        }
                    }
                    if (ok == 1) {
                        dp[i][1] = min(dp[i][1], dp[j][0] + abs(barriers[i].second - barriers[j].first));
                    }
                }
            }
            
            {
                int ok = 1;
                REP(j,i){
                    if (is_intersected_ls(barriers[j], Line{ inputs[0].center,barriers[i].first }) == 0) {
                        ok = 0;
                        break;
                    }
                }
                if (ok == 1) {
                    dp[i][0] = min(dp[i][0], abs(barriers[i].first - inputs[0].center));
                }
            }
            {
                int ok = 1;
                REP(j, i) {
                    if (is_intersected_ls(barriers[j], Line{ inputs[0].center,barriers[i].second }) == 0) {
                        ok = 0;
                        break;
                    }
                }
                if (ok == 1) {
                    dp[i][1] = min(dp[i][1], abs(barriers[i].second - inputs[0].center));
                }
            }
        }
        long double ans = 1e9;
        for (int j = n - 2; j >= 0; --j) {
            {
                int ok = 1;
                for (int t = j + 1; t < n - 1;++t) {
                    if (is_intersected_ls(barriers[t], Line{ inputs.back().center,barriers[j].first }) == 0) {
                        ok = 0;
                        break;
                    }
                }
                if (ok == 1) {
                    ans = min(ans, dp[j][0] + abs(barriers[j].first - inputs.back().center));
                }
            }
            {
                int ok = 1;
                for (int t = j + 1; t < n - 1; ++t) {
                    if (is_intersected_ls(barriers[t], Line{ inputs.back().center,barriers[j].second }) == 0) {
                        ok = 0;
                        break;
                    }
                }
                if (ok == 1) {
                    ans = min(ans, dp[j][1] + abs(barriers[j].second - inputs.back().center));
                }
            }
        }
        {
            int ok = 1;
            for (int t = 0; t < barriers.size(); ++t) {
                if (is_intersected_ls(barriers[t], Line{ inputs.back().center,inputs[0].center }) == 0) {
                    ok = 0;
                    break;
                }
            }
            if (ok == 1) {
                ans = min(ans, abs(inputs[0].center - inputs.back().center));
            }
        }
        answ.push_back(ans);
        //cout << ans << endl;
    }
}

#undef int
int main() {
    init();
    solve();
}