#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#define owo(i,a, b) for(int i=(a);i<(b); ++i)
#define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout<<"ayaya~"<<endl
 
using namespace std;
using ll = long long;
using ld = long double;
ll MOD = 998244353;
const ll root = 3;
ll binpow(ll a,ll b){ll res=1;while(b){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b>>=1;}return res;}
ll modInv(ll a){return binpow(a, MOD-2);}
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0xc0c0c0c0c0c0c0c0;
const int mxN = 100001;
int n, m;
int tc = 1;
vector<int> adj[mxN];
int fd[mxN];
bool mark[mxN];
int getlow[mxN];
int par[mxN];
bool vis[mxN];
int bck[mxN];
vector<ttgl> res;
int fnum = -1;
void dfs(int u) {
    vis[u] = 1;
    for(int v: adj[u]) {
        if(par[u]==v)continue;
        if(vis[v]==0) {
            par[v] = u;
            dfs(v);
        }else if(bck[v]!=u){
            bck[u] = v;
        }
    }
}
void dfsans(int u, int p = -1) {
    vis[u] = 1;
    if(mark[u]) {
        vector<int> curr;
        vector<int> leaves;
        for(int v: adj[u]) {
            if(!vis[v]) {
                //cout<<u<<" "<<v<<"\n";
                dfsans(v, u);
                if(!mark[v]) {
                    if(getlow[v]!=-1) {
                        curr.senpai(getlow[v]);
                    }else leaves.senpai(v);
                }
            }
        }
        /*cout<<u<<" dfs-----------------\n";
        for(int k: curr) {
            cout<<k<<" ";
        }
        cout<<"\n";*/
        while(curr.size() > 1) {
            int f = curr.back();
            curr.pop_back();
            res.senpai({f, curr.back()});
            curr.pop_back();
        }
        if(curr.size() > 0) {
            if(leaves.size() > 0) {
                res.senpai({curr.back(), leaves.back()});
                leaves.pop_back();
            }else res.senpai({curr.back(), u});
        }
        while(leaves.size() > 1) {
            int f = leaves.back();
            leaves.pop_back();
            res.senpai({f, leaves.back()});
            leaves.pop_back();
        }
        getlow[u] = -1;
    }else {
        vector<int> curr;
        vector<int> leaves;
        for(int v: adj[u]) {
            if(!vis[v]) {
                //cout<<u<<" "<<v<<"\n";
                dfsans(v, u);
                if(!mark[v]) {
                    if(getlow[v]!=-1) {
                        curr.senpai(getlow[v]);
                    }else leaves.senpai(v);
                }
            }
        }
        /*cout<<u<<" dfs-----------------\n";
        for(int k: curr) {
            cout<<k<<" ";
        }
        cout<<"\n";*/
        while(curr.size() > 1) {
            int f = curr.back();
            curr.pop_back();
            res.senpai({f, curr.back()});
            curr.pop_back();
        }
        if(curr.size() > 0) {
            getlow[u] = curr.back();
        }
        while(leaves.size() > 2) {
            int f = leaves.back();
            leaves.pop_back();
            res.senpai({f, leaves.back()});
            leaves.pop_back();
        }
        if(getlow[u]==-1 && leaves.size() > 0) {
            getlow[u] = leaves.back();
        }else if(getlow[u]!=-1 && leaves.size() > 1) {
            int f = leaves.back();
            leaves.pop_back();
            res.senpai({f, leaves.back()});
            leaves.pop_back();
        }
    }
}
void solve() {
    res.clear();
    owo(i, 0, n) {
        vis[i] = 0;
        par[i] = bck[i] = getlow[i] = -1;
        mark[i] = 0;
        fd[i] = i;
        adj[i].clear();
    }
    owo(i, 0, m) {
        int a, b, c;
        cin>>a>>b;
        if(fnum==-1)fnum = a;
        owo(j, 1, a) {
            cin>>c;
            adj[b-1].senpai(c-1);
            adj[c-1].senpai(b-1);
            swap(b, c);
        }
    }
    dfs(0);
    owo(i, 0, n) {
        if(bck[i]!=-1) {
            mark[i] = 1;
            while(par[i]!=bck[i]) {
                mark[par[i]] = 1;
                fd[par[i]] = i;
                par[i] = par[par[i]];
            }
            fd[par[i]] = i;
        }
    }
    owo(i, 0 ,n) {
        vis[i] = 0;
    }
    mark[0] = 1;
    dfsans(0);
        cout<<res.size()<<"\n";
        for(auto p: res) {
            cout<<p.first+1<<" "<<p.second+1<<"\n";
        }
}
int main() {
    //freopen("file.in", "r", stdin);
    //freopen("file.out", "w", stdout);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    cin.tie(0)->sync_with_stdio(0);
    while(tc) {
        cin>>n>>m;
        if(n==0 && m==0)exit(0);
        solve();
        tc++;
    }
    return 0;
}