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

Measured memory_bytes: 3220