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 namespace std::chrono;
vector<vector<pair<int, int>>> g(50000);
vector<int> vertex(50000, 0);
vector<int> arr(50000, -1);
vector<int> ll(50000, -1);
vector<int> rr(50000, -1);
int S, F;
int cnt = 0;
int f[1000000];
int c[1000000];
int z = 15000;
vector<int> ans;
vector<bool> dont_go;
int dfs(int v, vector<vector<pair<int, int>>>& g, vector<int>& dist,
        int delta) {
  if (v == F) return delta;
  for (pair<int, int> x : g[v]) {
    int u = x.first;
    int j = x.second;
    if (c[j] - f[j] != 0 && dist[u] == dist[v] + 1 && !dont_go[j]) {
      int k = dfs(u, g, dist, min(delta, c[j] - f[j]));
      if (k != 0 && k != 1e9) {
        f[j] += k;
        f[j ^ 1] -= k;
        return k;
      } else {
        dont_go[j] = true;
      }
    }
  }
  return 0;
}
int upd(vector<vector<pair<int, int>>>& g) {
  dont_go.assign(cnt, false);
  int n = g.size();
  vector<int> dist(30000, 1e9);
  deque<int> q;
  dist[S] = 0;
  q.push_back(S);
  while (!q.empty()) {
    int v = q.front();
    q.pop_front();
    for (pair<int, int> x : g[v]) {
      int u = x.first;
      int i = x.second;
      if (c[i] - f[i] != 0) {
        if (dist[u] > dist[v] + 1) {
          dist[u] = dist[v] + 1;
          q.push_back(u);
        }
      }
    }
  }
  if (dist[F] == 1e9) {
    return 0;
  }
  vector<vector<pair<int, int>>> g1(30000);
  for (int i = 0; i < n; ++i) {
    for (pair<int, int> x : g[i]) {
      int u = x.first;
      int j = x.second;
      if (c[j] - f[j] != 0 && dist[i] + 1 == dist[u]) {
        g1[i].push_back(x);
      }
    }
  }
  int add = 0;
  while (true) {
    int k = dfs(S, g1, dist, 1e9);
    if (k != 0 && k != 1e9) {
      add += k;
    } else {
      break;
    }
  }
  return add;
}
void decompose(int i, int l, int r, int q_l, int q_r, int n) {
  if (q_l <= l && r <= q_r) {
    ans.push_back(i + n);
    return;
  }
  if (r <= q_l || q_r <= l) return;
  int m = (l + r) / 2;
  decompose(i * 2 + 1, l, m, q_l, q_r, n);
  decompose(i * 2 + 2, m, r, q_l, q_r, n);
}
void build(int i, int l, int r, int n) {
  ll[i + n] = l;
  rr[i + n] = r;
  if (l + 1 == r) {
    vertex[l] = i + n;
    arr[i + n] = l;
    return;
  }
  int m = (l + r) / 2;
  build(i * 2 + 1, l, m, n);
  build(i * 2 + 2, m, r, n);
  if (l + 1 == m) {
    c[cnt] = 1;
  } else {
    c[cnt] = 1e9;
  }
  c[cnt + 1] = 0;
  if (m + 1 == r) {
    c[cnt + 2] = 1;
  } else {
    c[cnt + 2] = 1e9;
  }
  c[cnt + 3] = 0;
  g[i + n].push_back({i * 2 + 1 + n, cnt++});
  g[i * 2 + 1 + n].push_back({i + n, cnt++});
  g[i + n].push_back({i * 2 + 2 + n, cnt++});
  g[i * 2 + 2 + n].push_back({i + n, cnt++});
}
void solve() {
  int n, m;
  cin >> n >> m;
  S = 29998;
  F = 29999;
  vertex.assign(m, 0);
  build(0, 0, m, n);
  vector<bool> sql(n, false);
  vector<bool> stupid(n, false);
  vector<vector<int>> omg_par(n);
  for (int i = 0; i < n; ++i) {
    int type;
    cin >> type;
    if (type == 0) {
      int k;
      cin >> k;
      for (int j = 0; j < k; ++j) {
        int x;
        cin >> x;
        --x;
        x = vertex[x];
        c[cnt] = 1;
        c[cnt + 1] = 0;
        g[i].push_back({x, cnt++});
        g[x].push_back({i, cnt++});
      }
    } else if (type == 1) {
      stupid[i] = true;
      int l, r;
      cin >> l >> r;
      --l;
      ans.clear();
      decompose(0, 0, m, l, r, n);
      for (int j : ans) {
        c[cnt] = 1;
        c[cnt + 1] = 0;
        g[i].push_back({j, cnt++});
        g[j].push_back({i, cnt++});
      }
    } else {
      sql[i] = true;
      int u, v, w;
      cin >> u >> v >> w;
      --u;
      --v;
      --w;
      u = vertex[u];
      v = vertex[v];
      w = vertex[w];
      omg_par[i].push_back(u);
      omg_par[i].push_back(v);
      omg_par[i].push_back(w);
      c[cnt] = 1;
      c[cnt + 1] = 0;
      c[cnt + 2] = 1;
      c[cnt + 3] = 0;
      c[cnt + 4] = 1;
      c[cnt + 5] = 0;
      g[i].push_back({u, cnt++});
      g[u].push_back({i, cnt++});
      g[i].push_back({v, cnt++});
      g[v].push_back({i, cnt++});
      g[i].push_back({w, cnt++});
      g[w].push_back({i, cnt++});
    }
  }
  for (int i = 0; i < n; ++i) {
    c[cnt] = 1;
    if (sql[i]) {
      c[cnt] = 2;
    }
    c[cnt + 1] = 0;
    g[S].push_back({i, cnt++});
    g[i].push_back({S, cnt++});
  }
  for (int i : vertex) {
    c[cnt] = 1;
    c[cnt + 1] = 0;
    g[i].push_back({F, cnt++});
    g[F].push_back({i, cnt++});
  }
  int ans = 0;
  while (true) {
    int k = upd(g);
    ans += k;
    if (k == 0) {
      break;
    }
  }
  cout << ans << '\n';
  vector<bool> can_shoot(m, false);
  vector<pair<int, int>> answ;
  for (int i = 0; i < m; ++i) {
    int u = vertex[i];
    for (pair<int, int> x : g[u]) {
      int j = x.second;
      if (x.first != F) continue;
      if (c[j] - f[j] == 0 && c[j] == 1) {
        can_shoot[i] = true;
      }
    }
  }
  int count2 = 0;
  for (int i : can_shoot) {
    count2 += i;
  }
  assert(count2 == ans);
  for (int i = 0; i < n; ++i) {
    if (!sql[i] && !stupid[i]) {
      for (pair<int, int> x : g[i]) {
        int u = x.first;
        int j = x.second;
        if (c[j] - f[j] == 0 && c[j] == 1) {
          answ.push_back({i, arr[u]});
          can_shoot[arr[u]] = false;
        }
      }
    }
  }
  vector<pair<int, int>> answ1;
  for (int i = 0; i < n; ++i) {
    if (sql[i]) {
      for (pair<int, int> x : g[i]) {
        int u = x.first;
        int j = x.second;
        if (c[j] - f[j] == 0 && c[j] == 1) {
          answ1.push_back({i, arr[u]});
          can_shoot[arr[u]] = false;
        }
      }
    }
  }
  int k = answ.size() + answ1.size();
  int left = ans - k;
  vector<pair<int, pair<int, int>>> t;
  for (int i = 0; i < n; ++i) {
    if (stupid[i]) {
      for (pair<int, int> x : g[i]) {
        int u = x.first;
        int j = x.second;
        if (c[j] - f[j] == 0 && c[j] == 1) {
          t.push_back({i, {ll[u], rr[u] - 1}});
        }
      }
    }
  }
  vector<pair<pair<int, int>, pair<int, int>>> t1;
  for (pair<int, pair<int, int>> x : t) {
    t1.push_back(
        {{x.second.first, -1}, {-(x.second.second - x.second.first), x.first}});
    t1.push_back(
        {{x.second.second, 1}, {-(x.second.second - x.second.first), x.first}});
  }
  assert(left == t.size());
  for (int i = 0; i < m; ++i) {
    if (can_shoot[i]) {
      t1.push_back({{i, 0}, {i, i}});
      --left;
    }
  }
  assert(left == 0);
  sort(t1.begin(), t1.end());
  vector<int> st;
  vector<int> anss(n, -1);
  for (int i = 0; i < t1.size(); ++i) {
    if (t1[i].first.second == -1) {
      st.push_back(t1[i].second.second);
    } else if (t1[i].first.second == 1) {
      if (st.size() && st.back() == t1[i].second.second) {
        st.pop_back();
      }
    } else {
      anss[st.back()] = t1[i].first.first;
      st.pop_back();
    }
  }
  vector<pair<int, int>> answ2;
  for (int i = 0; i < n; ++i) {
    if (anss[i] != -1) {
      answ2.push_back({i, anss[i]});
    }
  }
  vector<vector<int>> to_par(n);
  vector<int> wh(m, -1);
  int count1 = 0;
  for (pair<int, int> x : answ) {
    to_par[x.first].push_back(x.second);
    wh[x.second] = x.first;
    ++count1;
  }
  for (pair<int, int> x : answ1) {
    to_par[x.first].push_back(x.second);
    wh[x.second] = x.first;
    ++count1;
  }
  for (pair<int, int> x : answ2) {
    to_par[x.first].push_back(x.second);
    wh[x.second] = x.first;
    ++count1;
  }
  assert(count1 == ans);
  vector<pair<int, int>> f_ans;
  for (int i = 0; i < n; ++i) {
    if (sql[i]) {
      if (to_par[i].size() == 1) {
        omg_par[i].erase(
            find(omg_par[i].begin(), omg_par[i].end(), vertex[to_par[i][0]]));
        to_par[i].push_back(arr[omg_par[i][0]]);
      }
    }
  }
  vector<bool> used(m, false);
  int count = 0;
  for (int i = 0; i < n; ++i) {
    if (sql[i]) {
      for (int j : to_par[i]) {
        cout << i + 1 << ' ' << j + 1 << '\n';
        used[j] = true;
        ++count;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    if (!sql[i]) {
      for (int j : to_par[i]) {
        if (!used[j]) {
          cout << i + 1 << ' ' << j + 1 << '\n';
          used[j] = true;
          ++count;
        }
      }
    }
  }
  assert(count == ans);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  auto start = system_clock::now();
  solve();
  auto end = system_clock::now();
  cerr << "Time: " << duration_cast<milliseconds>(end - start).count() << "ms"
       << endl;
}