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 <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <sstream>
#include <vector>
using namespace std;

#define REP2(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(auto i = (c).begin(); i != (c).end(); ++i)
#define BIT(n, m) (((n) >> (m)) & 1)

typedef long long ll;
typedef pair<int, int> PI;

const ll inf = 1e15;
const ll mod = 1000 * 1000 * 1000 + 7;
const double eps = 1e-9;

template <typename S, typename T> ostream &operator<<(ostream &out, const pair<S, T> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  REP(i, v.size()){
    if (i > 0) out << ", ";
    out << v[i];
  }
  out << "]";
  return out;
}

typedef complex<double> P;
typedef vector<P> Poly;
typedef pair<P, P> L;

double cross(const P &a, const P &b) { return imag(conj(a)*b); }
double dot(const P &a, const P &b) { return real(conj(a)*b); }
int ccw(P a, P b, P c) {
	double d = cross(b -= a, c -= a);
	return d>eps ? 1 : d<-eps ? -1 : dot(b,c)<-eps ? 2 : norm(b)+eps<norm(c) ? -2 : 0;
}

#define DI(l) ((l).second-(l).first)
#define CCW(l, a) ccw((l).first, (l).second, (a))

bool intersectSS(const L &s, const L &t) { return CCW(s,t.first)*CCW(s,t.second)<=0 && CCW(t,s.first)*CCW(t,s.second)<=0; }

double distanceSP(const L &l, const P &p) {
	P d = DI(l);
	if (dot(p-l.second, d) >= 0) return abs(l.second-p);
	if (dot(p-l.first, d) <= 0) return abs(l.first-p);
	return abs(cross(d, p-l.first) / abs(d));
}

double distanceSS(const L &s, const L &t) {
	if (intersectSS(s, t)) return 0;
	return min(min(distanceSP(s, t.first), distanceSP(s, t.second)), min(distanceSP(t, s.first), distanceSP(t, s.second)));
}

double distance(const Poly &p, const Poly &q){
  double res = inf;
  REP(i, p.size()) REP(j, q.size()){
    L lp(p[i], p[(i + 1) % p.size()]);
    L lq(q[j], q[(j + 1) % q.size()]);
    res = min(res, distanceSS(lp, lq));
    res = min(res, distanceSP(lp, q[j]));
    res = min(res, distanceSP(lq, p[i]));
  }
  return res;
}

int main(){
  int W, N;
  while (cin >> W >> N && W){
    vector<Poly> ps(N);

    REP(i, N){
      int M;
      double x, y;
      cin >> M;
      REP(j, M){
        cin >> x >> y;
        ps[i].emplace_back(x, y);
      }
    }
    
    vector<vector<double> > D(N + 2, vector<double>(N + 2, 0.0));
    REP(i, N) REP(j, N) if (i != j){
      D[i][j] = distance(ps[i], ps[j]);
    }
    REP(i, N) {
      D[i][N] = D[i][N + 1] = 1e9;
      REP(j, ps[i].size()){
        D[i][N] = min(D[i][N], ps[i][j].real());
        D[i][N + 1] = min(D[i][N + 1], W - ps[i][j].real());
      }
      D[N][i] = D[i][N];
      D[N + 1][i] = D[N + 1][i];
    }
    D[N][N + 1] = D[N + 1][N] = W;

    REP(k, D.size()) REP(i, D.size()) REP(j, D.size()) {
      D[i][j] = min(D[i][j], D[i][k] + D[k][j]);
    }
    cout << fixed << setprecision(20) << D[N][N + 1] << endl;
  }
  return 0;
}