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 <iostream>
#include <iomanip>
#include <vector>
#include <complex>
#include <queue>
using namespace std;

#define rep(i, n) for(int i = 0; i < (n); ++i)
#define pb  push_back

const double EPS = 1e-8;
typedef complex<double> Pt;

double dot(const Pt &a, const Pt &b) { return real(conj(a) * b); }
double cross(const Pt &a, const Pt &b) { return imag(conj(a) * b); }

struct L : public vector<Pt>
{
    L(){}
    L(const Pt &a, const Pt &b) {
        push_back(a); push_back(b);
    }
};

int ccw(Pt a, Pt b, Pt c)
{
    b -= a; c -= a;
    if(cross(b, c) > 0)     return 1;   // counter clockwise
    if(cross(b, c) < 0)     return -1;  // clockwise
    if(dot(b,c) < 0)        return 2;   // c-a-b on line
    if(norm(b) < norm(c))   return -2;  // a-b-c on line
    return 0;                           // a-c-b on line
}

bool intersectSP(const L &l, const Pt &p)
{
    return abs(l[0]-p) + abs(l[1]-p) - abs(l[1]-l[0]) < EPS;
}

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

Pt projection(const L &l, const Pt &p)
{
    double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);
    return l[0] + t*(l[0]-l[1]);
}

double distSP(const L& l, const Pt &p)
{
    const Pt r = projection(l, p);
    if(intersectSP(l, r)) return abs(r - p);
    return min(abs(l[0] - p), abs(l[1] - p));
}

double distSS(const L &s, const L &t)
{
    if(intersectSS(s, t)) return 0;
    return min(min(distSP(s, t[0]), distSP(s, t[1])),
               min(distSP(t, s[0]), distSP(t, s[1])));
}

int INF = 1001001001;
typedef pair<double, int> P;

int main()
{
    int W, N, M, x, y;
    
    while(cin >> W >> N && W)
    {
        vector<L> line;
        rep(i, N)
        {
            vector<Pt> pt;
            cin >> M;
            rep(j, M) cin >> x >> y, pt.pb(Pt(x, y));
            rep(j, M-1) line.pb(L(pt[j], pt[j+1]));
            line.pb(L(pt[0], pt[M-1]));
        }
        
        int src = line.size(), dst = src + 1, n = src + 2;
        line.pb(L(Pt(0, 0), Pt(0, 10000)));
        line.pb(L(Pt(W, 0), Pt(W, 10000)));
        
        vector<double> d(n, INF);
        d[src] = 0;
        priority_queue<P, vector<P>, greater<P> > que;
        que.push(P(0, src));
        
        while(true)
        {
            P p = que.top(); que.pop();
            int cur = p.second;
            if(cur == dst) {
                cout << setprecision(10) << d[dst] << endl; break;
            }
            if(d[cur] < p.first) continue;
            rep(i, n) if(i != n && d[i] > d[cur] + distSS(line[i], line[cur])) {
                d[i] = d[cur] + distSS(line[i], line[cur]);
                que.push(P(d[i], i));
            }
        }
    }
}