#include using namespace std; template struct Point { typedef Point P; T x, y; explicit Point(T x=0, T y=0) : x(x), y(y) {} P operator+(P p) const { return P(x+p.x, y+p.y); } P operator-(P p) const { return P(x-p.x, y-p.y); } P operator*(T d) const { return P(x*d, y*d); } P operator/(T d) const { return P(x/d, y/d); } T dist2() const { return x*x + y*y; } double dist() const { return sqrt((double)dist2()); } T dot(P p) const { return x*p.x + y*p.y; } P unit() const { return *this/dist(); } // makes dist()=1 }; typedef Point P; int n; vector> segments; P startp, endp; vector values; P point(int i) { if(i == -1) return startp; if(i == n) return endp; return segments[i].first * (1 - values[i]) + segments[i].second * values[i]; } long double total_dist() { long double result = 0; for(int i = 0; i <= n; i++) result += (point(i) - point(i-1)).dist(); return result; } void step(long double multiplier) { long double result = total_dist(); vector grads(n); for(int i = 0; i < n; i++) { P cur = point(i), prev = point(i-1), next = point(i + 1); long double grad1 = (prev - cur).unit().dot(segments[i].second - segments[i].first); long double grad2 = (next - cur).unit().dot(segments[i].second - segments[i].first); grads[i] = grad1 + grad2; } for(int i = 0; i < n; i++) { values[i] += grads[i] * multiplier; values[i] = clamp(values[i], 0.0l, 1.0l); } } int main() { cin >> n; segments.resize(n); values.resize(n); cin >> startp.x >> startp.y; long double multiplier = 0; for(int i = 0; i < n; i++) { cin >> segments[i].first.x >> segments[i].first.y >> segments[i].second.x >> segments[i].second.y; multiplier += (segments[i].second - segments[i].first).dist(); } cin >> endp.x >> endp.y; for(int i = 0; i < 1000000; i++) step(0.1 / multiplier); cout << setprecision(15) << fixed << total_dist() << "\n"; }