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 JAVA):

Solution:
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        double INF = Double.MAX_VALUE / 2;
        while (true) {
            int n = sc.nextInt();
            if (n == 0)
                break;
            Circle[] cs = new Circle[n];
            for (int i = 0; i < n; i++) {
                cs[i] = new Circle(sc.nextInt(), sc.nextInt(), sc.nextInt());
            }
            Point[] ps = new Point[2 * n];
            ps[0] = new Point(cs[0].x, cs[0].y);
            ps[ps.length - 1] = new Point(cs[n - 1].x, cs[n - 1].y);
            int c = 1;
            Line[] ls = new Line[n - 1];
            for (int i = 1; i < n; i++) {
                Point[] cis = cs[i].circleInterSection(cs[i - 1]);
                ps[c++] = cis[0];
                ps[c++] = cis[1];
                ls[i - 1] = new Line(cis[0], cis[1]);
            }
            // System.out.println(Arrays.toString(ps));
            double[][] cost = new double[2 * n][2 * n];
            for (int i = 0; i < 2 * n; i++) {
                Arrays.fill(cost[i], INF);
            }
            for (int i = 0; i <= 2 * n - 4; i++) {
                for (int j = i + 1; j < 2 * n; j++) {
                    if (j % 2 == 0) {
                        Line l1 = new Line(ps[i], ps[j]);
                        boolean inCircle = true;
                        for (int k = i % 2 == 0 ? i / 2 : i / 2 + 1; k < j / 2 - 1; k++) {
                            Line l2 = ls[k];
                            if (!l2.intersectsLine(l1)) {
                                inCircle = false;
                                break;
                            }
                        }
                        if (inCircle) {
                            // System.out.println(i + " => " + j);
                            cost[i][j] = l1.length();
                        }
                    } else {
                        Line l1 = new Line(ps[i], ps[j]);
                        boolean inCircle = true;
                        for (int k = i % 2 == 0 ? i / 2 : i / 2 + 1; k < j / 2; k++) {
                            Line l2 = ls[k];
                            if (!l2.intersectsLine(l1)) {
                                inCircle = false;
                                break;
                            }
                        }
                        if (inCircle) {
                            // System.out.println(i + " => " + j);
                            cost[i][j] = l1.length();
                        }
                    }
                }
            }
            // for (int i = 0; i < 2 * n; i++) {
            // System.out.println(Arrays.toString(cost[i]));
            // }
            cost[2 * n - 3][2 * n - 1] = new Line(ps[2 * n - 3], ps[2 * n - 1])
                    .length();
            cost[2 * n - 2][2 * n - 1] = new Line(ps[2 * n - 2], ps[2 * n - 1])
                    .length();
            for (int k = 0; k < 2 * n; k++) {
                for (int i = 0; i < 2 * n; i++) {
                    if (k < i)
                        break;
                    for (int j = i + 1; j < 2 * n; j++) {
                        cost[i][j] = Math.min(cost[i][j], cost[i][k]
                                + cost[k][j]);
                    }
                }
            }
            System.out.printf("%.5f\n", cost[0][2 * n - 1]);
        }

    }
}

@SuppressWarnings("serial")
class Point extends Point2D.Double {
    Point(int x, int y) {
        super(x, y);
    }

    Point(double x, double y) {
        super(x, y);
    }
}

@SuppressWarnings("serial")
class Line extends Line2D.Double {

    Line(Point p1, Point p2) {
        super(p1, p2);
    }

    public double length() {
        return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
    }

    public double ip(Line l) {
        Point q1 = new Point(l.x1, l.y1);
        Point q2 = new Point(l.x2, l.y2);
        return (x2 - x1) * (q2.x - q1.x) + (y2 - y1) * (q2.y - q1.y);
    }

    public double ep(Line l) {
        Point q1 = new Point(l.x1, l.y1);
        Point q2 = new Point(l.x2, l.y2);
        return (x2 - x1) * (q2.y - q1.y) - (y2 - y1) * (q2.x - q1.x);
    }
}

class Circle {
    int x;
    int y;
    int r;

    Circle(int x, int y, int r) {
        this.x = x;
        this.y = y;
        this.r = r;
    }

    public Point[] circleInterSection(Circle c2) {
        Circle c1 = this;
        double a = c1.x;
        double b = c1.y;
        double r = c1.r;
        double c = c2.x;
        double d = c2.y;
        double s = c2.r;
        double norm = (c - a) * (c - a) + (d - b) * (d - b);
        double t = 0.5 * (norm + r * r - s * s) / Math.sqrt(norm);
        double th = Math.acos(t / r);
        double th0 = Math.atan2((d - b), (c - a));
        double p1 = a + r * Math.cos(th + th0);
        double q1 = b + r * Math.sin(th + th0);
        double p2 = a + r * Math.cos(-th + th0);
        double q2 = b + r * Math.sin(-th + th0);
        Point[] ps = { new Point(p1, q1), new Point(p2, q2) };
        return ps;
    }

    public String toString() {
        return "x,y,r " + x + " " + y + " " + r + " ";
    }

}