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;
const int N = 1e6;
const int mod = 1e9 + 7;
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0', c = getchar();
  return x * f;
}
struct Edge {
  int to, w, nxt;
  Edge() {}
  Edge(int v, int c, int t) : to(v), w(c), nxt(t) {}
};
const int EN = 2e5 + 10;
const int EM = 1e6 + 10;
int pos[EN];
int st[EN];
vector<int> ans[EN];
struct Dinic {
  Edge e[EM << 1];
  int head[EN], scnt, d[EN], cur[EN];
  pair<int, int> pre[EN];
  int vis[EN];
  Dinic() { scnt = 1; }
  void addEdge(int u, int v, int w) {
    e[++scnt] = Edge(v, w, head[u]);
    head[u] = scnt;
    e[++scnt] = Edge(u, 0, head[v]);
    head[v] = scnt;
  }
  bool bfs(int s, int t) {
    queue<int> q;
    memset(d, 0, sizeof(d));
    q.push(s);
    d[s] = 1;
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      for (int i = head[x]; i; i = e[i].nxt) {
        int to = e[i].to;
        if (!d[to] && e[i].w) {
          d[to] = d[x] + 1;
          q.push(to);
          if (to == t) return true;
        }
      }
    }
    return false;
  }
  int dfs(int x, int t, int flow) {
    if (x == t) return flow;
    int res = 0;
    for (int i = cur[x]; i; i = e[i].nxt) {
      cur[x] = i;
      int to = e[i].to;
      if (d[to] == d[x] + 1 && e[i].w) {
        int dis = dfs(to, t, min(flow, e[i].w));
        if (dis) {
          e[i].w -= dis;
          e[i ^ 1].w += dis;
          flow -= dis;
          res += dis;
          if (!flow) return res;
        }
      }
    }
    return res;
  }
  int Maxflow(int s, int t) {
    int ans = 0;
    while (bfs(s, t)) {
      memcpy(cur, head, sizeof(head));
      ans += dfs(s, t, 0x3f3f3f3f);
    }
    return ans;
  }
  int dfs(int x, int pre) {
    if (pos[x] != 0) return pos[x];
    for (int i = head[x]; i; i = e[i].nxt) {
      if ((i & 1) && e[i].w && e[i].to != pre) {
        int k = dfs(e[i].to, x);
        e[i].w--;
        return k;
      }
    }
  }
} dc;
int p[EN];
struct Segment {
  int id[N], vis[N], out[N], in[N];
  int scnt;
  void init(int pos, int l, int r, int fa) {
    id[pos] = ++scnt;
    if (fa != -1) dc.addEdge(id[fa], id[pos], 0x3f3f3f3f);
    if (l == r) {
      p[l] = pos;
      return;
    }
    int mid = (l + r) >> 1;
    init(pos << 1, l, mid, pos);
    init(pos << 1 | 1, mid + 1, r, pos);
  }
  void add(int ql, int qr, int pos, int l, int r, int x) {
    if (ql <= l && r <= qr) {
      dc.addEdge(x, id[pos], 0x3f3f3f3f);
      return;
    }
    int mid = (l + r) >> 1;
    if (ql <= mid) add(ql, qr, pos << 1, l, mid, x);
    if (qr > mid) add(ql, qr, pos << 1 | 1, mid + 1, r, x);
  }
} T;
int y[N], op[N];
void find(int t) {
  for (int i = dc.head[t]; i; i = dc.e[i].nxt) {
    if (dc.e[i].w) {
      int x = dc.dfs(dc.e[i].to, t), k = y[dc.e[i].to];
      st[k] = x;
      ans[x].push_back(k);
    }
  }
}
int f[N][3];
int main() {
  int m = read(), n = read();
  T.init(1, 1, n, -1);
  int cnt = T.scnt;
  int s = ++cnt, t = ++cnt;
  for (int i = 1; i <= n; i++) dc.addEdge(T.id[p[i]], t, 1), y[T.id[p[i]]] = i;
  for (int i = 1; i <= m; i++) {
    op[i] = read();
    int q = ++cnt;
    pos[cnt] = i;
    if (op[i] == 0) {
      dc.addEdge(s, q, 1);
      int k = read();
      for (int j = 1; j <= k; j++) {
        int b = read();
        dc.addEdge(q, T.id[p[b]], 1);
      }
    }
    if (op[i] == 2) {
      dc.addEdge(s, q, 2);
      for (int j = 0; j < 3; j++) {
        int b = read();
        f[i][j] = b;
        dc.addEdge(q, T.id[p[b]], 1);
      }
    }
    if (op[i] == 1) {
      dc.addEdge(s, q, 1);
      int l = read(), r = read();
      T.add(l, r, 1, 1, n, q);
    }
  }
  printf("%d\n", dc.Maxflow(s, t));
  find(t);
  for (int i = 1; i <= m; i++) {
    if (op[i] == 2 && ans[i].size() == 1) {
      for (int j = 0; j < 3; j++) {
        if (st[f[i][j]] && st[f[i][j]] != i) {
          st[f[i][j]] = i;
          break;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (st[i]) printf("%d %d\n", st[i], i);
  }
}