#pragma GCC optimize(3)
#include<bits/stdc++.h>
using namespace std;
struct front_star{
    int to,next;
}e[500050];
struct grp{
    int fp,sp;
}ans[500050];
front_star eg[500050];
int n,m,cnt=1,t=0,tot=0,all=0,root;
int head[100005],dfn[100005],low[100005],brg[500005],hd[100005],vis[100005],sz[100005],dep[100005],upt[100005];
inline int read()
{
    int X=0,w=0; char ch=0;
    while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
    while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
inline void write(int x)
{
     if(x<0) putchar('-'),x=-x;
     if(x>9) write(x/10);
     putchar(x%10+'0');
}
inline void addedge(int u,int v)
{
    cnt++;
    e[cnt].to=v;
    e[cnt].next=head[u];
    head[u]=cnt;
}
inline void adde(int u,int v)
{
    tot++;
    eg[tot].to=v;
    eg[tot].next=hd[u];
    hd[u]=tot;
}
inline void tarjan(int x,int in_edge)
{
    t++;
    dfn[x]=low[x]=t;
    for(register int i=head[x];~i;i=e[i].next)
    {
        int y=e[i].to;
        if(!dfn[y])
        {
            tarjan(y,i);
            low[x]=min(low[x],low[y]);
            if(low[y]>dfn[x])
                brg[i]=brg[i^1]=1;
        }else if(i!=(in_edge^1))
            low[x]=min(low[x],dfn[y]);
    }
}
inline void dfs(int u,int fa)
{
    int spe=0;
    vis[u]=1;
    upt[u]=u;
    for(register int i=hd[u];~i;i=eg[i].next)
    {
        int v=eg[i].to;
        if(v==fa)
            continue;
        sz[u]++;
        if(!vis[v])
        {
            dep[v]=dep[u]+1;
            dfs(v,u);
            if(v==upt[v])
                spe=v;
        }
    }
    int pt=0,num=sz[u];
    if(u==root)
    {
        if(num%2==0)
        {
            for(register int i=hd[u];~i;i=eg[i].next)
            {
                int v=eg[i].to;
                if(v==fa)
                    continue;
                pt++;
                if(pt%2==1)
                {
                    all++;
                    ans[all].fp=upt[v];
                }
                else
                    ans[all].sp=upt[v];
            }
        }
        else
        {
            if(spe)
                num--;
            for(register int i=hd[u];~i;i=eg[i].next)
            {
                int v=eg[i].to;
                if(v==fa||v==spe)
                    continue;
                pt++;
                if(pt%2==1)
                {
                    all++;
                    ans[all].fp=upt[v];
                    if(pt==num)
                        ans[all].sp=u;
                }
                else
                    ans[all].sp=upt[v];
            }
        }
    }
    else
    {
        if(num%2==0)
        {
            if(spe)
                num--;
            for(register int i=hd[u];~i;i=eg[i].next)
            {
                int v=eg[i].to;
                if(v==fa||v==spe)
                    continue;
                pt++;
                if(pt%2==1)
                {
                    if(pt==num)
                    {
                        upt[u]=upt[v];
                        continue;
                    }
                    all++;
                    ans[all].fp=upt[v];
                }
                else
                    ans[all].sp=upt[v];
            }
        }
        else
        {
            for(register int i=hd[u];~i;i=eg[i].next)
            {
                int v=eg[i].to;
                if(v==fa)
                    continue;
                pt++;
                if(pt%2==1)
                {
                    if(pt==num)
                    {
                        upt[u]=upt[v];
                        continue;
                    }
                    all++;
                    ans[all].fp=upt[v];

                }
                else
                    ans[all].sp=upt[v];
            }
        }
    }
}
int main()
{
    memset(head,-1,sizeof(head));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(brg,0,sizeof(brg));
    memset(hd,-1,sizeof(hd));
    memset(vis,0,sizeof(vis));
    memset(sz,0,sizeof(sz));
    while(1)
    {
        t=0;
        cnt=1;
        tot=0;
        all=0;
        n=read();
        m=read();
        if(n==0&&m==0)
            break;
        for(register int i=1;i<=m;i++)
        {
            int s;
            s=read();
            int a,b;
            for(register int j=1;j<=s;j++)
            {
                a=read();
                if(j!=1)
                {
                    addedge(b,a);
                    addedge(a,b);
                }
                b=a;
            }
        }
        for(register int i=1;i<=n;i++)
        {
            if(!dfn[i])
                tarjan(i,0);
        }
        for(register int i=1;i<=n;i++)
            for(register int j=head[i];~j;j=e[j].next)
            {
                if(brg[j])
                    adde(i,e[j].to);
            }
        for(register int i=1;i<=n;i++)
        {
            if(!vis[i])
            {
                root=i;
                dep[i]=1;
                dfs(i,0);
            }
        }
        write(all);
        printf("\n");
        for(register int i=1;i<=all;i++)
        {
            write(ans[i].fp);
            printf(" ");
            write(ans[i].sp);
            printf("\n");
        }
        for(int i=1;i<=n;i++)
        {
            head[i]=-1;
            dfn[i]=0;
            low[i]=0;
            hd[i]=-1;
            vis[i]=0;
            sz[i]=0;
        }
        for(int i=1;i<=cnt+1;i++)
            brg[i]=0;
    }
    return 0;
}