#include <bits/stdc++.h>
using namespace std;
int n, m, a[40005], t[1000005], b, g, d[1000005], l[40005], l2[40005],
    l3[40005][5], ld[40005], t1, t2, t3, te = 0, tv, nx[1000005], sx, tx,
                                         px[40005], h[40005], ans;
inline void add() {
  te++;
  t[te] = t2, nx[te] = h[t1], d[te] = t3;
  h[t1] = te;
  return;
}
inline int bfs() {
  memset(a, 63, sizeof(a));
  queue<int> q;
  q.push(sx), a[sx] = 0;
  while (q.size()) {
    int x = q.front();
    q.pop();
    for (int i = h[x]; i; i = nx[i]) {
      if ((!d[i]) || a[t[i]] != a[0]) continue;
      a[t[i]] = a[x] + 1, q.push(t[i]);
      if (t[i] == tx) return 1;
    }
  }
  return 0;
}
inline int dfs(int x, int y) {
  if ((!y) || x == tx) return y;
  int r = 0;
  for (int &i = px[x]; i; i = nx[i])
    if (d[i] && a[t[i]] == a[x] + 1) {
      int z = dfs(t[i], min(d[i], y));
      if (!z) continue;
      y -= z, r += z, d[i] -= z, d[i ^ 1] += z;
      if (!y) break;
    }
  if (!r) a[x] = -1;
  return r;
}
inline int dinic() {
  int r = 0;
  while (bfs()) {
    memcpy(px, h, 80000);
    r += dfs(sx, 10000);
  }
  return r;
}
inline void bld(int x, int y1, int y2) {
  if (l[x] == 0) l[x] = ++tv;
  if (y1 == y2) {
    ld[l[x]] = y1;
    t1 = l[x], t2 = tx, t3 = 1;
    add();
    swap(t1, t2), t3 = 0;
    add();
    return;
  }
  int mid = (y1 + y2) >> 1;
  bld(x << 1, y1, mid), bld((x << 1) | 1, mid + 1, y2);
  t1 = l[x], t2 = l[x << 1], t3 = 10000000;
  add();
  swap(t1, t2), t3 = 0;
  add();
  t1 = l[x], t2 = l[(x << 1) | 1], t3 = 10000000;
  add();
  swap(t1, t2), t3 = 0;
  add();
  return;
}
inline void ins(int x, int y1, int y2, int z1, int z2, int w) {
  if (z1 <= y1 && z2 >= y2) {
    t1 = w, t2 = l[x], t3 = 1;
    add();
    swap(t1, t2), t3 = 0;
    add();
    return;
  }
  int mid = (y1 + y2) >> 1;
  if (mid >= z1) ins(x << 1, y1, mid, z1, z2, w);
  if (mid < z2) ins((x << 1) | 1, mid + 1, y2, z1, z2, w);
  return;
}
inline void s(int x, int y, int &z) {
  if (z) return;
  if (x && x <= n) {
    z = x;
    return;
  }
  for (int i = h[x]; i; i = nx[i]) {
    if (d[i] == 0 || t[i] == y) continue;
    s(t[i], x, z), d[i]--;
    return;
  }
  return;
}
int main() {
  scanf("%d%d", &n, &m);
  tv = n, memset(l, 0, sizeof(l)), memset(l3, 0, sizeof(l3)),
  memset(h, 0, sizeof(h)), te = 1;
  sx = ++tv, tx = ++tv;
  bld(1, 1, m);
  for (int i = 1; i <= n; i++) {
    int x, y, z;
    scanf("%d", &x);
    if (x == 0) {
      t1 = sx, t2 = i, t3 = 1 + (x == 2);
      add();
      swap(t1, t2), t3 = 0;
      add();
      scanf("%d", &y);
      for (int j = 1; j <= y; j++) {
        scanf("%d", &z);
        ins(1, 1, m, z, z, i);
      }
    } else if (x == 1) {
      t1 = sx, t2 = i, t3 = 1 + (x == 2);
      add();
      swap(t1, t2), t3 = 0;
      add();
      scanf("%d%d", &y, &z);
      ins(1, 1, m, y, z, i);
    } else if (x == 2) {
      scanf("%d%d%d", &x, &y, &z);
      l3[i][1] = x, l3[i][2] = y, l3[i][3] = z;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (l3[i][1]) {
      t1 = sx, t2 = i, t3 = 2;
      add();
      swap(t1, t2), t3 = 0;
      add();
      int x = l3[i][1], y = l3[i][2], z = l3[i][3];
      ins(1, 1, m, x, x, i), ins(1, 1, m, y, y, i), ins(1, 1, m, z, z, i);
    }
  }
  printf("%d\n", dinic());
  memset(l, 0, sizeof(l));
  for (int i = h[tx]; i; i = nx[i]) {
    if (d[i]) {
      s(t[i], tx, l[ld[t[i]]]);
      l2[l[ld[t[i]]]]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (l3[i][1] && l2[i] == 1) {
      l2[i]++;
      if (l[l3[i][1]] != i)
        l[l3[i][1]] = i;
      else
        l[l3[i][2]] = i;
    }
  }
  for (int i = h[tx]; i; i = nx[i]) {
    if (d[i]) {
      printf("%d %d\n", l[ld[t[i]]], ld[t[i]]);
    }
  }
  return 0;
}
