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.io.*;
import java.util.*;
import java.awt.geom.*;

class Circle {
    public Point2D.Double o;
    public double r;
    Circle() {
        this.o = new Point2D.Double(0, 0);
        this.r = 0;
    }
    Circle(Point2D.Double o, double r) {
        this.o = o;
        this.r = r;
    }
    Point2D.Double[] intersectionPoint(Circle c) {
        Point2D.Double[] ip = new Point2D.Double[2];
        double l = o.distance(c.o);
        double a = Math.atan2(c.o.y - o.y, c.o.x - o.x);
        double t = Math.acos((r*r - c.r*c.r + l*l) / (2 * l * r));
        ip[0] = new Point2D.Double(r*Math.cos(a+t) + o.x, r*Math.sin(a+t) + o.y);
        ip[1] = new Point2D.Double(r*Math.cos(a-t) + o.x, r*Math.sin(a-t) + o.y);
        return ip;
    }
}

public class Main {
    int n;
    Circle[] cs;
    Point2D.Double[] p;
    Scanner sc;

    Main() {
        sc = new Scanner(System.in);
    }

    boolean init() {
        n = sc.nextInt();
        if (n == 0) return false;
        cs = new Circle[n];
        for (int i = 0; i < n; i++) {
            double x = sc.nextDouble();
            double y = sc.nextDouble();
            double r = sc.nextDouble();
            cs[i] = new Circle(new Point2D.Double(x, y), r);
        }

        p = new Point2D.Double[2*n];
        p[0] = cs[0].o; p[2*n-1] = cs[n-1].o;
        for (int i = 0; i < n-1; i++) {
            Point2D.Double[] _p = cs[i].intersectionPoint(cs[i+1]);
            p[i*2+1] = _p[0];
            p[i*2+2] = _p[1];
        }
 
//        for (int i = 0; i < 2*n; i++) {
//            System.out.println(p[i]);
//        }
        return true;
    }

    void run() {
        while (init()) {
            double[][] graph = new double[2*n][2*n];
            for (int i = 0; i < 2*n; i++) {
                for (int j = 0; j < 2*n; j++) {
                    Line2D.Double line = new Line2D.Double(p[i], p[j]);
                    if (0 <= j - i && j - i <= 2) {
                        graph[i][j] = p[i].distance(p[j]);
                        graph[j][i] = graph[i][j];
                    } else {
                        for (int k = (i+1)/2+1; k < (j+1)/2; k++) {
                            if (!line.intersectsLine(new Line2D.Double(p[k*2-1], p[k*2]))) {
//                                System.out.println("Line " + (k*2-1) + "," + k*2 + 
//                                        " and Line " + i + "," + j + " are not intersects");
                                graph[i][j] = 1e18;
                                break;
                            }
                            graph[i][j] = p[i].distance(p[j]);
                            graph[j][i] = graph[i][j];
                        }
                    }
                }
            }

//            for (int i = 0; i < 2*n; i++) {
//                for (int j = 0; j < 2*n; j++) {
//                    System.out.print(graph[i][j] + " ");
//                }
//                System.out.println("");
//            }

//            for (int i = 0; i < 2*n; i++)
//                System.out.print(p[i].x + "," + p[i].y + " ");
//            System.out.println("");

            for (int i = 0; i < 2*n; i++) {
                for (int j = 0; j < 2*n; j++) {
                    if (graph[i][j] == 0) 
                        graph[i][j] = 1e18;
//                    System.out.print(graph[i][j] + " ");
                }
//                System.out.println("");
            }
            for (int k = 0; k < 2*n; k++) 
                for (int i = 0; i < 2*n; i++) 
                    for (int j = 0; j < 2*n; j++) 
                        graph[i][j] = Math.min(graph[i][j], graph[i][k]+graph[k][j]);
//            for (int i = 0; i < 2*n; i++) {
//                for (int j = 0; j < 2*n; j++) {
//                    System.out.print(graph[i][j]);
//                }
//                System.out.println("");
//            }
            System.out.println(graph[0][2*n-1]);
        }
    }

    public static void main(String[] args) {
        new Main().run();
    }
}