Problem: Two experienced climbers are planning a first-ever attempt: they start at two points of the equal altitudes on a mountain range, move back and forth on a single route keeping their altitudes equal, and finally meet with each other at a point on the route. A wise man told them that if a route has no point lower than the start points (of the equal altitudes) there is at least a way to achieve the attempt. This is the reason why the two climbers dare to start planning this fancy attempt.

The two climbers already obtained altimeters (devices that indicate altitude) and communication devices that are needed for keeping their altitudes equal. They also picked up a candidate route for the attempt: the route consists of consequent line segments without branches; the two starting points are at the two ends of the route; there is no point lower than the two starting points of the equal altitudes. An illustration of the route is given in Figure E.1 (this figure corresponds to the first dataset of the sample input).

<image>

Figure E.1: An illustration of a route

The attempt should be possible for the route as the wise man said. The two climbers, however, could not find a pair of move sequences to achieve the attempt, because they cannot keep their altitudes equal without a complex combination of both forward and backward moves. For example, for the route illustrated above: a climber starting at p1 (say A) moves to s, and the other climber (say B) moves from p6 to p5; then A moves back to t while B moves to p4; finally A arrives at p3 and at the same time B also arrives at p3. Things can be much more complicated and thus they asked you to write a program to find a pair of move sequences for them.

There may exist more than one possible pair of move sequences, and thus you are requested to find the pair of move sequences with the shortest length sum. Here, we measure the length along the route surface, i.e., an uphill path from (0, 0) to (3, 4) has the length of 5.



Input

The input is a sequence of datasets.

The first line of each dataset has an integer indicating the number of points N (2 ≤ N ≤ 100) on the route. Each of the following N lines has the coordinates (xi, yi) (i = 1, 2, ... , N) of the points: the two start points are (x1, y1) and (xN, yN); the line segments of the route connect (xi, yi) and (xi+1, yi+1) for i = 1, 2, ... , N - 1. Here, xi is the horizontal distance along the route from the start point x1, and yi is the altitude relative to the start point y1. All the coordinates are non-negative integers smaller than 1000, and inequality xi < xi+1 holds for i = 1, 2, .. , N - 1, and 0 = y1 = yN ≤ yi for i = 2, 3, ... , N - 1.

The end of the input is indicated by a line containing a zero.

Output

For each dataset, output the minimum sum of lengths (along the route) of move sequences until the two climbers meet with each other at a point on the route. Each output value may not have an error greater than 0.01.

Example

Input

6
0 0
3 4
9 12
17 6
21 9
33 0
5
0 0
10 0
20 0
30 0
40 0
10
0 0
1 2
3 0
6 3
9 0
11 2
13 0
15 2
16 2
18 0
7
0 0
150 997
300 1
450 999
600 2
750 998
900 0
0


Output

52.5
40.0
30.3356209304689
10078.072814085803

Solution (in CPP):

Solution:
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Point{
	void set(double arg_x,double arg_y){
		x = arg_x;
		y = arg_y;
	}
	bool operator<(const struct Point &arg) const{
		return x < arg.x;
	}
	double x,y;
};

Point point[100];

struct Line{
	Point p1,p2;
	double dist;
};

struct Data{
	Data(int arg_loc1,int arg_loc2,double arg_sum_dist){
		loc1 = arg_loc1;
		loc2 = arg_loc2;
		sum_dist = arg_sum_dist;
	}
	bool operator<(const struct Data &arg) const{
		return sum_dist > arg.sum_dist;
	}
	int loc1,loc2;
	double sum_dist;
};

int N;
map<int,double>MIN_COST,DIST;

Point calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){
	Point ret;
	ret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));
	if(x1 != x2){
		ret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);
	}else{
		ret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);
	}
	return ret;
}

double calc_dist(Line line){
	return sqrt((line.p1.x-line.p2.x)*(line.p1.x-line.p2.x)+(line.p1.y-line.p2.y)*(line.p1.y-line.p2.y));
}

int getNUM(int a,int b){
	return a*10000+b;
}


void func(){

	MIN_COST.clear();
	DIST.clear();
	vector<int> HEIGHT;

	for(int i = 0; i < N; i++){
		scanf("%lf %lf",&point[i].x,&point[i].y);
		if(point[i].y > 0){
			HEIGHT.push_back(point[i].y);
		}
	}

	sort(HEIGHT.begin(),HEIGHT.end());
	HEIGHT.erase(unique(HEIGHT.begin(),HEIGHT.end()),HEIGHT.end());

	vector<Point> info;

	for(int i = 0; i < N; i++)info.push_back(point[i]);

	Line tmp;
	Point ret;

	for(int i = 0; i < HEIGHT.size(); i++){
		for(int k = 0; k < N-1; k++){
			tmp.p1 = point[k];
			tmp.p2 = point[k+1];

			if(tmp.p1.y > tmp.p2.y){
				swap(tmp.p1,tmp.p2);
			}

			if(HEIGHT[i] > tmp.p1.y && HEIGHT[i] < tmp.p2.y){
				ret = calc_Cross_Point(tmp.p1.x,tmp.p2.x,-10000.0,10000.0,tmp.p1.y,tmp.p2.y,HEIGHT[i],HEIGHT[i]);
				info.push_back(ret);
			}
		}
	}

	sort(info.begin(),info.end());

	N = (int)(info.size()-1);

	double tmp_dist;

	for(int i = 0; i < info.size()-1; i++){
		tmp.p1 = info[i];
		tmp.p2 = info[i+1];
		tmp_dist = calc_dist(tmp);
		DIST[getNUM(i,i+1)] = tmp_dist;
		DIST[getNUM(i+1,i)] = tmp_dist;
	}

	double ans = DBL_MAX;

	MIN_COST[getNUM(0,N)] = 0.0;
	priority_queue<Data> Q;
	Q.push(Data(0,N,0));


	int tmp_num,next_loc_1,next_loc_2;
	double add_dist;

	while(!Q.empty()){

		if(Q.top().loc1 == Q.top().loc2){
			ans = min(ans,Q.top().sum_dist);
			Q.pop();
		}else if(Q.top().loc2 == Q.top().loc1+1 && info[Q.top().loc2].y == info[Q.top().loc1].y){
			ans = min(ans,Q.top().sum_dist+DIST[getNUM(Q.top().loc1,Q.top().loc2)]);
			Q.pop();
		}else if(Q.top().sum_dist > MIN_COST[getNUM(Q.top().loc1,Q.top().loc2)]){
			Q.pop();
		}else{

			for(int i = -1; i <= 1; i += 1){
				if((Q.top().loc1 == 0 && i == -1) || (Q.top().loc1 == N && i == 1))continue;
				for(int k = -1; k <= 1; k += 1){
					if((Q.top().loc2 == 0 && k == -1) || (Q.top().loc2 == N && k == 1))continue;

					next_loc_1 = Q.top().loc1+i;
					next_loc_2 = Q.top().loc2+k;

					if(fabs(info[next_loc_1].y-info[next_loc_2].y) > EPS || next_loc_1 > next_loc_2)continue;

					add_dist = 0.0;
					if(i != 0){
						add_dist += DIST[getNUM(next_loc_1,Q.top().loc1)];
					}

					if(k != 0){
						add_dist += DIST[getNUM(next_loc_2,Q.top().loc2)];
					}

					tmp_num = getNUM(next_loc_1,next_loc_2);
					auto at = MIN_COST.find(tmp_num);

					if(at != MIN_COST.end()){
						if(MIN_COST[tmp_num] > Q.top().sum_dist+add_dist){
							MIN_COST[tmp_num] = Q.top().sum_dist+add_dist;
							Q.push(Data(next_loc_1,next_loc_2,MIN_COST[tmp_num]));
						}
					}else{
						MIN_COST[tmp_num] = Q.top().sum_dist+add_dist;
						Q.push(Data(next_loc_1,next_loc_2,MIN_COST[tmp_num]));
					}
				}
			}
			Q.pop();
		}
	}

	printf("%.10lf\n",ans);
}

int main(){

	while(true){
		scanf("%d",&N);
		if(N == 0)break;

		func();
	}

	return 0;
}