#include <bits/stdc++.h>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using ld = long double;
using C = complex<ld>;
using L = pair<C, C>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz:" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}


template <typename T>
constexpr T INF = numeric_limits<T>::max() / 10;
constexpr ld EPS = 1e-12;

ld dot(const C a, const C b)
{
    return real(conj(a) * b);
}

bool intersectSP(const L& s, const C p)
{
    return abs(s.first - p) + abs(s.second - p) - abs(s.second - s.first) < EPS;
}

C projection(const L& l, const C p)
{
    ld t = dot(p - l.first, l.first - l.second) / norm(l.first - l.second);
    return l.first + t * (l.first - l.second);
}

ld 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.first - p), abs(s.first - p));
}

struct CostGraph {
    using T = ld;
    CostGraph(const int v) : V{v}
    {
        edge.resize(v);
        rev_edge.resize(v);
    }
    struct Edge {
        Edge(const int from, const int to, const T cost) : from{from}, to{to}, cost{cost} {}
        const int from;
        const int to;
        const T cost;
        bool operator<(const Edge& e) const
        {
            return cost != e.cost ? cost < e.cost : to < e.to;
        }
    };
    void addEdge(const int from, const int to, const T cost = 1)
    {
        edge[from].push_back(Edge{from, to, cost});
        rev_edge[to].push_back(Edge(to, from, cost));
    }
    vector<vector<Edge>> edge;
    vector<vector<Edge>> rev_edge;
    const int V;
};
void Dijkstra(const CostGraph& g, const int s, vector<CostGraph::T>& d)
{
    using T = CostGraph::T;
    assert(s < g.V);
    assert(d.size() == g.V);
    using P = pair<T, int>;
    priority_queue<P, vector<P>, greater<P>> q;
    for (int i = 0; i < g.V; i++) {
        d[i] = INF<T>;
    }
    d[s] = 0;
    q.push(make_pair(0, s));
    while (not q.empty()) {
        const P& p = q.top();
        const T cost = p.first;
        const int v = p.second;
        q.pop();
        if (d[v] < cost) {
            continue;
        }
        for (const auto& e : g.edge[v]) {
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                q.push(make_pair(d[e.to], e.to));
            }
        }
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while (true) {
        ld W;
        int N;
        cin >> W >> N;
        if (W == 0 and N == 0) {
            break;
        }
        vector<int> v(N);
        vector<vector<C>> vs(N);
        vector<vector<L>> es(N);
        for (int i = 0; i < N; i++) {
            cin >> v[i];
            for (int j = 0; j < v[i]; j++) {
                ld x, y;
                cin >> x >> y;
                vs[i].push_back(C{x, y});
            }
            for (int j = 0; j < v[i]; j++) {
                es[i].push_back(make_pair(vs[i][j], vs[i][(j + 1) % v[i]]));
            }
        }

        const int s = N;
        const int t = N + 1;
        const L leftwall = make_pair(C{0, 0}, C{0, 10000});
        const L rightwall = make_pair(C{W, 0}, C{W, 10000});
        CostGraph g(N + 2);
        for (int i = 0; i < N; i++) {
            ld mini = W;
            for (int j = 0; j < v[i]; j++) {
                mini = min(mini, distanceSP(leftwall, vs[i][j]));
            }
            g.addEdge(s, i, mini);
        }
        for (int i = 0; i < N; i++) {
            ld mini = W;
            for (int j = 0; j < v[i]; j++) {
                mini = min(mini, distanceSP(rightwall, vs[i][j]));
            }
            g.addEdge(i, t, mini);
        }

        for (int i = 0; i < N; i++) {
            for (int j = i + 1; j < N; j++) {
                ld mini = 20000;
                for (int k = 0; k < v[i]; k++) {
                    for (int l = 0; l < v[j]; l++) {
                        mini = min(mini, distanceSP(es[j][l], vs[i][k]));
                    }
                }
                for (int k = 0; k < v[i]; k++) {
                    for (int l = 0; l < v[j]; l++) {
                        mini = min(mini, distanceSP(es[i][k], vs[j][l]));
                    }
                }
                g.addEdge(i, j, mini);
                g.addEdge(j, i, mini);
            }
        }
        g.addEdge(s, t, W);
        vector<ld> dist(N + 2);
        Dijkstra(g, s, dist);
        cout << fixed << setprecision(15) << dist[t] << endl;
    }
    return 0;
}

Measured memory_bytes: 7380