Problem: It is the year 2969. 1000 years have passed from the moon landing. Meanwhile, the humanity colonized the Hyperspace™ and lived in harmony.

Until we realized that we were not alone.

Not too far away from the Earth, the massive fleet of aliens' spaceships is preparing to attack the Earth. For the first time in a while, the humanity is in real danger. Crisis and panic are everywhere. The scientists from all around the solar system have met and discussed the possible solutions. However, no progress has been made.

The Earth's last hope is YOU!

Fortunately, the Earth is equipped with very powerful defense systems made by MDCS. There are N aliens' spaceships which form the line. The defense system consists of three types of weapons: 

  * SQL rockets – every SQL rocket can destroy at most one spaceship in the given set.
  * Cognition beams – every Cognition beam has an interval [l,r] and can destroy at most one spaceship in that interval.
  * OMG bazooka – every OMG bazooka has three possible targets, however, each bazooka can destroy either zero or exactly two spaceships. In addition, due to the smart targeting system, the sets of the three possible targets of any two different OMG bazookas are disjoint (that means that every ship is targeted with at most one OMG bazooka). 



Your task is to make a plan of the attack which will destroy the largest possible number of spaceships. Every destroyed spaceship should be destroyed with exactly one weapon.

Input

The first line contains two integers: the number of your weapons N (1≤ N≤ 5000) and the number of spaceships M (1≤ M≤ 5000).

In the next N lines, each line starts with one integer that represents type (either 0, 1 or 2). If the type is 0, then the weapon is SQL rocket, the rest of the line contains strictly positive number K (∑{K} ≤ 100 000) and array k_i (1≤ k_i≤ M) of K integers. If the type is 1, then the weapon is Cognition beam, the rest of the line contains integers l and r (1≤ l≤ r≤ M). If the type is 2 then the weapon is OMG bazooka, the rest of the line contains distinct numbers a, b and c  (1 ≤ a,b,c ≤ M).

Output

The first line should contain the maximum number of destroyed spaceships — X.

In the next X lines, every line should contain two numbers A and B, where A is an index of the weapon and B is an index of the spaceship which was destroyed by the weapon A.

Example

Input

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


Output

4
2 1
3 2
1 4
2 5

Note

SQL rocket can destroy only 4th spaceship. OMG Bazooka can destroy two of 1st, 4th or 5th spaceship, and Cognition beam can destroy any spaceship from the interval [1,4]. The maximum number of destroyed spaceship is 4, and one possible plan is that SQL rocket should destroy 4th spaceship, OMG bazooka should destroy 1st and 5th spaceship and Cognition beam should destroy 2nd spaceship.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
int n, m, cnt, lsum = 1, Found, S, i, j, flow, Cnt, al, x, y, z, k, r, s, p;
int head[50010], gaps[50010], gap[50010], T[50010];
int X[50010], Y[50010], bh[50010], Z[50010], fa[50010], v[50010];
struct Edge {
  int t, next, re, fl;
} e[200010];
struct Tree {
  int l, r, b, size;
} t[50010 * 10];
inline int Abs(int x) { return (x < 0) ? x : -x; }
inline int Min(int a, int b) { return (a < b) ? a : b; }
inline int Max(int a, int b) { return (a > b) ? a : b; }
inline int read() {
  int p = 0;
  char c = getchar();
  while (c < 48 || c > 57) c = getchar();
  while (c >= 48 && c <= 57) p = (p << 1) + (p << 3) + c - 48, c = getchar();
  return p;
}
inline void Add(int s, int t, int fl) {
  e[lsum].t = t;
  e[lsum].re = lsum + 1;
  e[lsum].fl = fl;
  e[lsum].next = head[s];
  head[s] = lsum++;
  e[lsum].t = s;
  e[lsum].re = lsum - 1;
  e[lsum].next = head[t];
  head[t] = lsum++;
}
inline void Sap(int x) {
  int F = al, i = 0, mingap = cnt - 1;
  if (x == cnt) {
    Found = 1;
    flow += al;
    return;
  }
  for (i = head[x]; i; i = e[i].next)
    if (e[i].fl) {
      if (gap[e[i].t] + 1 == gap[x]) {
        al = Min(al, e[i].fl);
        Sap(e[i].t);
        if (gap[1] >= cnt) return;
        if (Found) break;
        al = F;
      }
      mingap = Min(mingap, gap[e[i].t]);
    }
  if (!Found) {
    gaps[gap[x]]--;
    if (!gaps[gap[x]]) gap[1] = cnt;
    gaps[gap[x] = mingap + 1]++;
  } else
    e[i].fl -= al, e[e[i].re].fl += al;
}
inline void Maketree(int x, int low, int high) {
  int mid = (low + high) >> 1;
  t[x].l = low;
  t[x].r = high;
  t[x].size = (high - low) + 1;
  if (low == high) {
    t[x].b = 1 + n + low;
    return;
  }
  t[x].b = ++cnt;
  Maketree((x << 1), low, mid);
  Maketree(((x << 1) + 1), mid + 1, high);
  Add(t[x].b, t[(x << 1)].b, t[(x << 1)].size);
  Add(t[x].b, t[((x << 1) + 1)].b, t[((x << 1) + 1)].size);
}
inline void Query(int x, int low, int high, int p) {
  int mid = (t[x].l + t[x].r) >> 1;
  if (t[x].l == low && t[x].r == high) {
    Add(p, t[x].b, 1);
    return;
  }
  if (high <= mid)
    Query((x << 1), low, high, p);
  else if (low > mid)
    Query(((x << 1) + 1), low, high, p);
  else
    Query((x << 1), low, mid, p), Query(((x << 1) + 1), mid + 1, high, p);
}
inline void MakeMap() {
  int i = 0;
  for (i = 1; i <= n; i++) {
    T[i] = read();
    if (T[i] == 0) {
      Add(1, 1 + i, 1);
      k = read();
      for (j = 1; j <= k; j++) {
        x = read();
        Add(1 + i, 1 + n + x, 1);
      }
    } else if (T[i] == 1) {
      Add(1, 1 + i, 1);
      x = read();
      y = read();
      Query(1, x, y, i + 1);
    } else {
      Add(1, 1 + i, 2);
      s++;
      bh[i] = s;
      X[s] = read();
      Y[s] = read();
      Z[s] = read();
      Add(1 + i, 1 + n + X[s], 1);
      Add(1 + i, 1 + n + Y[s], 1);
      Add(1 + i, 1 + n + Z[s], 1);
    }
  }
  cnt++;
  for (i = 1; i <= m; i++) Add(1 + n + i, cnt, 1);
}
inline void Dfs(int x, int p) {
  int i = 0, r = 0;
  if (Found) return;
  if (x > 1 && x <= 1 + n) {
    if (!v[x - 1]) fa[p] = x - 1, Found = v[x - 1] = 1;
    return;
  }
  for (i = head[x]; i; i = e[i].next) {
    r = e[i].t;
    if (!e[i].fl) continue;
    if (r > 1 + n) continue;
    Dfs(r, p);
  }
  if (Found) return;
  for (i = head[x]; i; i = e[i].next) {
    if (!e[i].fl) continue;
    if (e[i].t < 1 + n) continue;
    if (x > 1 + n + m)
      if (e[i].t < x) Dfs(e[i].t, p);
    if (x < 1 + n + m) Dfs(e[i].t, p);
  }
}
int main() {
  n = read();
  m = read();
  S = 1;
  cnt = 1 + n + m;
  Maketree(1, 1, m);
  MakeMap();
  gaps[0] = cnt;
  while (gap[1] < cnt) {
    al = 0x3f3f3f3f;
    Found = 0;
    Sap(1);
  }
  for (i = 2; i <= n + 1; i++) {
    for (j = head[i]; j; j = e[j].next) {
      p = e[j].t;
      if ((p > 1 + n) && (p <= 1 + n + m))
        if (e[j].fl == 0) fa[p - 1 - n] = i - 1;
    }
  }
  printf("%d\n", flow);
  for (i = 1; i <= m; i++) v[fa[i]] = 1;
  for (i = head[cnt]; i; i = e[i].next) {
    p = e[i].t;
    Found = 0;
    if (e[i].fl)
      if (!fa[p - 1 - n]) Dfs(p, p - 1 - n);
  }
  for (i = head[1]; i; i = e[i].next) {
    p = e[i].t - 1;
    if (T[p] != 2) continue;
    if (e[i].fl == 1) {
      r = bh[p];
      if (fa[X[r]] != p)
        fa[X[r]] = p;
      else if (fa[Y[r]] != p)
        fa[Y[r]] = p;
      else if (fa[Z[r]] != p)
        fa[Z[r]] = p;
    }
  }
  for (i = 1; i <= m; i++)
    if (fa[i]) printf("%d %d\n", fa[i], i);
  return 0;
}