// https://sio2.mimuw.edu.pl/c/oi33-1/p/
#include <bits/stdc++.h>
// using namespace std;
// #define GARY_DBG
#define GARY_LIB
// #define NIE_WYPISUJ_STOSIKOW
// #define int long long
constexpr int sizik = 1000 * 1001;
#define ar std::array
#define pr std::pair
#define vec std::vector
typedef vec<vec<int>> _kra;
int do_ktorego_stosu[sizik];
void solve() {
std::vector<std::pair<int, int>> ans;
int n, m;
std::cin >> n >> m;
std::vector<std::list<int>> stosy;
stosy.resize(m);
int empty_stack_idx = -1;
int stack_with_k1 = -1;
for (int i = 0; i < m; i++) {
int k;
std::cin >> k;
if (k == 0) empty_stack_idx = i;
for (int j = 0; j < k; j++) {
int a;
std::cin >> a;
stosy[i].push_back(a);
if (a == 1) stack_with_k1 = i;
do_ktorego_stosu[a] = i;
}
}
assert(stack_with_k1 >= 0);
bool isGood = true;
int prev = -1;
int idx = 0;
for (int x : stosy[stack_with_k1]) {
if (idx > 0) {
if (prev + 1 == x) {
} else {
isGood = false;
}
}
prev = x;
++idx;
}
if (!isGood && empty_stack_idx == -1) {
int mini = INT32_MAX;
int mini_id = -1;
for (int i = 0; i < m; i++) {
if (stosy[i].back() < mini) {
mini = stosy[i].back();
mini_id = i;
}
}
int best_id = -1;
int best_val = INT32_MAX;
for (int i = 0; i < m; i++) {
int x = stosy[i].front();
if (mini < x) {
if ((int)stosy[i].size() < best_val) {
best_val = (int)stosy[i].size();
best_id = i;
}
}
}
if (best_id >= 0) {
empty_stack_idx = best_id;
for (const auto& a : stosy[best_id]) {
ans.push_back({best_id, mini_id});
stosy[mini_id].push_back(a);
do_ktorego_stosu[a] = mini_id;
}
stosy[best_id].clear();
}
}
if (!isGood && empty_stack_idx == -1) {
std::cout << "-1\n";
return;
}
int primary_stack = stack_with_k1;
if (!isGood) {
int cnt = 1;
while (stosy[stack_with_k1].front() == cnt) {
cnt++;
stosy[empty_stack_idx].push_back(stosy[stack_with_k1].front());
ans.push_back({stack_with_k1, empty_stack_idx});
stosy[stack_with_k1].pop_front();
}
primary_stack = empty_stack_idx;
}
int last = stosy[primary_stack].back();
for (; last < n; last++) {
int idx1 = do_ktorego_stosu[last + 1];
ans.push_back({idx1, primary_stack});
}
std::cout << ans.size() << '\n';
#ifndef NIE_WYPISUJ_STOSIKOW
for (const auto& [a, b] : ans) {
std::cout << (a + 1) << " " << (b + 1) << '\n';
}
#endif
}
int32_t main() {
#ifndef GARY_DBG
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
std::cout.tie(0);
#endif
int t = 1;
// std::cin >> t;
for (; t > 0; t--) {
solve();
}
return 0;
}