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>
const int MAXN = 5010;
const int MAXP = MAXN * 3;
const int MAXE = 500010;
const int INF = 0x3f3f3f3f;
struct _ {
  int to, nxt, val;
} es[MAXE];
int head[MAXP], tot = 1;
void adde(int b, int e, int v) {
  es[++tot] = (_){e, head[b], v}, head[b] = tot;
  es[++tot] = (_){b, head[e], 0}, head[e] = tot;
}
int S, T, idx;
int dis[MAXP];
bool bfs() {
  static int que[MAXP], b, e;
  memset(dis, 0, idx + 1 << 2);
  dis[que[b = e = 1] = S] = 1;
  while (b <= e) {
    int t = que[b++];
    for (int i = head[t]; i; i = es[i].nxt)
      if (es[i].val && !dis[es[i].to]) dis[que[++e] = es[i].to] = dis[t] + 1;
  }
  return dis[T] > 0;
}
int dinic(int u, int minv) {
  if (u == T) return minv;
  int t, res = 0;
  for (int &i = head[u]; i; i = es[i].nxt)
    if (es[i].val && dis[es[i].to] == dis[u] + 1 &&
        (t = dinic(es[i].to, std::min(es[i].val, minv)))) {
      es[i].val -= t;
      es[i ^ 1].val += t;
      res += t;
      if (!(minv -= t)) break;
    }
  if (!res) dis[u] = 0;
  return res;
}
int flow() {
  static int th[MAXP];
  memcpy(th, head, idx + 1 << 2);
  int res = 0;
  while (bfs()) {
    res += dinic(S, INF);
    memcpy(head, th, idx + 1 << 2);
  }
  return res;
}
int bel[MAXN], frm[MAXP];
int n, m;
std::vector<int> opt[MAXN];
int tr[MAXN << 2];
void build(int u, int l, int r) {
  if (l == r) return (void)(tr[u] = bel[l]);
  int mid = l + r >> 1;
  build(u << 1, l, mid);
  build(u << 1 | 1, mid + 1, r);
  tr[u] = ++idx;
  adde(tr[u], tr[u << 1], INF);
  adde(tr[u], tr[u << 1 | 1], INF);
}
void qry(int u, int l, int r, int L, int R, int tar) {
  if (L <= l && r <= R) return adde(tar, tr[u], 1);
  int mid = l + r >> 1;
  if (L <= mid) qry(u << 1, l, mid, L, R, tar);
  if (mid < R) qry(u << 1 | 1, mid + 1, r, L, R, tar);
}
int glp(int u) {
  for (int &i = head[u]; i; i = es[i].nxt)
    if (!(i & 1) && es[i ^ 1].val) {
      es[i].val += 1, es[i ^ 1].val -= 1;
      if (es[i].to == T) return u;
      return glp(es[i].to);
    }
  assert(0);
  return -12243;
}
int main() {
  std::ios_base::sync_with_stdio(false), std::cin.tie(0);
  std::cin >> m >> n;
  for (int i = 1, typ; i <= m; ++i) {
    std::cin >> typ;
    int sz;
    if (typ == 0)
      std::cin >> sz;
    else if (typ == 1)
      sz = 2;
    else
      sz = 3;
    opt[i].resize(sz + 1);
    opt[i][0] = typ;
    for (int j = 1; j <= sz; ++j) std::cin >> opt[i][j];
    if (typ == 2) {
      frm[++idx] = i;
      for (int j = 1; j <= 3; ++j) bel[opt[i][j]] = idx;
    }
  }
  for (int i = 1; i <= n; ++i)
    if (!bel[i]) frm[bel[i] = ++idx] = -i;
  S = 0, T = ++idx;
  for (int i = 1; i < idx; ++i) adde(i, T, 1);
  build(1, 1, n);
  for (int i = 1; i <= m; ++i)
    if (opt[i][0] <= 1) {
      frm[++idx] = i;
      adde(S, idx, 1);
      if (opt[i][0] == 0) {
        for (auto t : opt[i])
          if (t) adde(idx, bel[t], 1);
      } else
        qry(1, 1, n, opt[i][1], opt[i][2], idx);
    }
  int fl = flow();
  std::cerr << "FIN " << fl << std::endl;
  static int lst[MAXN];
  for (int i = head[S]; i; i = es[i].nxt)
    if (es[i ^ 1].val) lst[frm[es[i].to]] = frm[glp(es[i].to)];
  std::vector<std::pair<int, int> > ansl;
  for (int i = 1; i <= m; ++i)
    if (lst[i] < 0)
      ansl.emplace_back(i, -lst[i]);
    else if (lst[i] > 0)
      lst[lst[i]] = i;
  for (int i = 1; i <= m; ++i)
    if (opt[i][0] == 2) {
      int cnt = 0;
      if (int v = lst[i]) {
        if (opt[v][0] == 0) {
          for (auto t : opt[v])
            for (int j = 1; j <= 3; ++j)
              if (t == opt[i][j]) cnt = t;
        } else {
          int l = opt[v][1], r = opt[v][2];
          for (int j = 1; j <= 3; ++j)
            if (l <= opt[i][j] && opt[i][j] <= r) cnt = opt[i][j];
        }
        ansl.emplace_back(v, cnt);
      }
      for (int j = 1, cl = 2; j <= 3 && cl; ++j)
        if (opt[i][j] != cnt) --cl, ansl.emplace_back(i, opt[i][j]);
    }
  std::cout << ansl.size() << std::endl;
  for (auto t : ansl) std::cout << t.first << ' ' << t.second << '\n';
  return 0;
}