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>
//#include <atcoder/all>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long long INF = 1e18;
//const ll mod = 1000000007;
ll N, M;
using UnWeightedGraph = vector<vector<int>>;
template< typename G >
struct LowLink {
  const G &g;
  vector< int > used, ord, low;
  vector< int > articulation;
  vector< pair< int, int > > bridge;
 
  LowLink(const G &g) : g(g) {}
 
  int dfs(int idx, int k, int par) {
    used[idx] = true;
    ord[idx] = k++;
    low[idx] = ord[idx];
    bool is_articulation = false;
    int cnt = 0;
    for(auto &to : g[idx]) {
      if(!used[to]) {
        ++cnt;
        k = dfs(to, k, idx);
        low[idx] = min(low[idx], low[to]);
        is_articulation |= ~par && low[to] >= ord[idx];
        if(ord[idx] < low[to]) bridge.emplace_back(minmax(idx, (int) to));
      } else if(to != par) {
        low[idx] = min(low[idx], ord[to]);
      }
    }
    is_articulation |= par == -1 && cnt > 1;
    if(is_articulation) articulation.push_back(idx);
    return k;
  }
 
  virtual void build() {
    used.assign(g.size(), 0);
    ord.assign(g.size(), 0);
    low.assign(g.size(), 0);
    int k = 0;
    for(int i = 0; i < g.size(); i++) {
      if(!used[i]) k = dfs(i, k, -1);
    }
  }
};
vector<ll> edges[105000];
ll depth[105000];
vector<l_l> ans;
ll dp[105000];
void dfs(int now, int from) {
    vector<int> children;
    for(auto to : edges[now]) {
        if(from == to) continue;
        depth[to] = depth[now] + 1;
        dfs(to, now);
        children.push_back(to);
    }
    if(children.empty()) {
        dp[now] = now;
        return;
    }
    vector<int> v;
    for(auto c : children) {
        assert(dp[c] != -1);
        if(dp[c] == -1) continue;
        v.push_back(dp[c]);
    }
    assert(v.size() == children.size());
    sort(v.begin(), v.end(), [&](ll q, ll w){
        return depth[q] > depth[w];
    });
    if(depth[now] != 0) {
        if(v.size() % 2 == 0) {
            for(int i = 0; i + 2 < v.size(); i += 2) {
                //cerr << "A" << endl;
                ans.push_back({v[i], v[i+1]});
            }
            int n = v.size();
            if(abs(depth[now] - depth[v[n-1]]) <= 1) {
                dp[now] = v[n-2];
            } else {
                ans.push_back({v[n-2], v[n-1]});
                dp[now] = now;
            }
        } else {
            for(int i = 0; i + 1 < v.size(); i += 2) {
                //cerr << "B" << endl;
                ans.push_back({v[i], v[i+1]});
            }
            dp[now] = v.back();
        }
    } else {
        if(v.size() % 2 == 0) {
            for(int i = 0; i < v.size(); i += 2) {
                //cerr << "A" << endl;
                ans.push_back({v[i], v[i+1]});
            }
        } else {
            for(int i = 0; i + 1 < v.size(); i += 2) {
                //cerr << "B" << endl;
                ans.push_back({v[i], v[i+1]});
            }
            int n = v.size();
            if(abs(depth[now] - depth[v[n-1]]) > 1) {
                ans.push_back({v[n-1], now});
            }
        }
    }
    //cerr << "dfs: " << now << " " << depth[now] << " " << dp[now] << endl;
}

void solve() {
    //cerr << "------------------" << endl;
    ans.clear();
    UnWeightedGraph g(N);
    for(int i = 0; i < N; i++) {
        edges[i].clear();
        depth[i] = -1;
        dp[i] = -1;
    }
    for(int i = 0; i < M; i++) {
        int S;
        cin >> S;
        vector<int> a(S);
        for(int j = 0; j < S; j++) {
            cin >> a[j];
            a[j]--;
        }
        for(int j = 0; j + 1 < S; j++) {
            g[a[j]].push_back(a[j+1]);
            g[a[j+1]].push_back(a[j]);
        }
    }
    LowLink<UnWeightedGraph> LL(g);
    LL.build();
    for(auto e : LL.bridge) {
        //cerr << e.first << " " << e.second << endl;
        edges[e.first].push_back(e.second);
        edges[e.second].push_back(e.first);
    }
    for(int i = 0; i < N; i++) {
        if(depth[i] != -1) continue;
        depth[i] = 0;
        dfs(i, -1);
    }
    cout << ans.size() << endl;
    for(auto e : ans) {
        cout << e.first + 1 << " " << e.second + 1 << endl;
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> N >> M) {
        if(N == 0 and M == 0) break;
        solve();
    }
    return 0;
}