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<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define EPS (1e-9)
#define COUNTER_CLOCKWISE 1
#define CLOCKWISE -1 
#define ONLINE_BACK 2
#define ONLINE_FRONT -2
#define ON_SEGMENT 0
#define equals(a,b) (fabs((a)-(b)) < EPS)

using namespace std;

const int IINF = INT_MAX;

// ----------------------

class Point{
public:
  double x,y;

  Point(double x = -IINF,double y = -IINF): x(x),y(y){}

  Point operator + (Point p){return Point(x+p.x,y+p.y);}
  Point operator - (Point p){return Point(x-p.x,y-p.y);}
  Point operator * (double a){return Point(a*x,a*y);}
  Point operator / (double a){return Point(x/a,y/a);}
  Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); }

  bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:y<p.y; }

  bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }

};

struct Segment{
  Point p1,p2;
  Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}
  bool operator == (const Segment& p)const { return p.p1 == p1 && p.p2 == p2; }
};

typedef Point Vector;
typedef Segment Line;

double dot(Point a,Point b){ return a.x*b.x + a.y*b.y; }

double cross(Point a,Point b){ return a.x*b.y - a.y*b.x; }

double norm(Point a){ return a.x*a.x+a.y*a.y; }

double abs(Point a){ return sqrt(norm(a)); }

int ccw(Point p0,Point p1,Point p2){
  Point a = p1-p0;
  Point b = p2-p0;
  if(cross(a,b) > EPS)return COUNTER_CLOCKWISE;
  if(cross(a,b) < -EPS)return CLOCKWISE;
  if(dot(a,b) < -EPS)return ONLINE_BACK;
  if(norm(a) < norm(b))return ONLINE_FRONT;
  return ON_SEGMENT;
}

Point crosspoint(Line l,Line m){
  double A = cross(l.p2-l.p1,m.p2-m.p1);
  double B = cross(l.p2-l.p1,l.p2-m.p1);
  if(abs(A) < EPS && abs(B) < EPS){
    vector<Point> vec;
    vec.push_back(l.p1),vec.push_back(l.p2),vec.push_back(m.p1),vec.push_back(m.p2);
    sort(vec.begin(),vec.end());
    assert(vec[1] == vec[2]);
    return vec[1];
  }
  if(abs(A) < EPS)assert(false);
  return m.p1 + (m.p2-m.p1)*(B/A);
}

// -------------------

struct Data {
  int L,R;
  double cost;
  bool operator < ( const Data& data ) const { return cost > data.cost; }
};

typedef pair<int,int> ii;
int N;
Point ps[101];
map<ii,double> mindist;

bool  LT(double a,double b) { return !equals(a,b) && a < b; } 
bool LTE(double a,double b) { return  equals(a,b) || a < b; } 
double getDist(double x,double y) { return sqrt(x*x+y*y); }

int getDir(Point s,Point t) {
  if( equals(s.y,t.y) ) return 0;
  if( LT(s.y,t.y) )     return 1;
  return -1;
}

ii canMove(int s,int coef,int t,const vector<Point>& vec){
  int next_s = s + coef;
  if( !( 0 <= next_s && next_s < vec.size() ) ) return ii(-1,-1);
  int dir = getDir(vec[s],vec[next_s]);
  if( dir == 0 ) return ii(t,-1);

  ii ret = ii(-1,-1);
  for(int d=-1;d<=1;d+=2){
    int cur_t = t;
    int next_t = t + d;
    while( ( 0 <= next_t && next_t < vec.size() ) && getDir(vec[cur_t],vec[next_t]) == dir ) {
      if( equals(vec[next_t].y,vec[next_s].y) ) {
        if( d == -1 ) ret.first  = next_t;
        else          ret.second = next_t;
        break;
      }
      cur_t = next_t; next_t = next_t + d;
    }
  }
  return ret;
}

inline void compute(){
  mindist.clear();
  vector<Point> vec;
  rep(i,N) {
    vec.push_back(ps[i]);
    Segment ver = Segment(Point(-2000,ps[i].y),Point(2000,ps[i].y));
    rep(j,N-1){
      Segment seg = Segment(ps[j],ps[j+1]);
      if( !equals(seg.p1.y,seg.p2.y) ) {
        if( LT(min(seg.p1.y,seg.p2.y),ver.p1.y) && LT(ver.p1.y,max(seg.p1.y,seg.p2.y)) ) {
          Point cp = crosspoint(ver,seg);
          vec.push_back(cp);
        }
      }
    }
  }

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

  int V = vec.size();
  priority_queue<Data> Q;
  Q.push((Data){0,V-1,0});
  mindist[ii(0,V-1)] = 0;
  while( !Q.empty() ){
    Data data = Q.top(); Q.pop();

    if( data.L == data.R ) {
      printf("%.10lf\n",data.cost);
      return;
    }

    ii next;
    for(int d=-1;d<=1;d+=2){
      next = ii(-1,-1);
      if( ( next = canMove(data.L,d*1,data.R,vec) ) != ii(-1,-1) ) {
        vector<int> to;
        if( next.first  != -1 ) to.push_back(next.first);
        if( next.second != -1 ) to.push_back(next.second);
        rep(j,to.size()){
          int ext = to[j];
          double dist = getDist(vec[data.L].x-vec[data.L+d*1].x,vec[data.L].y-vec[data.L+d*1].y) + getDist(vec[data.R].x-vec[ext].x,vec[data.R].y-vec[ext].y);
          ii p = ii(data.L+d*1,ext);
          if( !mindist.count(p) || ( mindist.count(p) && LT(data.cost + dist,mindist[p]) ) ) {
            mindist[p] = data.cost + dist;
            Q.push((Data){data.L+d*1,ext,mindist[p]});
          } 
        }
      }
    }

    for(int d=-1;d<=1;d+=2){
      next = ii(-1,-1);
      if( ( next = canMove(data.R,d*1,data.L,vec) ) != ii(-1,-1) ) {
        vector<int> to;
        if( next.first  != -1 ) to.push_back(next.first);
        if( next.second != -1 ) to.push_back(next.second);
        rep(j,to.size()) {
          int ext = to[j];
          double dist = getDist(vec[data.R].x-vec[data.R+d*1].x,vec[data.R].y-vec[data.R+d*1].y) + getDist(vec[data.L].x-vec[ext].x,vec[data.L].y-vec[ext].y);
          ii p = ii(ext,data.R+d*1);
          if( !mindist.count(p) || ( mindist.count(p) && LT(data.cost + dist,mindist[p]) ) ) {
            mindist[p] = data.cost + dist;
            Q.push((Data){ext,data.R+d*1,mindist[p]});
          } 
        }
      }
    }
  }  
  assert(false);
}

int main(){
  while( scanf("%d",&N), N ){
    rep(i,N) scanf("%lf %lf",&ps[i].x,&ps[i].y);//cin >> ps[i].x >> ps[i].y;
    compute();
  }
  return 0;
}