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;
const int N = 1e5;
const int inf = 1e4;
struct edge {
  int to, flow, lnk;
  edge(int new_to, int new_flow, int new_lnk)
      : to(new_to), lnk(new_lnk), flow(new_flow) {}
};
vector<pair<int, int> > seg[N];
int part[N];
int s = 0, t = 1;
int pt[N];
int vr[N], taken[N], lf[N], rg[N], ship[N], cur[N], lnk[N], cnt, q[N], cut[N],
    mark[N], used[N], d[N], weapon[N];
vector<edge> ed[N];
void add(int u, int v, int flow) {
  ed[u].push_back(edge(v, flow, ed[v].size()));
  ed[v].push_back(edge(u, 0, ed[u].size() - 1));
}
void build(int v, int l, int r, int &cnt) {
  cnt++;
  vr[v] = cnt;
  if (l == r) {
    lnk[l] = cnt;
    ship[cnt] = l;
    add(cnt, t, 1);
    return;
  }
  int mid = (r + l) / 2;
  add(vr[v], cnt + 1, mid - l + 1);
  build(v * 2, l, mid, cnt);
  add(vr[v], cnt + 1, r - mid);
  build(v * 2 + 1, mid + 1, r, cnt);
}
void go(int v, int l, int r, int ll, int rr, int cnt) {
  if (ll <= l && r <= rr) {
    add(cnt, vr[v], 1);
    return;
  }
  int mid = (r + l) / 2;
  if (ll <= mid) go(v * 2, l, mid, ll, rr, cnt);
  if (rr > mid) go(v * 2 + 1, mid + 1, r, ll, rr, cnt);
}
bool bfs(int s, int t) {
  int l = 1;
  int sz = 1;
  q[sz] = s;
  d[s] = 0;
  while (l <= sz) {
    int v = q[l];
    for (auto to : ed[v]) {
      if (to.flow == 0) continue;
      if (d[to.to] > d[v] + 1) {
        d[to.to] = d[v] + 1;
        sz++;
        q[sz] = to.to;
      }
    }
    l++;
  }
  return (d[t] != cnt + 1);
}
int dfs(int v, int t, int flow) {
  if (v == t) return flow;
  for (; cur[v] < ed[v].size(); cur[v]++) {
    auto to = ed[v][cur[v]];
    if (to.flow == 0) continue;
    if (d[to.to] != d[v] + 1) continue;
    int p = dfs(to.to, t, min(flow, to.flow));
    if (p == 0) continue;
    ed[v][cur[v]].flow -= p;
    ed[to.to][ed[v][cur[v]].lnk].flow += p;
    return p;
  }
  return 0;
}
void flow(int s, int t) {
  int sum = 0;
  while (1) {
    for (int i = 0; i <= cnt; i++) d[i] = cnt + 1, cur[i] = 0;
    if (!bfs(s, t)) break;
    int p = dfs(s, t, inf);
    sum += p;
    while (p > 0) {
      p = dfs(s, t, inf);
      sum += p;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  cnt = 1;
  build(1, 1, m, cnt);
  for (int i = 1; i <= n; i++) {
    int tp;
    cin >> tp;
    cnt++;
    mark[i] = tp;
    weapon[cnt] = i;
    if (tp == 0) {
      add(s, cnt, 1);
      int sz;
      cin >> sz;
      for (int j = 1; j <= sz; j++) {
        int to;
        cin >> to;
        add(cnt, lnk[to], 1);
      }
    } else if (tp == 1) {
      int l, r;
      add(s, cnt, 1);
      cin >> l >> r;
      lf[i] = l, rg[i] = r;
      go(1, 1, m, l, r, cnt);
    } else {
      int a, b, c;
      cin >> a >> b >> c;
      add(s, cnt, 2);
      add(cnt, lnk[a], 1);
      add(cnt, lnk[b], 1);
      add(cnt, lnk[c], 1);
    }
  }
  flow(s, t);
  for (int i = 0; i <= cnt; i++) used[i] = 0;
  for (int i = 1; i <= cnt; i++) {
    for (auto to : ed[i]) {
      if (to.to == t && to.flow == 0) part[i] = 1;
    }
  }
  for (auto to : ed[0]) {
    if (ed[to.to][to.lnk].flow != 0) used[to.to] = 1;
  }
  vector<pair<int, int> > ans;
  for (int i = 0; i <= cnt; i++)
    if (used[i] == 1) {
      int v = weapon[i];
      if (mark[v] != 2) continue;
      int need = 0;
      for (auto to : ed[i]) {
        if (to.to == 0) continue;
        if (to.flow == 0) {
          need++;
          taken[to.to] = 1;
          ans.push_back(make_pair(v, ship[to.to]));
        }
      }
      if (need == 1) {
        for (auto to : ed[i]) {
          if (to.to == 0) continue;
          if (taken[to.to]) continue;
          if (!part[to.to]) continue;
          need++;
          taken[to.to] = 1;
          ans.push_back(make_pair(v, ship[to.to]));
          break;
        }
        if (need == 1) ans.pop_back();
      }
    }
  for (int i = 0; i <= cnt; i++)
    if (used[i] == 1) {
      int v = weapon[i];
      if (mark[v] != 0) continue;
      for (auto to : ed[i]) {
        if (to.to == 0) continue;
        if (taken[to.to]) continue;
        if (to.flow == 0) {
          taken[to.to] = 1;
          ans.push_back(make_pair(v, ship[to.to]));
        }
      }
    }
  for (int i = 0; i <= cnt; i++)
    if (used[i] == 1) {
      int v = weapon[i];
      if (mark[v] != 1) continue;
      seg[rg[v]].push_back(make_pair(lf[v], v));
    }
  for (int i = 0; i <= cnt; i++) {
    if (taken[i] == 1) continue;
    bool ok = 0;
    for (auto to : ed[i]) {
      if (to.to != t) continue;
      if (to.flow == 0) pt[ship[i]] = 1;
    }
  }
  set<int> s;
  for (int i = 1; i <= m; i++) {
    if (pt[i]) s.insert(i);
    for (auto it : seg[i]) {
      int l = it.first;
      auto nxt = s.lower_bound(l);
      if (s.end() == nxt) continue;
      ans.push_back(make_pair(it.second, *nxt));
      s.erase(nxt);
    }
  }
  cout << ans.size() << "\n";
  for (auto p : ans) cout << p.first << " " << p.second << "\n";
  return 0;
}