#include <bits/stdc++.h>
#pragma GCC optimize "-O3"
using namespace std;
const int MK = 210;
const int MAXN = 31000;
int n, k;
int dd[MK][MAXN];
int go[MK];
int p[MAXN];
int sz[MAXN];
set<pair<int, int>> ed;
int get(int a) {
  if (a == p[a]) return a;
  return p[a] = get(p[a]);
}
void un(int a, int b) {
  if (a > b) swap(a, b);
  if (ed.count({a, b})) return;
  ed.insert({a, b});
  a = get(a);
  b = get(b);
  if (a == b) {
    cout << -1 << "\n";
    exit(0);
  }
  if (sz[a] > sz[b]) swap(a, b);
  p[a] = b;
  sz[b] += sz[a];
}
int way[MAXN];
int was[MAXN];
map<unsigned long long, int> mm;
const unsigned long long P = 1e9 + 7;
unsigned long long geths(int x) {
  unsigned long long now = 0;
  for (int i = 0; i < k; ++i) now = (now * P + dd[i][x]);
  return now;
}
unsigned long long geths1(int x) {
  unsigned long long now = 0;
  for (int i = 0; i < k; ++i) {
    if (dd[i][x] == 0) {
      cout << -1 << "\n";
      exit(0);
    }
    now = (now * P + dd[i][x] - 1);
  }
  return now;
}
void no() {
  cout << -1 << "\n";
  exit(0);
}
int dp[MAXN];
vector<int> eds[MAXN];
void dfs1(int v, int h) {
  was[v] = 1;
  dp[v] = h;
  for (int u : eds[v]) {
    if (!was[u]) dfs1(u, h + 1);
  }
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; ++i) p[i] = i, sz[i] = 1;
  for (int i = 0; i < k; ++i) {
    go[i] = -1;
    for (int j = 0; j < n; ++j) {
      cin >> dd[i][j];
      if (dd[i][j] == 0) go[i] = j;
    }
    if (go[i] == -1) no();
  }
  was[go[0]] = 1;
  for (int i = 1; i < k; ++i) {
    int len = dd[0][go[i]];
    for (int j = 0; j < n; ++j) {
      if (dd[0][j] + dd[i][j] == len) {
        way[dd[0][j]] = j;
      }
    }
    for (int i = 0; i <= len; ++i) was[way[i]] = 1;
    for (int i = 0; i + 1 <= len; ++i) un(way[i], way[i + 1]);
  }
  for (int i = 0; i < n; ++i) {
    if (was[i]) {
      mm[geths(i)] = i;
    }
  }
  vector<pair<int, int>> vv;
  for (int i = 0; i < n; ++i)
    if (!was[i]) vv.emplace_back(dd[0][i], i);
  sort((vv).begin(), (vv).end());
  for (int i = 0; i < vv.size(); ++i) {
    int x = vv[i].second;
    unsigned long long now = geths1(x);
    if (!mm.count(now)) no();
    un(mm[now], x);
    mm[geths(x)] = x;
  }
  if (ed.size() != n - 1) {
    no();
  }
  for (auto x : ed) {
    eds[x.first].push_back(x.second);
    eds[x.second].push_back(x.first);
  }
  for (int i = 0; i < k; ++i) {
    memset(was, 0, sizeof(was));
    dfs1(go[i], 0);
    for (int j = 0; j < n; ++j)
      if (dp[j] != dd[i][j]) no();
  }
  for (auto x : ed) cout << x.first + 1 << " " << x.second + 1 << "\n";
  return 0;
}

### Prompt

Construct a cpp code solution to the problem outlined:

Petya had a tree consisting of n vertices numbered with integers from 1 to n. Accidentally he lost his tree. 

Petya remembers information about k vertices: distances from each of them to each of the n tree vertices.

Your task is to restore any tree that satisfies the information that Petya remembers or report that such tree doesn't exist.

Input

The first line contains two integers n and k (2 ≤ n ≤ 30 000, 1 ≤ k ≤ min(200, n)) — the number of vertices in the tree and the number of vertices about which Petya remembers distance information.

The following k lines contain remembered information. The i-th line contains n integers di, 1, di, 2, ..., di, n (0 ≤ di, j ≤ n - 1), where di, j — the distance to j-th vertex from the i-th vertex that Petya remembers.

Output

If there are no suitable trees, print -1.

In the other case, print n - 1 lines: each line should contain two vertices connected by edge in the required tree. You can print edges and vertices in an edge in any order. The tree vertices are enumerated from 1 to n.

If there are many solutions print any of them.

Examples

Input

5 2
0 1 2 3 2
2 1 0 1 2


Output

2 1
3 2
4 3
5 2


Input

3 1
1 2 1


Output

-1

Note

Picture for the first sample:

<image>

### Response

```cpp
#include <bits/stdc++.h>
#pragma GCC optimize "-O3"
using namespace std;
const int MK = 210;
const int MAXN = 31000;
int n, k;
int dd[MK][MAXN];
int go[MK];
int p[MAXN];
int sz[MAXN];
set<pair<int, int>> ed;
int get(int a) {
  if (a == p[a]) return a;
  return p[a] = get(p[a]);
}
void un(int a, int b) {
  if (a > b) swap(a, b);
  if (ed.count({a, b})) return;
  ed.insert({a, b});
  a = get(a);
  b = get(b);
  if (a == b) {
    cout << -1 << "\n";
    exit(0);
  }
  if (sz[a] > sz[b]) swap(a, b);
  p[a] = b;
  sz[b] += sz[a];
}
int way[MAXN];
int was[MAXN];
map<unsigned long long, int> mm;
const unsigned long long P = 1e9 + 7;
unsigned long long geths(int x) {
  unsigned long long now = 0;
  for (int i = 0; i < k; ++i) now = (now * P + dd[i][x]);
  return now;
}
unsigned long long geths1(int x) {
  unsigned long long now = 0;
  for (int i = 0; i < k; ++i) {
    if (dd[i][x] == 0) {
      cout << -1 << "\n";
      exit(0);
    }
    now = (now * P + dd[i][x] - 1);
  }
  return now;
}
void no() {
  cout << -1 << "\n";
  exit(0);
}
int dp[MAXN];
vector<int> eds[MAXN];
void dfs1(int v, int h) {
  was[v] = 1;
  dp[v] = h;
  for (int u : eds[v]) {
    if (!was[u]) dfs1(u, h + 1);
  }
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; ++i) p[i] = i, sz[i] = 1;
  for (int i = 0; i < k; ++i) {
    go[i] = -1;
    for (int j = 0; j < n; ++j) {
      cin >> dd[i][j];
      if (dd[i][j] == 0) go[i] = j;
    }
    if (go[i] == -1) no();
  }
  was[go[0]] = 1;
  for (int i = 1; i < k; ++i) {
    int len = dd[0][go[i]];
    for (int j = 0; j < n; ++j) {
      if (dd[0][j] + dd[i][j] == len) {
        way[dd[0][j]] = j;
      }
    }
    for (int i = 0; i <= len; ++i) was[way[i]] = 1;
    for (int i = 0; i + 1 <= len; ++i) un(way[i], way[i + 1]);
  }
  for (int i = 0; i < n; ++i) {
    if (was[i]) {
      mm[geths(i)] = i;
    }
  }
  vector<pair<int, int>> vv;
  for (int i = 0; i < n; ++i)
    if (!was[i]) vv.emplace_back(dd[0][i], i);
  sort((vv).begin(), (vv).end());
  for (int i = 0; i < vv.size(); ++i) {
    int x = vv[i].second;
    unsigned long long now = geths1(x);
    if (!mm.count(now)) no();
    un(mm[now], x);
    mm[geths(x)] = x;
  }
  if (ed.size() != n - 1) {
    no();
  }
  for (auto x : ed) {
    eds[x.first].push_back(x.second);
    eds[x.second].push_back(x.first);
  }
  for (int i = 0; i < k; ++i) {
    memset(was, 0, sizeof(was));
    dfs1(go[i], 0);
    for (int j = 0; j < n; ++j)
      if (dp[j] != dd[i][j]) no();
  }
  for (auto x : ed) cout << x.first + 1 << " " << x.second + 1 << "\n";
  return 0;
}

```