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;
typedef vector<Point> Polygon;

ostream& operator << (ostream& os,const Point& a){ os << "(" << a.x << "," << a.y << ")"; }

ostream& operator << (ostream& os,const Segment& a){ os << "( " << a.p1 << " , " << a.p2 << " )"; }

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)); }

//rad は角度をラジアンで持たせること
Point rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }

// 度をラジアンに変換
double toRad(double agl){ return agl*M_PI/180.0; }

// a => prev, b => cur, c=> next
// prev から cur へ行って next へ行く際の角度を求める
double getArg(Point a,Point b,Point c){
  double arg1 = atan2(b.y-a.y,b.x-a.x);
  double arg2 = atan2(c.y-b.y,c.x-b.x);
  double arg = fabs( arg1 - arg2 );
  while( arg > M_PI ) arg -= 2.0 * M_PI;
  return fabs(arg);
}

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;
}

bool intersectLL(Line l, Line m) {
  return abs(cross(l.p2-l.p1, m.p2-m.p1)) > EPS || // non-parallel
         abs(cross(l.p2-l.p1, m.p1-l.p1)) < EPS;   // same line
}
bool intersectLS(Line l, Line s) {
  return cross(l.p2-l.p1, s.p1-l.p1)*       // s[0] is left of l
         cross(l.p2-l.p1, s.p2-l.p1) < EPS; // s[1] is right of l
}
bool intersectLP(Line l,Point p) {
  return abs(cross(l.p2-p, l.p1-p)) < EPS;
}
bool intersectSS(Line s, Line t) {
  return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 &&
         ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0;
}
bool intersectSP(Line s, Point p) {
  return abs(s.p1-p)+abs(s.p2-p)-abs(s.p2-s.p1) < EPS; // triangle inequality
}

Point projection(Line l,Point p) {
  double t = dot(p-l.p1, l.p1-l.p2) / norm(l.p1-l.p2);
  return l.p1 + (l.p1-l.p2)*t;
}
Point reflection(Line l,Point p) {
  return p + (projection(l, p) - p) * 2;
}
double distanceLP(Line l, Point p) {
  return abs(p - projection(l, p));
}
double distanceLL(Line l, Line m) {
  return intersectLL(l, m) ? 0 : distanceLP(l, m.p1);
}

double distanceLS(Line l, Line s) {
  if (intersectLS(l, s)) return 0;
  return min(distanceLP(l, s.p1), distanceLP(l, s.p2));
}
double distanceSP(Line s, Point p) {
  Point r = projection(s, p);
  if (intersectSP(s, r)) return abs(r - p);
  return min(abs(s.p1 - p), abs(s.p2 - p));
}

double distanceSS(Line s, Line t) {
  if (intersectSS(s, t)) return 0;
  return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)),
             min(distanceSP(t, s.p1), distanceSP(t, s.p2)));
}


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];
    //return m.p1;
  }
  if(abs(A) < EPS)assert(false);
  return m.p1 + (m.p2-m.p1)*(B/A);
}

/*
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) return m.p1; // same line
  if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!
  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];
//double mindist[MAX_V][MAX_V];
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());

  /*
  rep(i,vec.size()) {
    cout << i << "-th : " << vec[i] << endl;
  } cout << endl;
  */
  int V = vec.size();
  //cout << "V = " << V << endl;
  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();
    //cout << data.L << "," << data.R << " " << setprecision(10) << data.cost << endl;
    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);
          //cout << "(" << data.L << "," << data.R << ") to " << data.L+d*1 << "," << ext << "? ";
          if( !mindist.count(p) || ( mindist.count(p) && LT(data.cost + dist,mindist[p]) ) ) {
            //cout << "YES" << endl;
            mindist[p] = data.cost + dist;
            Q.push((Data){data.L+d*1,ext,mindist[p]});
          } //else cout << "NO" << endl;
        }
      }
    }

    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);
          //cout << "(" << data.L << "," << data.R << ") to " << ext << "," << data.R+d*1 << "? ";
          if( !mindist.count(p) || ( mindist.count(p) && LT(data.cost + dist,mindist[p]) ) ) {
            //cout << "YES" << endl;
            mindist[p] = data.cost + dist;
            Q.push((Data){ext,data.R+d*1,mindist[p]});
          } //else cout << "NO" << endl;
        }
      }
    }
  }  
  assert(false);
}

int main(){
  while( cin >> N, N ){
    rep(i,N) cin >> ps[i].x >> ps[i].y;
    compute();
  }
  return 0;
}