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

Measured memory_bytes: 3248