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 REP(i,n) for(int i=0; i<(int)(n); ++i)

using namespace std;

const double EPS = 1e-8;

typedef complex<double> Point;
typedef Point P;
typedef vector<Point> Line;
typedef Line L;
typedef vector<Point> Pol;
double dot(P a, P b){return real(conj(a) * b);}
double cross(P a, P b){return imag(conj(a) * b);}
P vec(L l){return l[1] - l[0];}
int sign(double x){ return x > EPS ? 1 : x < -EPS ? -1 : 0; }
int ccw(P a, P b, P c) {
    b -= a; c -= a;
    return sign(cross(b,c)) ? : dot(b,c) < 0 ? 2 : norm(b) < norm(c) ? -2 : 0;
}
Point next(Pol p, int k) {
    return p[ (k+1) % p.size() ];
}
bool iSS(L s, L t) {
    auto ok = [](L a, L b){return ccw(a[0],a[1],b[0]) * ccw(a[0],a[1],b[1]) <= 0;};
    return ok(s,t) && ok(t,s);
}
double dLP(L l, P p){return abs(cross(vec(l), p - l[0])) / abs(vec(l));}
double dSP(L s, P p){
    if(sign(dot(vec(s), p - s[0])) <= 0) return abs(p - s[0]);
    if(sign(dot(-vec(s), p - s[1])) <= 0) return abs(p - s[1]);
    return dLP(s, p);
}
double dSS(L s, L t){return iSS(s,t) ? 0.0 : min({dSP(s, t[0]), dSP(s, t[1]), dSP(t, s[0]), dSP(t, s[1])});}
double dist(Pol p, Pol q) {
    double res = 1e18;
    REP(i, p.size()){
        Line l = {p[i], next(p, i)};
        REP(j, q.size()) {
            Line m = {q[j], next(q, j)};
            res = min(res, dSS(l, m));
        }
    }
    return res;
}
double distL(Line l, Pol q) {
    double res = 1e18;
    REP(j, q.size()) {
        Line m = {q[j], next(q, j)};
        res = min(res, dSS(l, m));
    }
    return res;
}
int main(){
    double W;
    int N;
    while(cin >> W >> N && W > 0) {
        vector<Pol> pols(N);
        REP(i, N) {
            int K;
            cin >> K;
            Pol p(K);
            REP(j, K) {
                double x, y;
                cin >> x >> y;
                p[j] = {x, y};
            }
            pols[i] = p;
        }
        Line left = {{0, 0}, {0, 1e4}};
        Line right = {{W, 0}, {W, 1e4}};

        double d[203][203] = {};
        REP(i, N+2) REP(j, N+2) d[i][j] = (i == j ? 0 : 1e18);

        d[0][1] = d[1][0] = W;

        REP(i, N) REP(j, N) d[i+2][j+2] = dist(pols[i], pols[j]);
        REP(i, N) d[0][i+2] = d[i+2][0] = distL(left, pols[i]);
        REP(i, N) d[1][i+2] = d[i+2][1] = distL(right, pols[i]);
        // REP(i, N) REP(j, pols[i].size()) d[0][i+2] = d[i+2][0] = min(d[0][i+2], pols[i][j].real());
        // REP(i, N) REP(j, pols[i].size()) d[1][i+2] = d[i+2][1] = min(d[0][i+2], W - pols[i][j].real());

        REP(k, N+2) REP(i, N+2) REP(j, N+2) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        // REP(i, N+2) REP(j, N+2) printf("d[%d][%d] = %f\n", i, j, d[i][j]);

        printf("%.12f\n", d[0][1]);
    }
    return 0;
}