Problem: Wind Corridor is a covered passageway where strong wind is always blowing. It is a long corridor of width W, and there are several pillars in it. Each pillar is a right prism and its face is a polygon (not necessarily convex).

In this problem, we consider two-dimensional space where the positive x-axis points the east and the positive y-axis points the north. The passageway spans from the south to the north, and its length is infinity. Specifically, it covers the area 0 ≤ x ≤ W. The outside of the passageway is filled with walls. Each pillar is expressed as a polygon, and all the pillars are located within the corridor without conflicting or touching each other.

Wind blows from the south side of the corridor to the north. For each second, w unit volume of air can be flowed at most if the minimum width of the path of the wind is w. Note that the path may fork and merge, but never overlaps with pillars and walls.

Your task in this problem is to write a program that calculates the maximum amount of air that can be flowed through the corridor per second.



Input

The input consists of multiple datasets. Each dataset has the following format:

The first line of the input contains two integers W and N. W is the width of the corridor, and N is the number of pillars. W and N satisfy the following condition: 1 ≤ W ≤ 104 and 0 ≤ N ≤ 200.

Then, N specifications of each pillar follow. Each specification starts with a line that contains a single integer M, which is the number of the vertices of a polygon (3 ≤ M ≤ 40). The following M lines describe the shape of the polygon. The i-th line (1 ≤ i ≤ M) contains two integers xi and yi that denote the coordinate of the i-th vertex (0 < xi < W, 0 < yi < 104).

The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed.

Output

For each dataset, your program should print a line that contains the maximum amount of air flow per second, in unit volume. The output may contain arbitrary number of digits after the decimal point, but the absolute error must not exceed 10-6.

Example

Input

5 2
4
1 1
1 2
2 2
2 1
4
3 3
3 4
4 4
4 3
0 0


Output

3.41421356

Solution (in CPP):

Solution:
#include <bits/stdc++.h>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using ld = long double;
using C = complex<ld>;
using L = pair<C, C>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz:" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}


template <typename T>
constexpr T INF = numeric_limits<T>::max() / 10;
constexpr ld EPS = 1e-12;

ld dot(const C a, const C b)
{
    return real(conj(a) * b);
}

bool intersectSP(const L& s, const C p)
{
    return abs(s.first - p) + abs(s.second - p) - abs(s.second - s.first) < EPS;
}

C projection(const L& l, const C p)
{
    ld t = dot(p - l.first, l.first - l.second) / norm(l.first - l.second);
    return l.first + t * (l.first - l.second);
}

ld distanceSP(const L& s, const C p)
{
    const C r = projection(s, p);
    if (intersectSP(s, r))
        return abs(r - p);
    return min(abs(s.first - p), abs(s.first - p));
}

struct CostGraph {
    using T = ld;
    CostGraph(const int v) : V{v}
    {
        edge.resize(v);
        rev_edge.resize(v);
    }
    struct Edge {
        Edge(const int from, const int to, const T cost) : from{from}, to{to}, cost{cost} {}
        const int from;
        const int to;
        const T cost;
        bool operator<(const Edge& e) const
        {
            return cost != e.cost ? cost < e.cost : to < e.to;
        }
    };
    void addEdge(const int from, const int to, const T cost = 1)
    {
        edge[from].push_back(Edge{from, to, cost});
        rev_edge[to].push_back(Edge(to, from, cost));
    }
    vector<vector<Edge>> edge;
    vector<vector<Edge>> rev_edge;
    const int V;
};
void Dijkstra(const CostGraph& g, const int s, vector<CostGraph::T>& d)
{
    using T = CostGraph::T;
    assert(s < g.V);
    assert(d.size() == g.V);
    using P = pair<T, int>;
    priority_queue<P, vector<P>, greater<P>> q;
    for (int i = 0; i < g.V; i++) {
        d[i] = INF<T>;
    }
    d[s] = 0;
    q.push(make_pair(0, s));
    while (not q.empty()) {
        const P& p = q.top();
        const T cost = p.first;
        const int v = p.second;
        q.pop();
        if (d[v] < cost) {
            continue;
        }
        for (const auto& e : g.edge[v]) {
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                q.push(make_pair(d[e.to], e.to));
            }
        }
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while (true) {
        ld W;
        int N;
        cin >> W >> N;
        if (W == 0 and N == 0) {
            break;
        }
        vector<int> v(N);
        vector<vector<C>> vs(N);
        vector<vector<L>> es(N);
        for (int i = 0; i < N; i++) {
            cin >> v[i];
            for (int j = 0; j < v[i]; j++) {
                ld x, y;
                cin >> x >> y;
                vs[i].push_back(C{x, y});
            }
            for (int j = 0; j < v[i]; j++) {
                es[i].push_back(make_pair(vs[i][j], vs[i][(j + 1) % v[i]]));
            }
        }

        const int s = N;
        const int t = N + 1;
        const L leftwall = make_pair(C{0, 0}, C{0, 10000});
        const L rightwall = make_pair(C{W, 0}, C{W, 10000});
        CostGraph g(N + 2);
        for (int i = 0; i < N; i++) {
            ld mini = W;
            for (int j = 0; j < v[i]; j++) {
                mini = min(mini, distanceSP(leftwall, vs[i][j]));
            }
            g.addEdge(s, i, mini);
        }
        for (int i = 0; i < N; i++) {
            ld mini = W;
            for (int j = 0; j < v[i]; j++) {
                mini = min(mini, distanceSP(rightwall, vs[i][j]));
            }
            g.addEdge(i, t, mini);
        }

        for (int i = 0; i < N; i++) {
            for (int j = i + 1; j < N; j++) {
                ld mini = 20000;
                for (int k = 0; k < v[i]; k++) {
                    for (int l = 0; l < v[j]; l++) {
                        mini = min(mini, distanceSP(es[j][l], vs[i][k]));
                    }
                }
                for (int k = 0; k < v[i]; k++) {
                    for (int l = 0; l < v[j]; l++) {
                        mini = min(mini, distanceSP(es[i][k], vs[j][l]));
                    }
                }
                g.addEdge(i, j, mini);
                g.addEdge(j, i, mini);
            }
        }
        g.addEdge(s, t, W);
        vector<ld> dist(N + 2);
        Dijkstra(g, s, dist);
        cout << fixed << setprecision(15) << dist[t] << endl;
    }
    return 0;
}