Problem: There was no problem about a cactus at the NERC 2020 online round. That's a bad mistake, so judges decided to fix it. You shall not pass to the World Finals 2021 without solving a problem about a cactus!

A cactus is a connected undirected graph in which every edge lies on at most one simple cycle. Intuitively, a cactus is a generalization of a tree where some cycles are allowed. Multiedges (multiple edges between a pair of vertices) and loops (edges that connect a vertex to itself) are not allowed in a cactus. 

Cher has got a cactus. She calls cactus strong if it is impossible to add an edge to it in such a way that it still remains a cactus. But Cher thinks her cactus is not strong enough. She wants to add the smallest possible number of edges to it to make it strong, i. e. to create a new cactus with the same vertices, so that the original cactus is a subgraph of the new one, and it is impossible to add another edge to it so that the graph remains a cactus. Cher hired you to do this job for her. So... it's on you!

Input

The input consists of one or more independent test cases.

The first line of each test case contains two integers n and m (1 ≤ n ≤ 10^5, 0 ≤ m ≤ 10^5), where n is the number of vertices in the graph. Vertices are numbered from 1 to n. Edges of the graph are represented by a set of edge-distinct paths, where m is the number of such paths. 

Each of the following m lines contains a path in the graph. A path starts with an integer number s_i (2 ≤ s_i ≤ 1000) followed by s_i integers from 1 to n. These s_i integers represent vertices of a path. Adjacent vertices in a path are distinct. The path can go through the same vertex multiple times, but every edge is traversed exactly once in the whole test case. There are no multiedges in the graph (there is at most one edge between any two vertices).

The last line of the input after all test cases always contains two zeros. It does not define a test case. It just marks the end of the input and does not require any output.

All graphs in the input are cacti. The total sum of all values of n and the total sum of all values of m throughout the input both do not exceed 10^5.

Output

For each test case, first output the line with the minimal possible number of additional edges A. Then output A lines, each describing one edge as u_i v_i, where u_i and v_i are the numbers of vertices to connect. After adding these edges, the resulting graph must be a strong cactus.

Example

Input


6 1
7 1 2 5 6 2 3 4
3 1
4 1 2 3 1
5 2
3 1 3 5
3 1 2 4
7 2
6 1 2 3 4 5 3
3 6 5 7
0 0


Output


1
1 4
0
1
5 4
2
1 3
6 7

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int n, m;
    while(cin >> n >> m && (n || m)){
        vector<int> G[n + 1], g[n + 1];
        vector<int> dfn(n + 1), vis(n + 1), fa(n + 1);
        int dfs_clock = 0;
        int bcc_cnt = 0;
        vector<pair<int, int> > use_edge;
        function<void(int, int) > dfs = [&](int u, int f){
            dfn[u] = ++dfs_clock;
            fa[u] = f;
            for(auto v : G[u]){
                if(v == f) continue;
                if(!dfn[v])dfs(v, u);
                else if(dfn[v] > dfn[u]) continue;
                else{
                    bcc_cnt++;
                    int p = u;
                    while(1){
                        use_edge.emplace_back(min(p, fa[p]), max(p, fa[p]));
                        vis[p] = bcc_cnt;
                        p = fa[p];
                        if(p == v) break;
                    }
                    use_edge.emplace_back(min(u, v), max(u, v));
                }
            }
        };



        for (int i = 1; i <= m; i += 1){
            int len;
            cin >> len;
            vector<int> path(len);
            for(int j = 0; j < len; j += 1){
                cin >> path[j];
            }
            for(int j = 1; j < len; j += 1){
                G[path[j]].emplace_back(path[j - 1]);
                G[path[j - 1]].emplace_back(path[j]);
            }
        }

        for(int i = 1; i <= n; i++){
            sort(G[i].begin(), G[i].end());
            G[i].erase(unique(G[i].begin(), G[i].end()), G[i].end());
        }


        if(n <= 2){
            cout << "0\n";
            continue;
        }


        for(int i = 1; i <= n; i += 1){
            if(!dfn[i]) dfs(i, 0);
        }

        sort(use_edge.begin(), use_edge.end());
        use_edge.erase(unique(use_edge.begin(), use_edge.end()), use_edge.end());

        /*
        cout << "used\n";
        for(auto[x, y] : use_edge){
            cout << x << " " << y << "\n";
        }
        */

        vector<pair<int, int> > new_edge;
        for (int i = 1; i <= n; i += 1){
            for(auto u : G[i]){
                if(i > u) continue;
                int x = min(i, u);
                int y = max(i, u);
                if(!binary_search(use_edge.begin(), use_edge.end(), make_pair(x, y))) {
                    new_edge.emplace_back(x, y);
                    new_edge.emplace_back(y, x);
                }
            }
        }

        /*
        cout << "edge list\n";
        for(auto [x, y] : new_edge){
            cout << x << " " << y << "\n";
        }
        */
        vector<int> deg(n + 1);
        sort(new_edge.begin(), new_edge.end());
        new_edge.erase(unique(new_edge.begin(), new_edge.end()), new_edge.end());
        for(auto [x, y] : new_edge){
            assert(x != y);
            g[x].emplace_back(y);    
        }
        for (int i = 1; i <= n; i += 1){
            sort(g[i].begin(), g[i].end());
            g[i].erase(unique(g[i].begin(), g[i].end()), g[i].end());
            deg[i] = g[i].size();
        }
        
        auto done = vector<int>(n + 1, 0);
        fa.clear();
        fa = vector<int>(n + 1, 0);
        vector<pair<int, int> > res;

        auto add = [&](int x, int y){
            assert(x != y);
            res.emplace_back(x, y);
        };

        vector<int> tag(n + 1);
        function<void(int, int, vector<int> &) > find_point = [&](int u, int f, vector<int> &vec){
            vec.emplace_back(u);
            for(auto v : g[u]){
                if(v == f || done[v]) continue;
                find_point(v, u, vec);
            }
        };
        
        function<int(int, int) > solve = [&](int u, int f){
            done[u] = 1;
            fa[u] = f;
            vector<int> vec;
            for(auto v : g[u]){
                if(v == f || done[v]) continue;
                int p = solve(v, u);
                if(p < 0) {
                    tag[u] = -p;
                }
                if(p)vec.emplace_back(p);
            }
            sort(vec.begin(), vec.end());
            reverse(vec.begin(), vec.end());
            if(tag[u]) vec.pop_back();
            for(auto &p : vec){
                p = abs(p);
            }
            for(int i = 1; i < vec.size(); i += 2){
                add(vec[i - 1], vec[i]);
            }
            if(int(vec.size()) % 2){
                if(fa[u]){
                    return vec.back();
                }
                else{
                    if(fa[vec.back()] != u) add(u, vec.back()); 
                    else if(tag[vec.back()]) add(u, tag[vec.back()]);
                    return 0;
                }
            }
            else{
                if(tag[u]) return u;
                return -u;
            }
        };

        for(int i = 1; i <= n; i += 1){
            if(!done[i]) {
                vector<int> point;
                find_point(i, 0, point);
                int rt = -1;

                for(auto u : point){
                    if(deg[u] == 1){
                        rt = u;
                        break;
                    }
                }
                if(rt == -1){
                    done[i] = 1;
                    continue;
                }
                solve(rt, 0);
            }
        }
        
        /*
        cout << res.size() << "\n";
        for(auto [x, y] : res){
            cout << x << " " << y << "\n";
        }

        cout << "left list:\n";
        for(auto [x, y] : left){
            cout << x << " " << y << "\n";
        }*/

        cout << res.size() << "\n";
        for(auto [x, y] : res){
            cout << x << " " << y << "\n";
        }

    }
    return 0;
}