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.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.lang.Math.*;

public class Main {

	int INF = 1 << 28;
	//long INF = 1L << 62;
	double EPS = 1e-10;
	
	int n;
	P[][] cps;
	C[] cs;
	double[][] es;
	
	void run() {
		Scanner sc = new Scanner(System.in);
		
		for(;;) {
			n = sc.nextInt();
			if( n == 0 ) break;
			
			cs = new C[n];
			for(int i=0;i<n;i++) cs[i] = new C(sc.nextDouble(), sc.nextDouble(), sc.nextDouble());
			
			cps = new P[n-1][];
			for(int i=0;i<n-1;i++) cps[i] = circleIntersection(cs[i].p, cs[i+1].p, cs[i].r, cs[i+1].r);
//			for(int i=0;i<n-1;i++) debug(cps[i][0].x, cps[i][0].y, cps[i][1].x, cps[i][1].y);
			
			es = new double[2*n][2*n];
			for(double[] a: es) fill(a, INF);
			
			for(int i=0;i<n-1;i++) {
				if(isCross(cs[0].p, cps[i][0], 0, i)) es[0][2*(i+1)-1] = cs[0].p.abs(cps[i][0]);
				if(isCross(cs[0].p, cps[i][1], 0, i)) es[0][2*(i+1)] = cs[0].p.abs(cps[i][1]);
				if(isCross(cs[n-1].p, cps[i][0], i+1, n-1)) es[2*(i+1)-1][2*n-1] = cs[n-1].p.abs(cps[i][0]);
				if(isCross(cs[n-1].p, cps[i][1], i+1, n-1)) es[2*(i+1)][2*n-1] = cs[n-1].p.abs(cps[i][1]);
			}
			
			for(int i=0;i<n-1;i++) for(int j=i+1;j<n-1;j++) {
				if(isCross(cps[i][0], cps[j][0], i+1, j)) es[2*(i+1)-1][2*(j+1)-1] = cps[i][0].abs(cps[j][0]);
				if(isCross(cps[i][0], cps[j][1], i+1, j)) es[2*(i+1)-1][2*(j+1)] = cps[i][0].abs(cps[j][1]);
				if(isCross(cps[i][1], cps[j][0], i+1, j)) es[2*(i+1)][2*(j+1)-1] = cps[i][1].abs(cps[j][0]);
				if(isCross(cps[i][1], cps[j][1], i+1, j)) es[2*(i+1)][2*(j+1)] = cps[i][1].abs(cps[j][1]);
			}
			
			if(isCross(cs[0].p, cs[n-1].p, 0, n-1)) es[0][2*n-1] = cs[0].p.abs(cs[n-1].p);
			
//			for(double[] a: es) debug(a);
			System.out.println(dijkstra());
		}
	}
	
	double dijkstra() {
		double[] d = new double[2*n];
		boolean[] used = new boolean[2*n];
		int[] path = new int[2*n];
		fill(d, INF);d[0] = 0;
		for(;;) {
			int v = -1;
			for(int u=0;u<2*n;u++) if(!used[u] && (v == -1 || d[v] > d[u])) v = u;
			if(v == -1) {
//				debug(path);
				return d[2*n-1];
			}
			
			used[v] = true;
			for(int u=0;u<2*n;u++) {
				path[u] = d[u] > d[v] + es[v][u]? v: path[u];
				d[u] = min(d[u], d[v] + es[v][u]);
				
			}
		}
	}
	
	boolean isCross(P s, P e, int p, int q) {
//		debug(s.x, s.y, e.x, e.y);
		for(int i=p;i<q;i++) {
			if(!cross(s, e, cps[i][0], cps[i][1])) return false;
		}
		return true;
	}

	void debug(Object... os) {
		System.err.println(Arrays.deepToString(os));
	}

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

	P[] circleIntersection(P a, P b, double r1, double r2) {
		P AB = b.sub(a);

		double ab = AB.abs();
		double ai = ( r1*r1 - r2*r2 + ab*ab ) / ( 2 * ab );
		P AI = AB.mlt(ai/ab);

		double ip = sqrt( r1*r1 - ai * ai);
		P IP = new P(AB.y * ip/ab, -AB.x* ip/ab);

		P P1 = a.add(AI).add(IP);

		IP = IP.mlt(-1.0);
		P P2 = a.add(AI).add(IP);
		
//		debug(ab, ai, ip);
//		debug("P1:", P1.x, P1.y);
//		debug("P2:", P2.x, P2.y);
		
		
		return new P[] {P1, P2};
	}
	
	//線分交差判定
	boolean cross(P p1, P p2, P q1, P q2) {
		return ccw(p1, p2, q1) * ccw(p1, p2, q2) <= 0 &&
			ccw(q1, q2, p1) * ccw(q1, q2, p2) <= 0;
	}

	//時計回りなのか．反時計回りなのか，直線状なのか，とにかく位置関係をみる
	int ccw (P a, P b, P c) {
		P s = b.sub(a);
		P t = c.sub(a);
		if( s.det(t) > EPS ) return 1;
		if( s.det(t) < -EPS ) return -1;
		if( s.dot(t) < -EPS ) return 2;
		if( s.dot(s) + EPS < t.dot(t) ) return -2;
		return 0;
	}
	
	class C {
		P p;
		double r;
		C(double x, double y, double r) {
			p = new P(x, y);
			this.r = r;
		}
	}
	
	class P {
		double EPS = 1e-10;
		double x, y;
		P(double x, double y) {
			this.x = x;
			this.y = y;
		}

		double add(double a, double b) {
			//誤差を考慮
			if( Math.abs(a+b) < EPS * ( Math.abs(a) + Math.abs(b) ) ) return 0;
			return a + b;
		}

		P add(P p) {	//和
			return new P(x+p.x, y+p.y);
		}

		P sub(P p) {	//差
			return new P(x-p.x, y-p.y);
		}

		P mlt(double k) {
			return new P(x*k, y*k);
		}

		double dot(P p) {	//内積
				return add(x*p.x, y*p.y);
		}

		double det(P p) {	//外積
			return add(x*p.y, -y*p.x);
		}

		double abs() {
			double ret = sqrt( add(x*x, y*y) );
			if ( Math.abs(ret) < EPS ) return 0;
			return ret;
		}

		double abs(P p) {
			double dx = add( x, -p.x );
			double dy = add( y, -p.y );
			double ret = sqrt( add(dx*dx, dy*dy) );
			if( Math.abs(ret) < EPS ) return 0;
			return ret;
		}
	}
}