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;
using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;
struct Edge {
  int end;
  i64 capa, flow;
  Edge* dual;
  Edge() : Edge(-1, 0) {}
  Edge(int e, i64 c) : end(e), capa(c), flow(0), dual(nullptr) {}
  i64 remain() { return capa - flow; }
  void add_flow(i64 f) {
    flow += f;
    dual->flow -= f;
  }
};
template <int Size>
class Dinic {
 public:
  const i64 INFINITE = 1ll << 62;
  ~Dinic() {
    for (auto& a : adj) {
      for (auto& e : a) {
        delete e;
      }
    }
  }
  Edge* add_edge(int a, int b, i64 c) {
    auto e1 = new Edge(b, c);
    auto e2 = new Edge(a, 0);
    e1->dual = e2;
    e2->dual = e1;
    adj[a].push_back(e1);
    adj[b].push_back(e2);
    return e1;
  }
  i64 max_flow(int source, int sink) {
    i64 totalFlow = 0;
    while (bfs(source, sink)) {
      memset(work, 0, sizeof(work));
      while (true) {
        i64 flow = dfs(source, sink, INFINITE);
        if (flow == 0) break;
        totalFlow += flow;
      }
    }
    return totalFlow;
  }
  int edge_count(int s) { return adj[s].size(); }
  Edge* get_edge(int s, int i) { return adj[s][i]; }

 private:
  vector<Edge*> adj[Size];
  int level[Size];
  int work[Size];
  bool bfs(int source, int sink) {
    memset(level, -1, sizeof(level));
    level[source] = 0;
    queue<int> q;
    q.push(source);
    while (!q.empty()) {
      int now = q.front();
      q.pop();
      for (auto& e : adj[now]) {
        if (level[e->end] != -1 || e->remain() <= 0) continue;
        level[e->end] = level[now] + 1;
        q.push(e->end);
      }
    }
    return level[sink] != -1;
  }
  i64 dfs(int s, int e, i64 f) {
    if (s == e) return f;
    for (int& i = work[s]; i < adj[s].size(); i++) {
      auto& next = adj[s][i];
      if (level[next->end] != level[s] + 1 || next->remain() <= 0) continue;
      i64 df = dfs(next->end, e, min(f, next->remain()));
      if (df != 0) {
        next->add_flow(df);
        return df;
      }
    }
    return 0;
  }
};
Dinic<50005> dinic;
int bcand[5005][3];
bool bdead[5005];
bool used[5005];
vector<int> bazooka;
int n, m;
int ships;
int bas;
int segs;
int beaml[5005];
int beamr[5005];
vector<ii> ls;
void seg_init(int l, int r, int idx) {
  if (l == r) {
    dinic.add_edge(segs + idx, ships + l, 1);
    return;
  }
  int mid = (l + r) / 2;
  dinic.add_edge(segs + idx, segs + idx * 2, n);
  dinic.add_edge(segs + idx, segs + idx * 2 + 1, n);
  seg_init(l, mid, idx * 2);
  seg_init(mid + 1, r, idx * 2 + 1);
}
void seg_add(int k, int nl, int nr, int l, int r, int idx) {
  if (nl <= l && r <= nr) {
    dinic.add_edge(k, segs + idx, 1);
    return;
  }
  int mid = (l + r) / 2;
  if (mid < nl) {
    seg_add(k, nl, nr, mid + 1, r, 2 * idx + 1);
    return;
  }
  if (mid + 1 > nr) {
    seg_add(k, nl, nr, l, mid, 2 * idx);
    return;
  }
  seg_add(k, nl, nr, l, mid, 2 * idx);
  seg_add(k, nl, nr, mid + 1, r, 2 * idx + 1);
}
vector<ii> rtrace[5005];
void trace() {
  for (int i = 1; i <= n; i++) {
    int c = dinic.edge_count(i);
    for (int j = 0; j < c; j++) {
      auto e = dinic.get_edge(i, j);
      if (e->flow <= 0) continue;
      if (e->end > ships && e->end <= ships + m) {
        ls.emplace_back(i, e->end - ships);
        used[e->end - ships] = true;
        break;
      }
      if (e->end > segs) {
        rtrace[beaml[i]].emplace_back(beamr[i], i);
        break;
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    int c = dinic.edge_count(ships + i);
    for (int j = 0; j < c; j++) {
      auto e = dinic.get_edge(ships + i, j);
      if (e->flow <= 0) continue;
      if (e->end >= bas && e->end < bas + bazooka.size()) {
        bdead[i] = true;
        break;
      }
    }
  }
  for (int i = 0; i < bazooka.size(); i++) {
    int k = 0;
    for (int j = 0; j < 3 && k < 2; j++) {
      if (bdead[bcand[i][j]]) continue;
      ls.emplace_back(bazooka[i], bcand[i][j]);
      used[bcand[i][j]] = true;
      k++;
    }
  }
  priority_queue<ii, vector<ii>, greater<ii>> q;
  for (int i = 1; i <= m; i++) {
    for (auto& ri : rtrace[i]) q.push(ri);
    if (used[i]) continue;
    if (q.empty()) continue;
    auto now = q.top();
    q.pop();
    ls.emplace_back(now.second, i);
  }
}
bool tob[5005];
int main() {
  scanf("%d %d", &n, &m);
  ships = n;
  bas = n + m + 1;
  segs = 2 * n + m + 1;
  seg_init(1, m, 1);
  int source = 50001, dest = 50002;
  for (int i = 1; i <= n; i++) {
    int t;
    scanf("%d", &t);
    if (t == 0) {
      dinic.add_edge(source, i, 1);
      int k;
      scanf("%d", &k);
      for (int j = 0; j < k; j++) {
        int x;
        scanf("%d", &x);
        dinic.add_edge(i, ships + x, 1);
      }
    } else if (t == 1) {
      dinic.add_edge(source, i, 1);
      scanf("%d %d", &beaml[i], &beamr[i]);
      seg_add(i, beaml[i], beamr[i], 1, m, 1);
    } else {
      int idx = bazooka.size();
      scanf("%d %d %d", &bcand[idx][0], &bcand[idx][1], &bcand[idx][2]);
      bazooka.push_back(i);
      for (int j = 0; j < 3; j++) {
        tob[bcand[idx][j]] = true;
        dinic.add_edge(ships + bcand[idx][j], bas + idx, 1);
      }
      dinic.add_edge(n + m + 1 + idx, dest, 1);
    }
  }
  for (int i = 1; i <= m; i++) {
    if (!tob[i]) dinic.add_edge(ships + i, dest, 1);
  }
  int ans = dinic.max_flow(source, dest) + bazooka.size() * 2;
  printf("%d\n", ans);
  trace();
  for (auto& li : ls) printf("%d %d\n", li.first, li.second);
  return 0;
}