#include <bits/stdc++.h>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n==0) ? 1 : 10*TEN(n-1); }
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;

/*
struct E {
    int to, cap, dist, rev;
};
VV<E> g;
auto add_edge = [&](int from, int to, int cap, int dist) {
    g[from].push_back(E{to, cap, dist, int(g[to].size())});
    g[to].push_back(E{from, 0, -dist, int(g[from].size())-1});
};

auto res = min_cost_flow<int, int>(g, s, t, false);
res.max_flow(TEN(9));

// cap_flow : 最大流量
// flow : 最小費用
*/

template<class C, class D, class E>
struct MinCostFlow {
    static constexpr D INF = numeric_limits<D>::max();
    int n;
    VV<E> g;
    int s, t;
    C nc, cap_flow = 0;
    D nd, flow = 0;

    V<D> dual;
    V<int> pv, pe;

    MinCostFlow(VV<E> _g, int _s, int _t, bool neg) :
        n(int(_g.size())), g(_g), s(_s), t(_t) {
        assert(s != t);       
        dual = V<D>(n);
        pv = pe = V<int>(n);
        if (neg) {
            V<D> dist(g.size(), D(INF)); dist[s] = 0;
            for (int ph = 0; ph < n; ph++) {
                for (int i = 0; i < n; i++) {
                    for (auto e: g[i]) {
                        if (!e.cap || dist[i] == INF) continue;
                        dist[e.to] = min(dist[e.to], dist[i] + e.dist);
                    }
                }
            }
            for (int v = 0; v < int(g.size()); v++) {
                dual[v] += dist[v];
            }
        }
        dual_ref();
    }

    C single_flow(C c) {
        if (nd == INF) return nc;
        c = min(c, nc);
        for (int v = t; v != s; v = pv[v]) {
            E &e = g[pv[v]][pe[v]];
            e.cap -= c;
            g[v][e.rev].cap += c;
        }
        cap_flow += c;
        flow += nd * c;
        nc -= c;
        if (!nc) dual_ref();
        return c;
    }
    
    void max_flow(C c) {
        while (c) {
            C f = single_flow(c);
            if (!f) break;
            c -= f;
        }
    }

    void dual_ref() {
        V<D> dist(g.size(), D(INF)); dist[s] = 0;
        pv = pe = V<int>(n, -1);
        
        using P = pair<D, int>;
        priority_queue<P, vector<P>, greater<P>> que; que.push(P(D(0), s));
        V<bool> vis(n);
        while (!que.empty()) {
            int v = que.top().second; que.pop();
            if (v == t) break;
            if (vis[v]) continue;
            vis[v] = true;
            for (int i = 0; i < int(g[v].size()); i++) {
                E e = g[v][i];
                if (vis[e.to] || !e.cap) continue;
                D cost = dist[v] + e.dist + dual[v] - dual[e.to];
                if (dist[e.to] > cost) {
                    dist[e.to] = cost;
                    pv[e.to] = v; pe[e.to] = i;
                    que.push(P(dist[e.to], e.to));
                }
            }
        }
        if (dist[t] == INF) {
            nd = INF; nc = 0;
            return;
        }
        for (int v = 0; v < int(g.size()); v++) {
            if (!vis[v]) continue;
            dual[v] += dist[v] - dist[t];
        }
        nd = dual[t] - dual[s]; assert(0 <= nd);
        nc = numeric_limits<C>::max();
        for (int v = t; v != s; v = pv[v]) {
            nc = min(nc, g[pv[v]][pe[v]].cap);
        }        
    }
};

template<class C, class D, class E>
auto min_cost_flow(const VV<E> &g, int s, int t,bool neg = false) {
    return MinCostFlow<C, D, E>(g, s, t, neg);
}

using D = double;
const D PI = acos(D(-1)), EPS = 1e-10;

int sgn(D a) { return (abs(a) <= EPS) ? 0 : (a < 0 ? -1 : 1); }
int sgn(D a, D b) { return sgn(a-b); }

struct Pt2 {
    D x, y;
    Pt2() {}
    Pt2(D _x, D _y) : x(_x), y(_y) {}
    Pt2 operator-(const Pt2 &r) const { return Pt2(x-r.x, y-r.y); }
    bool operator<(const Pt2 &r) const { return 2*sgn(x, r.x)+sgn(y, r.y)<0; }
    bool operator==(const Pt2 &r) const { return sgn((*this-r).rabs()) == 0; }
    D abs() const { return sqrt(x*x + y*y); }
    D rabs() const { return max(std::abs(x), std::abs(y)); }
};
ostream& operator<<(ostream& os, const Pt2 &p) {
    return os << "(" << p.x << ", " << p.y << ")";
}
using P = Pt2;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(20) << fixed;

    D ans = 0;

    int n;
    cin >> n;
    D x, y;
    P p;
    cin >> x >> y; p = P(x, y);
    map<P, int> mp;
    for (int i = 0; i < n; i++) {
        P p, q;
        cin >> x >> y; p = P(x, y);
        mp[p]++;
        cin >> x >> y; q = P(x, y);
        mp[q]--;
        ans += (p-q).abs();
    }

    using Pi = pair<P, int>;
    V<Pi> up, dw;
    for (auto p: mp) {
        if (p.second == 0) continue;
        if (p.second > 0) {
            up.push_back(p);
        } else {
            dw.push_back(Pi(p.first, -p.second));
        }
    }

    int l = int(up.size()), r = int(dw.size());

    struct E {
        int to, cap;
        D dist;
        int rev;
    };
    int sv = l+r, tv = sv+1;
    VV<E> g(l+r+2);
    auto add_edge = [&](int from, int to, int cap, D dist) {
        g[from].push_back(E{to, cap, dist, int(g[to].size())});
        g[to].push_back(E{from, 0, -dist, int(g[from].size())-1});
    };

    for (int i = 0; i < l; i++) {
        add_edge(sv, i, up[i].second, 0);
    }
    for (int j = 0; j < r; j++) {
        add_edge(l+j, tv, dw[j].second, 0);
    }
    for (int i = 0; i < l; i++) {
        for (int j = 0; j < r; j++) {
            add_edge(i, l+j, TEN(9), (up[i].first - dw[j].first).abs());
        }
    }

    auto res = min_cost_flow<int, D>(g, sv, tv, false);
    res.max_flow(TEN(9));

    cout << ans + res.flow << endl;
    return 0;
}



Measured memory_bytes: 18676