#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <complex>

using namespace std;
typedef  long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf  1000000007
#define rep(i,n) for(int i=0;i<(int)(n);++i)


#define EPS 1e-12
#define rep(i,n) for(int i=0;i<(int)(n);++i)

typedef complex<double> C;

const double PI = 4*atan(1.0);

namespace std
{
    bool operator < (const C a, const C b) {
        return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();
    }
}

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

bool eq(double a,double b)
{
  return (-EPS<a-b&&a-b<EPS);
}

bool eq(C c1,C c2)
{
  return (eq(c1.real(),c2.real()) && eq(c1.imag(),c2.imag()));
}

//?????¶??????sqrt
double Sqrt(double x)
{
    if(x<0) return 0;
    else    return sqrt(x);
}

//??£??????
C normalize(C c)
{
    return c / abs(c);
}

//?§????(rad)
double getarg(C a,C b){
    return arg(b*conj(a));
}

//??????
double cross(const C a, const C b)
{
    return imag(conj(a)*b);
}
//??????
double dot(const C a, const C b)
{
    return real(conj(a)*b);
}

int ccw(C a, C b, C 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;   //b--a--c on line
}
//??´???????????????????????????(????????´??????True)
bool intersectLL(const L& l, const L& m)
{
    return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || abs(cross(l[1]-l[0], m[0]-l[0])) < EPS;
}
//??´?????¨?????????????????????(????????±??????????????¨??????)
bool intersectLS(const L& l, const L& s)
{
    return cross(l[1]-l[0], s[0]-l[0]) * cross(l[1]-l[0], s[1]-l[0]) < EPS;
}
//??´?????¨????????????(??±???)??????
bool intersectLP(const L& l, const C p)
{
    return abs(cross(l[1]-p, l[0]-p)) < 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;
}
//????????¨????????????(??±???)??????
bool intersectSP(const L& s, const C p)
{
    return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS;
}
//??´???????????????????????????
C crosspointLL(const L& l, const L& m)
{
    double A = cross(l[1] - l[0], m[1] - m[0]);
    double B = cross(l[1] - l[0], l[1] - m[0]);
    //????????´????????¨???
    if(abs(A) < EPS && abs(B) < EPS){
        return m[0];
    }
    return m[0] + B / A * (m[1] - m[0]);
}
//???p?????´???l???????°???±
C projection(const L& l, const C 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 distanceLP(const L &l, const C p)
{
    return abs(p - projection(l, p));
}
//??´?????¨??´???????????¢
double distanceLL(const L &l, const L &m)
{
    return intersectLL(l, m) ? 0 : distanceLP(l, m[0]);
}
//??´?????¨??????????????¢
double distanceLS(const L &l, const L &s)
{
    if (intersectLS(l, s)) return 0;
    return min(distanceLP(l, s[0]), distanceLP(l, s[1]));
}
//????????¨???????????¢
double 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[0] - p), abs(s[1] - p));
}
//????????¨??????????????¢
double distanceSS(const L &s, const L &t)
{
    if (intersectSS(s, t)) return 0;
    return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),min(distanceSP(t, s[0]), distanceSP(t, s[1])));
}


int main(){
	double w;
	int n;
	int m;
	while(cin >> w >> n&&w!=0){
		vector<vector<C> > p(n+2,vector<C>());
		p[0].push_back(C(0.0,-100000.0));
		p[0].push_back(C(0.0,100000.0));
		p[n+1].push_back(C(w,-100000.0));
		p[n+1].push_back(C(w,100000.0));
		rep(i,n){
			cin >> m;
			rep(j,m){
				double a,b;
				cin >> a >> b;
				p[i+1].push_back(C(a,b));
			}
		}
		vector<vector<double> > dst(n+2,vector<double>(n+2,100000000.0));
		for(int i=0;i<n+2;i++){
			for(int j=i+1;j<n+2;j++){
				for(int x=0;x<p[i].size();x++){
					L l1;
					if(x!=p[i].size()-1)l1=L(p[i][x],p[i][x+1]);
					else l1=L(p[i][x],p[i][0]);
					
					for(int y=0;y<p[j].size();y++){
						double d = distanceSP(l1,p[j][y]);		
						dst[i][j] = min(dst[i][j],d);
						dst[j][i] = dst[i][j];
					}
					for(int y=0;y<p[j].size();y++){
						L l2;
						if(y!=p[j].size()-1) l2=L(p[j][y],p[j][y+1]);
						else l2=L(p[j][y],p[j][0]);					
						double d = distanceSP(l2,p[i][x]);		
						dst[i][j] = min(dst[i][j],d);
						dst[j][i] = dst[i][j];
					}
				}
			}
		}
		double pinf = 1000000000;
		vector<double> d(n+2,pinf);
		d[0] = 0;
		priority_queue<pair<double,int>,vector<pair<double,int> >,greater<pair<double,int> > > pq;
		pq.push(MP(0,0));
		while(!pq.empty()){
			pair<double,int> s;
			s = pq.top();
			pq.pop();
			if(d[s.second]< s.first)continue;
			for(int i=0;i<n+2;i++){
				if(d[i] > d[s.second]+dst[s.second][i]){
					d[i] = d[s.second]+dst[s.second][i];
					pq.push(MP(d[i],i));
				}
			}
			
		}
		cout.precision(10);
		cout << d[n+1] << endl;
	}
	return 0;
}

Measured memory_bytes: 3808