#include<bits/stdc++.h>
#define MAX 256
#define inf 1<<29
#define linf 1e16
#define eps (1e-8)
#define mod 1000000007
#define pi acos(-1)
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define pd(a) printf("%.10f\n",(double)(a))
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)
#define equals(a,b) (fabs((a)-(b))<eps)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<int,double> pid;
typedef pair<double,int> pdi;
typedef vector<int> vi;
typedef vector<pii> vpi;
const int dx[8]={1,0,-1,0,1,1,-1,-1};
const int dy[8]={0,1,0,-1,1,-1,1,-1};

class Point{
public:
  double x,y;
  Point(double x=0,double y=0):x(x),y(y){}

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

  double abs(){ return sqrt(norm());}
  double norm(){ return (x*x+y*y);}
};
typedef Point Vector;
typedef vector<Point> Polygon;

class Segment{
public:
  Point p1,p2;
  Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}
};
typedef Segment Line;

double norm(Vector a){ return (a.x*a.x+a.y*a.y);}
double abs(Vector a){ return sqrt(norm(a));}
double dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}
double cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}

double getDistanceLP(Line l,Point p){
  return abs(cross(l.p2-l.p1,p-l.p1)/abs(l.p2-l.p1));
}

double getDistanceSP(Segment s,Point p){
  if(dot(s.p2-s.p1,p-s.p1)<0.0)return abs(p-s.p1);
  if(dot(s.p1-s.p2,p-s.p2)<0.0)return abs(p-s.p2);
  return getDistanceLP(s,p);
}

double getDistance(Segment s1,Segment s2){
  return min(min(getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2)),
             min(getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2)));
}

double getDistance(Polygon a,Polygon b){
  double res=inf;
  int n=a.size(),m=b.size();
  FOR(i,0,n){
    FOR(j,0,m){
      Segment s1(a[i],a[(i+1)%n]),s2(b[j],b[(j+1)%m]);
      res=min(res,getDistance(s1,s2));
    }
  }
  return res;
}

double getDistance(Polygon p,Line L){
  double res=inf;
  FOR(i,0,p.size())res=min(res,getDistanceLP(L,p[i]));
  return res;
}

int w,n,m,s,t;
vector<Polygon> pillars;
vector<pid> e[MAX];

void init(){
  pillars.clear();
  FOR(i,0,MAX)e[i].clear();
}

void add_edge(int from,int to,double cost){
  e[from].pb(mp(to,cost));
  e[to].pb(mp(from,cost));
}

double dijkstra(){
  double d[MAX];
  priority_queue<pdi,vector<pdi>,greater<pdi> > pq;
  fill(d,d+MAX,inf);
  d[s]=0;
  pq.push(mp(0,s));
  
  while(pq.size()){
    pdi u=pq.top();
    pq.pop();

    if(d[u.s]<u.f)continue;
    if(u.s==t)return d[u.s];

    FOR(i,0,e[u.s].size()){
      int next=e[u.s][i].f;
      double cost=d[u.s]+e[u.s][i].s;
      if(cost<d[next]){
        d[next]=cost;
        pq.push(mp(cost,next));
      }
    }
  }
  return w;
}

double solve(){
  FOR(i,0,n){
    FOR(j,i+1,n){
      add_edge(i,j,getDistance(pillars[i],pillars[j]));
    }
  }
  s=n;t=s+1;
  Line L(Point(0,0),Point(0,1)),R(Point(w,0),Point(w,1));
  FOR(i,0,n){
    add_edge(i,s,getDistance(pillars[i],L));
    add_edge(i,t,getDistance(pillars[i],R));
  }
  return dijkstra();
}

int main()
{
  while(1){
    cin>>w>>n;
    if(n+w==0)break;
    init();
    FOR(i,0,n){
      cin>>m;
      Polygon p;
      FOR(j,0,m){
        int x,y;
        cin>>x>>y;
        p.pb(Point(x,y));
      }
      pillars.pb(p);
    }
    pd(solve());
  }
  return 0;
}

Measured memory_bytes: 4192