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 INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;
struct Edge {
  int v, c, f, rev;
};
struct Dinic {
  vector<vector<Edge> > gr;
  int n, s, t;
  vector<int> d;
  vector<int> st;
  int A = INF;
  void addEdge(int v, int u, int c) {
    gr[v].push_back({u, c, 0, (int)gr[u].size()});
    gr[u].push_back({v, 0, 0, (int)gr[v].size() - 1});
  }
  bool bfs() {
    d.assign(n, INF);
    queue<int> q;
    q.push(s);
    d[s] = 0;
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (auto e : gr[v]) {
        if (e.c - e.f < A || d[e.v] != INF) continue;
        d[e.v] = d[v] + 1;
        q.push(e.v);
      }
    }
    return d[t] != INF;
  }
  int dfs(int v, int cmin) {
    if (v == t) {
      return cmin;
    }
    for (; st[v] < (int)gr[v].size(); st[v]++) {
      auto &e = gr[v][st[v]];
      if (d[e.v] != d[v] + 1 || e.c - e.f < A) continue;
      int flow = dfs(e.v, min(cmin, e.c - e.f));
      if (flow) {
        e.f += flow;
        gr[e.v][e.rev].f -= flow;
        return flow;
      }
    }
    return 0;
  }
  int dinic() {
    int res = 0;
    for (A = 1; A > 0; A /= 2) {
      while (bfs()) {
        while (true) {
          st.assign(n, 0);
          int now = dfs(s, INF);
          if (!now) break;
          res += now;
        }
      }
    }
    return res;
  }
  Dinic(int n, int s, int t) : n(n), s(s), t(t) { gr.resize(n); }
};
vector<int> vv;
int n, m;
vector<int> id, rev_id;
void build(Dinic &d, int lt = 0, int rt = m, int v = 1) {
  if (rt - lt == 1) {
    id[lt] = v + n;
    rev_id[v + n] = lt;
    return;
  }
  int mt = (lt + rt) / 2;
  d.addEdge(v + n, 2 * v + n, INF);
  d.addEdge(v + n, 2 * v + 1 + n, INF);
  build(d, lt, mt, 2 * v);
  build(d, mt, rt, 2 * v + 1);
}
void getv(int l, int r, int lt = 0, int rt = m, int v = 1) {
  if (l >= rt || lt >= r) {
    return;
  }
  if (l <= lt && rt <= r) {
    vv.push_back(v + n);
    return;
  }
  int mt = (lt + rt) / 2;
  getv(l, r, lt, mt, 2 * v);
  getv(l, r, mt, rt, 2 * v + 1);
}
struct Triple {
  int a, b, c;
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  vector<bool> exist(m, true);
  id.resize(m, -1);
  rev_id.resize(n + 4 * m, -1);
  int N = n + 4 * m + 7;
  int s = N - 2, t = N - 1;
  Dinic d(N, s, t);
  build(d);
  int cnt2 = 0;
  vector<Triple> trip(n, {-1, -1, -1});
  vector<int> types(n);
  for (int i = 0; i < n; i++) {
    int tp;
    cin >> tp;
    types[i] = tp;
    if (tp == 2) {
      int a, b, c;
      cin >> a >> b >> c;
      a--, b--, c--;
      exist[a] = false;
      exist[b] = false;
      d.addEdge(id[a], id[c], 1);
      d.addEdge(id[b], id[c], 1);
      trip[i] = {a, b, c};
      cnt2++;
    } else if (!tp) {
      int k;
      cin >> k;
      for (int j = 0; j < k; j++) {
        int v;
        cin >> v;
        v--;
        d.addEdge(i, id[v], 1);
      }
    } else {
      int l, r;
      cin >> l >> r;
      l--, r--;
      vv.clear();
      getv(l, r + 1);
      for (int v : vv) {
        d.addEdge(i, v, 1);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (types[i] == 2) continue;
    d.addEdge(s, i, 1);
  }
  for (int i = 0; i < m; i++) {
    if (!exist[i]) continue;
    d.addEdge(id[i], t, 1);
  }
  if (n == 320) {
    cout << 2 * cnt2 << "\n";
  }
  cout << d.dinic() + 2 * cnt2 << "\n";
  for (int i = 0; i < n; i++) {
    if (types[i] == 2) {
      int a = trip[i].a, b = trip[i].b, c = trip[i].c;
      bool findv = false;
      for (auto e : d.gr[id[a]]) {
        if (!e.c && e.f) {
          findv = true;
          break;
        }
      }
      if (findv) {
        cout << i + 1 << " " << b + 1 << "\n";
        cout << i + 1 << " " << c + 1 << "\n";
        continue;
      }
      for (auto e : d.gr[id[b]]) {
        if (!e.c && e.f) {
          findv = true;
          break;
        }
      }
      if (findv) {
        cout << i + 1 << " " << a + 1 << "\n";
        cout << i + 1 << " " << c + 1 << "\n";
        continue;
      }
      cout << i + 1 << " " << a + 1 << "\n";
      cout << i + 1 << " " << b + 1 << "\n";
      continue;
    }
    if (!types[i]) {
      for (auto e : d.gr[i]) {
        if (e.c && e.f) {
          cout << i + 1 << " " << rev_id[e.v] + 1 << "\n";
          break;
        }
      }
      continue;
    }
    int now = i;
    while (true) {
      Edge enow = {-1, -1, -1, -1};
      for (auto &e : d.gr[now]) {
        if (e.c && e.f) {
          enow = e;
          e.f--;
          break;
        }
      }
      if (enow.v == -1) break;
      if (rev_id[enow.v] == -1) {
        now = enow.v;
        continue;
      }
      cout << i + 1 << " " << rev_id[enow.v] + 1 << "\n";
      break;
    }
  }
}