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 JAVA):

Solution:
//package com.company;

import java.io.*;
import java.util.*;

public class Main {
    public static class Task {

        public class Maxflow {
            class Edge {
                int t, rev;
                long cap, f;
                public Edge(int t, int rev, long cap) {
                    this.t = t;
                    this.rev = rev;
                    this.cap = cap;
                }
            }
            public Maxflow(int n) {
                graph = new List[n];
                for (int i = 0; i < n; i++) {
                    graph[i] = new ArrayList<>();
                }
            }
            List<Edge>[] graph;
            void addEdge(int s, int t, long cap) {
                graph[s].add(new Edge(t, graph[t].size(), cap));
                graph[t].add(new Edge(s, graph[s].size() - 1, 0));
//                if (cap != Integer.MAX_VALUE)
//                System.err.println(s + " " + t + " " + cap);
            }

            boolean dinicBFS(int src, int dest, int[] dist) {
                Arrays.fill(dist, -1);
                dist[src] = 0;
                int[] Q = new int[graph.length];
                int sizeQ = 0;
                Q[sizeQ++] = src;
                for (int i = 0; i < sizeQ; i++) {
                    int u = Q[i];
                    for (Edge e: graph[u]) {
                        if (dist[e.t] < 0 && e.f < e.cap) {
                            dist[e.t] = dist[u] + 1;
                            Q[sizeQ++] = e.t;
                        }
                    }
                }
                return dist[dest] >= 0;
            }

            long dinicDFS(int[] ptr, int[] dist, int dest, int u, long f) {
                if (u == dest) return f;
                for (;ptr[u] < graph[u].size(); ++ptr[u]) {
                    Edge e =  graph[u].get(ptr[u]);
                    if (dist[e.t] == dist[u] + 1 && e.f < e.cap) {
                        long df = dinicDFS(ptr, dist, dest, e.t, Math.min(f, e.cap - e.f));
                        if (df > 0) {
                            e.f += df;
                            graph[e.t].get(e.rev).f -= df;
                            return df;
                        }
                    }
                }
                return 0;
            }

            long maxFLow(int src, int dest) {
                long flow = 0;
                int[] dist = new int[graph.length];
                while (dinicBFS(src, dest, dist)) {
                    int[] ptr = new int[graph.length];
                    while (true) {
                        long df = dinicDFS(ptr, dist, dest, src, Long.MAX_VALUE);
                        if (df == 0) break;
                        flow += df;
                    }
                }
                return flow;
            }
        }

        int interValIndex;
        public class IntervalNode {
            int l, r;
            IntervalNode left, right;
            int index;
            public IntervalNode(int L, int R) {
                this.l = L;
                this.r = R;
                this.index = interValIndex++;
                int mid = (l + r) / 2;
                if (mid > l) {
                    left = new IntervalNode(l, mid);
                    right = new IntervalNode(mid, r);
                }
            }
        }
        int mfEnd;
        IntervalNode[] baseNodes;
        public void dfsAddEdge(Maxflow mf, IntervalNode node) {
            if (node.left != null) {
                mf.addEdge(node.index, node.left.index, Integer.MAX_VALUE / 2);
                mf.addEdge(node.index, node.right.index, Integer.MAX_VALUE / 2);
                dfsAddEdge(mf, node.left);
                dfsAddEdge(mf, node.right);
            } else {
                baseNodes[node.l] = node;
                mf.addEdge(node.index, mfEnd, 1);
            }
        }

        public void searchCover(Maxflow mf, IntervalNode node, int l, int r, int start) {
            if (l <= node.l && node.r <= r) {
                mf.addEdge(start, node.index, 1);
                return;
            }
            if (node.l >= r || node.r <= l) return;
            searchCover(mf, node.left, l, r, start);
            searchCover(mf, node.right, l, r, start);
        }

        public int dfsDecode(List<Maxflow.Edge>[] graph, int u) {
            for (Maxflow.Edge e: graph[u]) {
                if (e.f > 0) {
                    if (e.t == mfEnd) {
                        e.f -= 1;
                        return u;
                    }
                    int ret = dfsDecode(graph, e.t);
                    if (ret > 0) {
                        e.f -= 1;
                        return ret;
                    }
                }
            }
            return -1;
        }

        public void solve(Scanner sc, PrintWriter pw) throws IOException {
            int n = sc.nextInt();
            int m = sc.nextInt();
            interValIndex = n + 1;
            IntervalNode root = new IntervalNode(0, m);
            mfEnd = interValIndex;
            Maxflow mf = new Maxflow(mfEnd + 1);
            baseNodes = new IntervalNode[m];
            dfsAddEdge(mf, root);
            int[][] weapons = new int[n][];
            for (int i = 0; i < n; i++) {
                int type = sc.nextInt();
                if (type == 0) {
                    int k = sc.nextInt();
                    weapons[i] = new int[k + 1];
                    weapons[i][0] = type;
                    for (int j = 1; j <= k; j++) {
                        weapons[i][j] = sc.nextInt() - 1;
                    }
                    mf.addEdge(0, i + 1, 1);
                    for (int j = 1; j <= k; j++) {
                        mf.addEdge(i + 1, baseNodes[weapons[i][j]].index, 1);
                    }
                } else if (type == 1) {
                    int l = sc.nextInt() - 1;
                    int r = sc.nextInt();
                    weapons[i] = new int[3];
                    weapons[i][0] = type;
                    weapons[i][1] = l;
                    weapons[i][2] = r;
                    mf.addEdge(0, i + 1, 1);
                    searchCover(mf, root, l, r, 1 + i);
                } else if (type == 2) {
                    int a = sc.nextInt() - 1;
                    int b = sc.nextInt() - 1;
                    int c = sc.nextInt() - 1;
                    weapons[i] = new int[4];
                    weapons[i][0] = type;
                    weapons[i][1] = a;
                    weapons[i][2] = b;
                    weapons[i][2] = c;
                    mf.addEdge(0, i + 1, 2);
                    mf.addEdge(i + 1, baseNodes[a].index, 1);
                    mf.addEdge(i + 1, baseNodes[b].index, 1);
                    mf.addEdge(i + 1, baseNodes[c].index, 1);
                }
            }
            long flow = mf.maxFLow(0, mfEnd);
            pw.println(flow);
            List<Maxflow.Edge>[] graph = mf.graph;
            Map<Integer, Integer> toIndex = new HashMap<>();
            for (int i = 0; i < baseNodes.length; i++) {
                toIndex.put(baseNodes[i].index, i);
            }
            toIndex.put(-1, -1);
            int[][] connectTo = new int[n][2];
            for (int i = 0; i < n; i++) {
                connectTo[i][1] = -1;
                connectTo[i][0] = toIndex.get(dfsDecode(graph, i + 1));
                if (weapons[i][0] == 2) connectTo[i][1] = toIndex.get(dfsDecode(graph, i + 1));
            }
            int[] whose = new int[m];
            for (int i = 0; i < n; i++) {
                if (weapons[i][0] != 2 && connectTo[i][0] != -1) {
                    whose[connectTo[i][0]] = i;
                }
            }
            for (int i = 0; i < n; i++) {
                if (weapons[i][0] == 2 && connectTo[i][1] == -1 && connectTo[i][0] != -1) {
                    for (int j = 1; j < 4; j++) {
                        if (connectTo[i][0] != weapons[i][j]) {
                            connectTo[i][1] = weapons[i][j];
                            connectTo[whose[weapons[i][j]]][0] = -1;
                            break;
                        }
                    }
                }
            }
            for (int i = 0; i < n; i++) {
                if (connectTo[i][0] >= 0) {
                    pw.println((i + 1) + " " + (connectTo[i][0] + 1));
                }
                if (connectTo[i][1] >= 0) {
                    pw.println((i + 1) + " " + (connectTo[i][1] + 1));
                }
            }
        }
    }

    static long TIME_START, TIME_END;

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
//        Scanner sc = new Scanner(new FileInputStream("input"));
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
//        PrintWriter pw = new PrintWriter(new FileOutputStream("output"));

        Runtime runtime = Runtime.getRuntime();
        long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory();
        TIME_START = System.currentTimeMillis();
        Task t = new Task();
        t.solve(sc, pw);
        TIME_END = System.currentTimeMillis();
        long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory();
        pw.close();
        System.err.println("Memory increased: " + (usedMemoryAfter - usedMemoryBefore) / 1000000);
        System.err.println("Time used: " + (TIME_END - TIME_START) + ".");
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public Scanner(FileReader s) throws FileNotFoundException {
            br = new BufferedReader(s);
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        public boolean ready() throws IOException {
            return br.ready();
        }
    }
}