#include <iostream>
#include <iomanip>
#include <complex>
#include <vector>
#include <algorithm>
#include <cmath>
#include <array>
using namespace std;
const double EPS = 1e-10;
const double INF = 1e12;
const double PI = acos(-1);
#define EQ(n,m) (abs((n)-(m)) < EPS)
#define X real()
#define Y imag()
typedef complex<double> P;
typedef vector<P> VP;
struct L : array<P, 2>{
    L(const P& a, const P& b){ at(0)=a; at(1)=b; }
    L(){}
};

namespace std{
    bool operator < (const P& a, const P& b){
        return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y;
    }
    bool operator == (const P& a, const P& b){
        return abs(a-b) < EPS;
    }
}

double dot(P a, P b){
    return (conj(a)*b).X;
}
double cross(P a, P b){
    return (conj(a)*b).Y;
}
int ccw(P a, P b, P c){
    b -= a;
    c -= a;
    if(cross(b,c) > EPS) return +1; //ccw
    if(cross(b,c) < -EPS) return -1; //cw
    if(dot(b,c) < -EPS) return +2; //c-a-b
    if(abs(c)-abs(b) > EPS) return -2; //a-b-c
    return 0; //a-c-b
}

bool intersectSS(const L& a, const L& b){
    return ( ccw(a[0],a[1],b[0]) *ccw(a[0],a[1],b[1]) <= 0 ) &&
        ( ccw(b[0],b[1],a[0]) *ccw(b[0],b[1],a[1]) <= 0 );
}

double shortest_path(P begin, P end, vector<L> &pl){
    pl.insert(pl.begin(), L(begin, begin));
    pl.push_back(L(end, end));
    int n = pl.size();
    vector<vector<double> > dp(n, vector<double>(2, INF));
    dp[0][0] = 0;
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            for(int s=0; s<2; s++){
                for(int t=0; t<2; t++){
                    L edge(pl[i][s], pl[j][t]);
                    bool ok = true;
                    for(int k=i+1; k<j; k++){
                        if(!intersectSS(edge, pl[k])){
                            ok = false;
                            break;
                        }
                    }
                    if(ok){
                        dp[j][t] = min(dp[j][t], dp[i][s] +abs(edge[1] -edge[0]));
                    }
                }
            }
        }
    }
    return dp[n-1][0];
}

int main(){
    while(1){
        int m,n;
        cin >> m >> n;
        if(m == 0) break;

        VP p(m), pin(n);
        P rot(cos(0.01), sin(0.01));
        for(int i=0; i<m; i++){
            double x,y;
            cin >> x >> y;
            p[i] = P(x, y)*rot;
        }
        for(int i=0; i<n; i++){
            double x,y;
            cin >> x >> y;
            pin[i] = P(x, y)*rot;
        }

        sort(pin.begin(), pin.end());
        vector<L> ls;
        for(int i=0; i<n; i++){
            ls.emplace_back(P(pin[i].X, -1e4), pin[i]);
            ls.emplace_back(pin[i], P(pin[i].X, 1e4));
        }

        vector<int> pass;
        for(int i=0; i<m-1; i++){
            L line(p[i], p[i+1]);
            if(EQ(line[0].X, line[1].X)) continue;
            bool goR = line[0].X < line[1].X;
            int j = goR? 0: ls.size()-1;
            while((goR && j<(int)ls.size()) || (!goR && j>=0)){
                if(intersectSS(ls[j], line)){
                    if(pass.empty() || pass.back() != j){
                        pass.push_back(j);
                    }else{
                        pass.pop_back();
                    }
                }
                if(goR) j++;
                else j--;
            }
        }

        double ans = 0;
        P start = p[0];
        vector<L> pl;
        for(int i=0; i<(int)pass.size(); i++){
            if(i!=(int)pass.size()-1 && EQ(ls[pass[i]][0].X, ls[pass[i+1]][0].X)){
                P end;
                if(ls[pass[i]][0].Y < ls[pass[i+1]][0].Y){
                    end = ls[pass[i]][1];
                }else{
                    end = ls[pass[i]][0];
                }
                ans += shortest_path(start, end, pl);
                start = end;
                pl = vector<L>();
            }else{
                pl.push_back(ls[pass[i]]);
            }
            if(i == (int)pass.size()-1){
                ans += shortest_path(start, p[m-1], pl);
            }
        }
        cout << fixed << setprecision(4);
        cout << ans << endl;
    }
    return 0;
}


Measured memory_bytes: 3300