#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define all(h) (h).begin(), (h).end()
using namespace std;
typedef double D;
typedef pair<D,int> pdi;
typedef pair<int,int> pii;
typedef pair<D,pii> pdii;

D dist(pdi a, pdi b){
  return sqrt((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second));
}

int main(){
  int n;
  while(cin >> n, n){
    vector<int> x(n),y(n);
    vector<int> h;
    rep(i,n){
      cin >> x[i] >> y[i];
      h.push_back(y[i]);
    }
    sort(all(h));
    h.erase(unique(all(h)),h.end());
    
    vector<pdi> p;
    rep(i,n-1){
      p.push_back(pdi(x[i],y[i]));
      if(y[i] < y[i+1]){
	rep(j,h.size()){
	  if(h[j]<=y[i] || y[i+1]<=h[j])continue;
	  D X = x[i] + (D)(x[i+1]-x[i])*(h[j]-y[i])/(y[i+1]-y[i]);
	  p.push_back(pdi(X,h[j]));
	}
      }else if(y[i] > y[i+1]){
	rrep(j,h.size()){
	  if(h[j]<=y[i+1] || y[i]<=h[j])continue;
	  D X = x[i+1] - (D)(x[i+1]-x[i])*(h[j]-y[i+1])/(y[i]-y[i+1]);
	  p.push_back(pdi(X,h[j]));
	}
      }
    }
    p.push_back(pdi(x[n-1],y[n-1]));

    map<pii,D> d;
    d[pii(0,(int)p.size()-1)] = 0;
    priority_queue< pdii, vector<pdii>, greater<pdii> > q;
    q.push(pdii(0,pii(0,p.size()-1)));
    
    while(q.size()){
      pdii s = q.top(); q.pop();
      D dis = s.first;
      int a = s.second.first, b = s.second.second;
      if(a==b){
	cout << fixed << setprecision(3) << dis << endl;
      	break;
      }
      if(d[pii(a,b)] < dis)continue;
      
      for(int aa=a-1;aa<=a+1;aa++){
	if(aa<0 || aa>=(int)p.size())continue;
	for(int bb=b-1;bb<=b+1;bb++){
	  if(bb<0 || bb>=(int)p.size())continue;
	  if(p[aa].second != p[bb].second)continue;
	  pii np = pii(aa,bb);
	  D ndis = dist(p[a],p[aa]) + dist(p[b],p[bb]) + dis;

	  if(d.count(np)==0 || d[np] > ndis){
	    d[np] = ndis;
	    q.push(pdii(ndis,np));
	  }
	}
      }
    }
  }
}