import std.stdio;
import std.ascii;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

void main() {
    int N;
    int[] X, Y;
    bool input() {
        scanf("%d\n", &N);
        if (N == 0) return false;
        X = new int[N];
        Y = new int[N];
        foreach (i; 0 .. N) {
            scanf("%d %d\n", &X[i], &Y[i]);
        }
        return true;
    }

    void solve() {
        int[] ys = Y.dup;
        ys = ys.sort.uniq.array;

        double[] nX, nY;
        foreach (i; 0 .. N - 1) {
            nX ~= X[i];
            nY ~= Y[i];
            int min = Y[i] < Y[i + 1] ? i : i + 1;
            int max = Y[i] > Y[i + 1] ? i : i + 1;
            auto range = Y[i] < Y[i + 1] ? ys.dup : ys.dup.reverse;
            foreach (y; range) {
                if (Y[min] < y && y < Y[max]) {
                    double d = y - Y[i];
                    if (d != 0) {
                        double dx = X[i + 1] - X[i];
                        double dy = Y[i + 1] - Y[i];
                        nX ~= X[i] + dx * (d / dy);
                        nY ~= y;
                    }
                }
            }
        }
        nX ~= X.back;
        nY ~= Y.back;

        N = cast(int)nX.length;

        double dist(int a, int b) {
            double dy = nY[a] - nY[b],
                   dx = nX[a] - nX[b];
            return sqrt(dy * dy + dx * dx);
        }

        struct S {
            int a, b;
            double cost;
        }
        BinaryHeap!(Array!S, "a.cost > b.cost") PQ;
        PQ.insert(S(0, N - 1, 0));
        auto D = new double[][](N, N);
        foreach (i; 0 .. N) D[i][] = 1e9;
        D[0][N - 1] = 0;
        while (!PQ.empty) {
            S c = PQ.front; PQ.removeFront;
            for (int da = -1; da <= 1; da++) {
                for (int db = -1; db <= 1; db++) {
                    int na = c.a + da,
                        nb = c.b + db;
                    if (na < 0 || nb >= N || na > nb) continue;
                    if (nY[na] != nY[nb]) continue;
                    auto next = S(na, nb, c.cost + dist(c.a, na) + dist(c.b, nb));
                    if (D[na][nb] > next.cost) {
                        //writeln("next: ", next);
                        D[na][nb] = next.cost;
                        PQ.insert(next);
                    }
                }
            }
        }

        //foreach (L; D) L.writeln; writeln;

        double Ans = 1e9;
        foreach (i; 0 .. N) {
            Ans = min(Ans, D[i][i]);
        }
        writefln("%.10f", Ans);
    }
    while (input) solve;
}