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>
const static int INF = std::numeric_limits<int>::max() / 3;
struct DinicFlow {
  struct edge {
    int a, b;
    int flow, cap;
    edge* rev;
    edge(const int& _a, const int& _b, const int& _flow, const int& _cap,
         edge* _rev = nullptr)
        : a(_a), b(_b), flow(_flow), cap(_cap), rev(_rev) {}
  };
  typedef std::vector<std::vector<edge*>> adj_list;
  std::vector<char> blocked;
  std::vector<int> distances;
  adj_list graph;
  int n;
  int source;
  int sink;
  DinicFlow(const int& _n) : n(_n) {
    source = n++;
    sink = n++;
    blocked.assign(n, false);
    distances.assign(n, INF);
    graph.assign(n, std::vector<edge*>());
  }
  ~DinicFlow(void) {
    for (auto& row : graph) {
      for (auto& e : row) {
        delete e;
      }
    }
  }
  edge* add_edge(const int& a, const int& b, const int& cap,
                 const int& flow = 0) {
    edge* normal = new edge(a, b, flow, cap);
    edge* reverse = normal->rev = new edge(b, a, 0, 0, normal);
    graph[a].push_back(normal);
    graph[b].push_back(reverse);
    return normal;
  }
  bool bfs(void) {
    distances.assign(n, INF);
    distances[sink] = 0;
    std::queue<int> order;
    order.push(sink);
    while (!order.empty()) {
      int best = order.front();
      order.pop();
      if (best == source) return true;
      for (edge* neigh : graph[best]) {
        if (neigh->rev->cap > neigh->rev->flow &&
            distances[neigh->b] > 1 + distances[best]) {
          distances[neigh->b] = 1 + distances[best];
          order.push(neigh->b);
        }
      }
    }
    return false;
  }
  int dfs(const int& pos, const int& min) {
    if (pos == sink) {
      return min;
    }
    int flow = 0;
    for (edge* e : graph[pos]) {
      int curr = 0;
      if (!blocked[e->b] && distances[e->b] == distances[pos] - 1 &&
          e->cap > e->flow) {
        curr = dfs(e->b, std::min(min - flow, e->cap - e->flow));
        e->flow += curr;
        e->rev->flow = -(e->flow);
        flow += curr;
      }
      if (flow == min) return flow;
    }
    blocked[pos] = flow != min;
    return flow;
  }
  int flow(void) {
    for (auto& row : graph) {
      for (edge* e : row) e->flow = 0;
    }
    int ret = 0;
    while (bfs()) {
      blocked.assign(blocked.size(), false);
      ret += dfs(source, INF);
    }
    return ret;
  }
};
DinicFlow::edge* yeet[5007];
std::pair<int, int> inv[4 * 5007];
struct segment_tree {
  const int n;
  int* node;
  int* leaf;
  int cnt;
  segment_tree(const int& _n) : n(_n) {
    node = new int[4 * n + 1];
    leaf = new int[n + 1];
    cnt = 0;
  }
  ~segment_tree(void) {
    delete[] node;
    delete[] leaf;
  }
  void build(const int& i, const int& rl, const int& rr, DinicFlow& df) {
    node[i] = cnt++;
    inv[node[i]] = {rl, rr};
    if (rl == rr) {
      leaf[rl] = node[i];
      yeet[rl] = df.add_edge(leaf[rl], df.sink, 1);
      return;
    }
    int mid = (rl + rr) / 2;
    build(2 * i + 1, rl, mid, df);
    build(2 * i + 2, mid + 1, rr, df);
    df.add_edge(node[i], node[2 * i + 1], 5001);
    df.add_edge(node[i], node[2 * i + 2], 5001);
  }
  void add_edge(const int& i, const int& rl, const int& rr, const int& left,
                const int& right, DinicFlow& df, const int& other) {
    if (left == rl && right == rr) {
      df.add_edge(other, node[i], 5001);
      return;
    }
    int mid = (rl + rr) / 2;
    if (left <= mid)
      add_edge(2 * i + 1, rl, mid, left, std::min(mid, right), df, other);
    if (mid < right)
      add_edge(2 * i + 2, mid + 1, rr, std::max(left, mid + 1), right, df,
               other);
  }
};
int n, m;
struct bg_query {
  int t;
  int a, b, c, l, r;
  DinicFlow::edge* e;
  int sz;
  int* stuff;
  DinicFlow::edge** fe;
} type[5007];
int match[5007];
int main(void) {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  std::cin >> m >> n;
  DinicFlow df(4 * n + 2 + m);
  segment_tree tree(n);
  tree.build(0, 0, n - 1, df);
  for (int i = 0; i < m; ++i) {
    std::cin >> type[i].t;
    type[i].e = df.add_edge(df.source, tree.cnt + i, 1 + (type[i].t == 2));
    if (type[i].t == 0) {
      std::cin >> type[i].sz;
      type[i].stuff = new int[type[i].sz];
      type[i].fe = new DinicFlow::edge*[type[i].sz];
      for (int j = 0; j < type[i].sz; ++j) {
        std::cin >> type[i].stuff[j];
        type[i].stuff[j]--;
        type[i].fe[j] =
            df.add_edge(tree.cnt + i, tree.leaf[type[i].stuff[j]], 1);
      }
    } else if (type[i].t == 1) {
      std::cin >> type[i].l >> type[i].r;
      tree.add_edge(0, 0, n - 1, --type[i].l, --type[i].r, df, tree.cnt + i);
    } else {
      std::cin >> type[i].a >> type[i].b >> type[i].c;
      df.add_edge(tree.cnt + i, tree.leaf[--type[i].a], 1);
      df.add_edge(tree.cnt + i, tree.leaf[--type[i].b], 1);
      df.add_edge(tree.cnt + i, tree.leaf[--type[i].c], 1);
    }
  }
  int ans = df.flow();
  memset(match, 0xff, sizeof(match));
  for (int i = 0; i < m; ++i) {
    if (type[i].t != 2) continue;
    int amt = type[i].e->flow;
    if (amt == 1) {
      for (const auto& nei : df.graph[tree.cnt + i])
        if (nei->b != df.source && nei->flow) {
          match[inv[nei->b].first] = i;
          break;
        }
      if (yeet[type[i].a]->flow && match[type[i].a] < 0)
        match[type[i].a] = i;
      else if (yeet[type[i].b]->flow && match[type[i].b] < 0)
        match[type[i].b] = i;
      else if (yeet[type[i].c]->flow && match[type[i].c] < 0)
        match[type[i].c] = i;
    } else if (amt == 2) {
      for (const auto& nei : df.graph[tree.cnt + i])
        if (nei->b != df.source && nei->flow) match[inv[nei->b].first] = i;
    }
  }
  for (int i = 0; i < m; ++i) {
    if (type[i].t != 0) continue;
    const int sz = type[i].sz;
    for (int j = 0; j < sz; ++j)
      if (match[type[i].stuff[j]] < 0 && type[i].fe[j]->flow) {
        match[type[i].stuff[j]] = i;
        break;
      }
  }
  for (int i = 0; i < m; ++i) {
    if (type[i].t != 1 || !type[i].e->flow) continue;
    int rl = type[i].l, rr = type[i].r;
    int cur_node = tree.cnt + i;
    while (rl < rr) {
      for (auto& nei : df.graph[cur_node])
        if (nei->b != df.source && nei->flow) {
          --nei->flow;
          rl = inv[nei->b].first;
          rr = inv[nei->b].second;
          cur_node = nei->b;
          break;
        }
    }
    if (match[rl] < 0) match[rl] = i;
  }
  std::cout << ans << "\n";
  for (int i = 0; i < n; ++i)
    if (match[i] >= 0) std::cout << match[i] + 1 << " " << i + 1 << "\n";
  return 0;
}