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;
class edge {
 public:
  int e, nxt, cap;
  edge(int e1 = -1, int nxt1 = -1, int cap1 = 0) {
    e = e1, nxt = nxt1, cap = cap1;
  }
};
int S, T, n, m, sum;
int dep[25010];
bool vis[25010];
class graph {
 public:
  edge g[5000000];
  int head[25010], tmpcnt;
  int cur[25010];
  void init() {
    memset(head, 255, sizeof(head));
    tmpcnt = 0;
  }
  int addedge(int s, int e, int cap) {
    g[tmpcnt] = edge(e, head[s], cap), head[s] = tmpcnt++;
    g[tmpcnt] = edge(s, head[e], 0), head[e] = tmpcnt++;
    return tmpcnt - 2;
  }
  bool bfs() {
    memset(vis, 0, sizeof(vis));
    memset(dep, 0, sizeof(dep));
    queue<int> q;
    q.push(S);
    vis[S] = 1;
    while (!q.empty()) {
      int fr = q.front();
      q.pop();
      for (int i = head[fr]; i != -1; i = g[i].nxt) {
        if (g[i].cap > 0 && !vis[g[i].e]) {
          dep[g[i].e] = dep[fr] + 1;
          vis[g[i].e] = 1;
          q.push(g[i].e);
        }
      }
    }
    return vis[T];
  }
  int dfs(int np, int maxflow) {
    int f;
    int flow = 0;
    if (np == T || !maxflow) return maxflow;
    for (int i = cur[np]; i != -1; i = g[i].nxt) {
      cur[np] = i;
      if (g[i].cap > 0 && dep[g[i].e] == dep[np] + 1 &&
          (f = dfs(g[i].e, min(g[i].cap, maxflow))) > 0) {
        g[i].cap -= f;
        g[i ^ 1].cap += f;
        flow += f;
        maxflow -= f;
        if (!maxflow) break;
      }
    }
    return flow;
  }
  int dinic() {
    int out = 0;
    while (bfs()) memcpy(cur, head, sizeof(cur)), out += dfs(S, 0x3f3f3f3f);
    return out;
  }
} G;
int rt, ch[25010][2], nodecnt, id[5010], iid[25010], efa[25010];
void build(int &p, int l, int r) {
  p = ++nodecnt;
  if (l == r) {
    id[l] = p;
    iid[p] = l;
    return;
  }
  int mid = (l + r) >> 1;
  build(ch[p][0], l, mid);
  build(ch[p][1], mid + 1, r);
  efa[ch[p][0]] = G.addedge(p, ch[p][0], 0x3f3f3f3f);
  efa[ch[p][1]] = G.addedge(p, ch[p][1], 0x3f3f3f3f);
}
void link(int p, int l, int r, int ml, int mr, int st, int w) {
  if (ml <= l && r <= mr) {
    G.addedge(st, p, w);
    return;
  }
  int mid = (l + r) >> 1;
  if (ml <= mid) link(ch[p][0], l, mid, ml, mr, st, w);
  if (mr > mid) link(ch[p][1], mid + 1, r, ml, mr, st, w);
}
int ANS[5010], cnt[5010], etoT[5010];
vector<int> operate(int p, int l, int r) {
  vector<int> ret;
  if (l == r) {
    if (G.g[etoT[l] ^ 1].cap) ret.push_back(l);
  } else {
    int mid = (l + r) >> 1;
    ret = operate(ch[p][0], l, mid);
    vector<int> tmp = operate(ch[p][1], mid + 1, r);
    ret.insert(ret.end(), tmp.begin(), tmp.end());
  }
  for (int i = G.head[p]; i != -1; i = G.g[i].nxt) {
    if (G.g[i].e <= n && G.g[i].cap) {
      ANS[ret.back()] = G.g[i].e;
      ret.pop_back();
    }
  }
  return ret;
}
int opt[5010], val[5010][3];
int main() {
  G.init();
  scanf("%d%d", &n, &m);
  nodecnt = n;
  build(rt, 1, m);
  S = nodecnt + 1;
  T = nodecnt + 2;
  for (int i = 1; i <= m; i++) etoT[i] = G.addedge(id[i], T, 1);
  for (int i = 1, ti, tj; i <= n; i++) {
    scanf("%d", &opt[i]);
    if (opt[i] == 0) {
      scanf("%d", &ti);
      for (int j = 1; j <= ti; j++) {
        scanf("%d", &tj);
        G.addedge(i, id[tj], 1);
      }
      G.addedge(S, i, 1);
    } else if (opt[i] == 1) {
      scanf("%d%d", &ti, &tj);
      link(rt, 1, m, ti, tj, i, 1);
      G.addedge(S, i, 1);
    } else {
      for (int j = 0; j < 3; j++)
        scanf("%d", &val[i][j]), G.addedge(i, id[val[i][j]], 1);
      G.addedge(S, i, 2);
    }
  }
  G.dinic();
  int ans = 0;
  operate(rt, 1, m);
  for (int i = 1; i <= m; i++) cnt[ANS[i]]++, ans += (ANS[i] != 0);
  for (int i = 1; i <= n; i++) {
    if (opt[i] == 2) {
      if (cnt[i] == 1) {
        for (int j = 0; j < 3; j++) {
          if (ANS[val[i][j]] != i) {
            ANS[val[i][j]] = i;
            break;
          }
        }
      }
    }
  }
  printf("%d\n", ans);
  for (int i = 1; i <= m; i++)
    if (ANS[i]) printf("%d %d\n", ANS[i], i);
  return 0;
}