#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); i++)

const int N = 10000;

int n, m;
bitset<N> c[N];
int cc = 1, used[N], pa[N]; // used[i] == cc
/** used[1], pa[2] */

int dfs( int v ) {
  used[v] = cc;
  forn(i, m)
    if (c[v][i] && (pa[i] == -1 || (used[pa[i]] != cc && dfs(pa[i])))) {
      pa[i] = v;
      return 1;
    }
  return 0;
}

int main() {
  cin >> n >> m;
  forn(i, n) {
    int x;
    while ((cin >> x) && x)
      c[i][x - 1] = 1;
  }
  fill(pa, pa + m, -1);
  /** deg <= D, |M| = k */
  forn(i, n) /** O(n*D + k*(k*D))  */
    if (dfs(i))
      cc++;
  printf("%d\n", cc - 1);
  forn(i, m)
    if (pa[i] != -1)
      printf("%d %d\n", pa[i] + 1, i + 1);
}