#include <iostream>
#include <vector>
#include <complex>
#include <algorithm>
#include <queue>
#include <iomanip>
using namespace std;

#define EPS 1e-8

typedef complex<double> P;
typedef vector<P> G;

namespace std {
  bool operator < (const P& a, const P& b) {
    return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
  }
}
//àÏ
double dot(const P& a, const P& b) {
  return real(conj(a)*b);
}
//OÏ
double cross(const P& a, const P& b) {
  return imag(conj(a)*b);
}

//üª
struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a); push_back(b);
  }
};
//_Ìisûü
int ccw(P a, P b, P 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;
}

//üªÌ£
P projection(const L &l, const P &p) {
  double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);
  return l[0] + t*(l[0]-l[1]);
}
P reflection(const L &l, const P &p) {
  return p + 2.0 * (projection(l, p) - p);
}

bool intersectSP(const L &s, const P &p) {
  return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality
}

double distanceLP(const L &l, const P &p) {
  return abs(p - projection(l, p));
}

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

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 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])));
}

double dist[205][205];

int main()
{
	int W,N,V;
	while(cin >> W >> N, W||N)
	{
		vector<G> polys;

		for(int i=0; i<N; i++)
		{
			cin >> V;
			G poly;
			while(V--)
			{
				int x,y;
				cin >> x >> y;
				poly.push_back(P(x,y));
			}
			polys.push_back(poly);
		}
	
		for(int i=0; i<N-1; i++)
		{
			dist[i][i]=0;
			for(int j=i+1; j<N; j++)
			{
				double pd = 1e20;
				for(int k=0; k<polys[i].size(); k++)
				for(int m=0; m<polys[j].size(); m++)
				{
					L L1(polys[i][k],polys[i][(k+1)%polys[i].size()]);
					L L2(polys[j][m],polys[j][(m+1)%polys[j].size()]);

					pd = min(pd, distanceSS(L1,L2));
				}
				dist[i][j]=dist[j][i]=pd;
			}
		}

		double lx[] = {0,W};
		for(int i=0; i<2; i++)
		{
			dist[N+i][N+i]=0;

			L L1(P(lx[i],1e10),P(lx[i],-1e10));
			for(int j=0; j<N; j++)
			{
				double pd=1e10;
				for(int k=0; k<polys[j].size(); k++)
				{
					L L2(polys[j][k],polys[j][(k+1)%polys[j].size()]);
					pd = min(pd, distanceLS(L1,L2));
				}
				dist[N+i][j]=dist[j][N+i]=pd;
			}
		}
		dist[N][N+1]=dist[N+1][N]=W;

		priority_queue<pair<double,int>, vector<pair<double,int> >, greater<pair<double,int> > > q;
		q.push(make_pair(0,N));

		bool visit[205]={0};
		while(!q.empty())
		{
			pair<double,int> t=q.top(); q.pop();

			//cout << t.second << endl;
			
			if(visit[t.second]) continue;
			visit[t.second] = 1;

			if(t.second==N+1)
			{
				cout << setiosflags(ios::fixed) << setprecision(10) << t.first << endl;
				break;
			}

			for(int i=0; i<N+2; i++)
			{
				if(visit[i]) continue;
				q.push(make_pair(t.first+dist[t.second][i],i));
			}
		}

		/*
		for(int i=0; i<N+2; i++)
		{
			for(int j=0; j<N+2; j++)
			{
				cout.width(6);
				cout << dist[i][j] << " ";
			}
			cout << endl;
		}
		*/	
	}
}

Measured memory_bytes: 1720