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>

using namespace std;

constexpr double EPS = 1e-8;

struct Point {
    double x, y;

    Point() {}
    Point(double x, double y) : x{x}, y{y} {}
 
    Point operator + (const Point& p) const
    {
        return Point(x + p.x, y + p.y);
    }
    
    Point operator - (const Point& p) const
    {
        return Point(x - p.x, y - p.y);
    }
    
    Point operator * (const double& k) const
    {
        return Point(x * k, y * k);
    }
    
    Point operator / (const double& k) const
    {
        return Point(x / k, y / k);
    }
    
    bool operator < (const Point& p) const
    {
        return x != p.x ? x < p.x : y < p.y;
    }
    
    bool operator == (const Point& p) const
    {
        return (x == p.x && y == p.y);
    }
};


double dot(const Point& a, const Point& b)
{
    return a.x * b.x + a.y * b.y;
}

double cross(const Point& a, const Point& b)
{
    return a.x * b.y - b.x * a.y;
}

double norm(const Point& p)
{
    return dot(p, p);
}

double abs(const Point& p)
{
    return sqrt(norm(p));
}

constexpr int COUNTER_CLOCKWISE = +1;
constexpr int CLOCKWISE         = -1;
constexpr int ONLINE_BACK       = +2;
constexpr int ONLINE_FRONT      = -2;
constexpr int ON_SEGMENT        = +0;
using Vector = Point;

int ccw(const Point& p0, const Point& p1, const Point& p2)
{
    Vector a = p1 - p0;
    Vector b = p2 - p0;
    if (cross(a, b) > EPS)  return COUNTER_CLOCKWISE;
    if (cross(a, b) < -EPS) return CLOCKWISE;
    if (dot(a, b) < -EPS)   return ONLINE_BACK;  
    if (norm(a) < norm(b))  return ONLINE_FRONT; 
    return ON_SEGMENT;                         
}

struct Segment {
    Point s, t;
    Segment() {}
    Segment(Point s, Point t) : s{s}, t{t} {}
};

Point proj(const Segment& s, const Point& p)
{
    Vector b = s.t - s.s;
    double t = dot(p - s.s, b) / norm(b);
    return s.s + b * t;
}

bool intersect_SP(const Segment& s, const Point& p)
{
    return (ccw(s.s, s.t, p) == 0);
}

bool intersect_SS(const Segment& a, const Segment& b)
{
    Point s[2] = {a.s, a.t}, t[2] = {b.s, b.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);
}

double distance_SP(const Segment& s, const Point& p)
{
    Point r = proj(s, p);
    if (intersect_SP(s, r)) return abs(r - p);
    return min(abs(s.s - p), abs(s.t - p));
}

double distance_SS(const Segment& a, const Segment& b)
{
    if (intersect_SS(a, b)) return 0;
    return min(min(distance_SP(a, b.s), distance_SP(a, b.t)),
               min(distance_SP(b, a.s), distance_SP(b, a.t)));
}

int main()
{
    int W, N;
    while (cin >> W >> N, W > 0) {
        vector<vector<Point>> v(N + 2);
        vector<int> size(N + 2);
        for (int i = 0; i < N; i++) {
            int M;
            cin >> M;
            vector<Point> ps(M);
            for (int j = 0; j < M; j++) {
                cin >> ps[j].x >> ps[j].y;
            }
            v[i] = ps;
            size[i] = ps.size();
        }

        constexpr double MAX = 10000;
        constexpr double INF = 1LL << 55;
        
        v[N] = {{0, 0}, {0, MAX}};
        v[N + 1] = {{(double)W, 0}, {(double)W, MAX}};
        size[N] = size[N + 1] = 2;
        
        vector<vector<double>> di(N + 2, vector<double>(N + 2, INF));
        for (int i = 0; i < N + 2; i++) {
            for (int j = 0; j < N + 2; j++) {
                if (i == j) continue;
                for (int k = 0; k < size[i]; k++) {
                    for (int l = 0; l < size[j]; l++) {
                        Segment s1{v[i][k], v[i][(k + 1) % size[i]]};                    
                        Segment s2{v[j][l], v[j][(l + 1) % size[j]]};

                        di[i][j] = min(di[i][j], distance_SS(s1, s2));
                    }
                }
            }
        }

        using P = pair<double, int>;               
        priority_queue<P, vector<P>, greater<P>> pq;
        pq.push(P{0, N});

        vector<double> d(N + 2, INF);
        d[N] = 0;
                
        while (!pq.empty()) {
            double c;
            int v;
            tie(c, v) = pq.top(); pq.pop();

            if (v == N + 1) {
                printf("%.15f\n", d[v]);
                break;
            }

            for (int i = 0; i < N + 2; i++) {
                if (v == i) continue;
                if (d[v] + di[v][i] < d[i]) {
                    d[i] = d[v] + di[v][i];
                    pq.push(P{d[i], i});
                }
            }
        }
    }        
    return 0;
}