#include<bits/stdc++.h>
using namespace std;
#define EPS (1e-10)
#define equals(a, b) (fabs((a)-(b)) < EPS )
 
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 a){return Point (x*a,y*a);}
  Point operator / (double a){return Point (x/a,y/a);}
};
 
double norm( Point a ){ return a.x*a.x + a.y*a.y;}
double abs( Point a ){ return sqrt(norm(a)); }
double dot( Point a, Point b ){return a.x*b.x+a.y*b.y;}
double cross(Point a,Point b){return a.x*b.y-a.y*b.x;}
 
class Segment{
public:
  Point p1,p2;
  Segment(Point a,Point b):p1(a),p2(b){}
};
 
int ccw(Point p0,Point p1,Point p2){
  Point a=p1-p0;
  Point b=p2-p0;
  if( cross(a, b) > EPS ) return 1;
  if( cross(a, b) < -EPS ) return -1;
  if( dot(a,b) < -EPS ) return 2;
  if( norm(a) < norm(b) ) return -2;
  return 0;
}
 
bool isIntersect(Point p1,Point p2,Point p3,Point p4){
  return (ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0 &&
      ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0);
}
 
bool isIntersect(Segment s1,Segment s2){
  return isIntersect(s1.p1, s1.p2, s2.p1, s2.p2);
}
 
double getDistanceLP(Segment s,Point p){
  return abs(cross(s.p2-s.p1,p-s.p1)/abs(s.p2-s.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){
  if(isIntersect(s1,s2))return 0.0;
  return min(min(getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2)),
         min(getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2)));
}
 
int W,N;
vector<Segment> t[300];
double d[300];
double cost[300][300];
 
double calc(int a,int b){
  double res=1e9;
  for(int i=0;i<(int)t[a].size();i++)
    for(int j=0;j<(int)t[b].size();j++)
      res=min(res,getDistance(t[a][i],t[b][j]));
  return res;
}
 
typedef pair<double,int> P;
 
void solve(){
 
  for(int i=0;i<N;i++)
    for(int j=0;j<N;j++)
      cost[i][j]=calc(i,j);
     
  Segment si(Point(0,0),Point(0,1e8));
  Segment ti(Point(W,0),Point(W,1e8));
 
  priority_queue< P , vector<P> ,greater<P> > Q;
 
  for(int i=0;i<N;i++){
    d[i]=1e9;
    int size=t[i].size();
    for(int j=0;j<size;j++)
      d[i]=min(d[i],getDistance(t[i][j],si));
    Q.push(P(d[i],i));
  }
 
  while(!Q.empty()){
    P p=Q.top();Q.pop();
    if( p.first > EPS+d[p.second])continue;
    for(int to=0;to<N;to++){
      if( p.first+cost[p.second][to] < d[to]-EPS){
    d[to]=p.first+cost[p.second][to];
    Q.push(P(d[to],to));
      }
    }
  }
 
  double ans=W;
  for(int i=0;i<N;i++){
    double dist=1e9;
    int size=t[i].size();
    for(int j=0;j<size;j++){
      dist=min(dist,getDistance(t[i][j],ti));
    }
    ans=min(ans,d[i]+dist);
  }
  printf("%.8f\n",ans);
}
 
void init(){
  for(int i=0;i<300;i++){
    t[i].clear();
  }
}
 
int main(){
  while(1){
    cin>>W>>N;
    if(W==0&&N==0)break;
    init();
    for(int i=0;i<N;i++){
      int a;
      cin>>a;
 
      Point A,B,C;
      cin>>A.x>>A.y;
      C=A;
      for(int j=0;j<a-1;j++){
    B=A;
    cin>>A.x>>A.y;
    t[i].push_back((Segment){A,B});
      }
      t[i].push_back((Segment){C,A});
    }
    solve();
  }
  return 0;
}

Measured memory_bytes: 2144