Problem: There are n students in Polycarp's class (including himself). A few days ago all students wrote an essay "My best friend". Each student's essay was dedicated to one of the students of class, to his/her best friend. Note that student b's best friend is not necessarily student a, if a's best friend is b.

And now the teacher leads the whole class to the museum of the history of sports programming. Exciting stories of legendary heroes await the students: tourist, Petr, tomek, SnapDragon — that's who they will hear about!

The teacher decided to divide students into pairs so that each pair consisted of a student and his best friend. She may not be able to split all the students into pairs, it's not a problem — she wants to pick out the maximum number of such pairs. If there is more than one variant of doing so, she wants to pick out the pairs so that there were as much boy-girl pairs as possible. Of course, each student must not be included in more than one pair.

Input

The first line contains an integer n (2 ≤ n ≤ 105), n is the number of students per class. Next, n lines contain information about the students, one per line. Each line contains two integers fi, si (1 ≤ fi ≤ n, fi ≠ i, 1 ≤ si ≤ 2), where fi is the number of i-th student's best friend and si denotes the i-th pupil's sex (si = 1 for a boy and si = 2 for a girl).

Output

Print on the first line two numbers t, e, where t is the maximum number of formed pairs, and e is the maximum number of boy-girl type pairs among them. Then print t lines, each line must contain a pair ai, bi (1 ≤ ai, bi ≤ n), they are numbers of pupils in the i-th pair. Print the pairs in any order. Print the numbers in pairs in any order. If there are several solutions, output any of them.

Examples

Input

5
5 2
3 2
5 1
2 1
4 2


Output

2 2
5 3
4 2


Input

6
5 2
3 2
5 1
2 1
4 2
3 1


Output

3 1
4 2
5 1
3 6


Input

8
2 2
3 2
5 1
3 1
6 1
5 1
8 2
7 1


Output

4 1
5 6
3 4
2 1
7 8

Note

The picture corresponds to the first sample. On the picture rhomb stand for boys, squares stand for girls, arrows lead from a pupil to his/her best friend. Bold non-dashed arrows stand for pairs in the answer. 

<image>

Solution (in JAVA):

Solution:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * To be documented..
 * Date: May 4, 2011
 * Time: 8:57:59 AM
 *
 * @author Pavel Mavrin
 */
public class E {

    private void solve() throws IOException {
        int n = nextInt();
        int[] f = new int[n];
        int[] s = new int[n];
        for (int i = 0; i < n; i++) {
            f[i] = nextInt() - 1;
            s[i] = nextInt();
        }
        int[] mark = new int[n];
        Node[] nodes = new Node[n];
        for (int i = 0; i < n; i++) {
            nodes[i] = new Node(i, s[i]);
        }

        List<List<Node>> cycles = new ArrayList<List<Node>>();
        for (int i = 0; i < n; i++) {
            if (mark[i] == 0) {
                int j = i;
                while (mark[j] == 0) {
                    mark[j] = 1;
                    j = f[j];
                }
                if (mark[j] == 1) {
                    List<Node> cycle = new ArrayList<Node>();
                    while (mark[j] == 1) {
                        cycle.add(nodes[j]);
                        mark[j] = 2;
                        j = f[j];
                    }
                    cycles.add(cycle);
                }
                j = i;
                while (mark[j] == 1) {
                    mark[j] = 3;
                    j = f[j];
                }
            }
        }

        for (int i = 0; i < n; i++) {
            if (mark[i] == 3) {
                nodes[f[i]].children.add(nodes[i]);
            }
        }

        List<Pair> result = new ArrayList<Pair>();

        for (List<Node> cycle : cycles) {
            for (Node node : cycle) {
                node.precalc();
            }
            Res[] d = new Res[cycle.size()];
            int[] pp = new int[cycle.size()];
            d[0] = cycle.get(0).best;
            for (int i = 1; i < cycle.size(); i++) {
                d[i] = cycle.get(i).best.sum(d[i - 1]);
                Res res = cycle.get(i).bestEmpty.sum(cycle.get(i - 1).bestEmpty);
                if (i > 1) {
                    res = res.sum(d[i - 2]);
                }
                res = res.sum(new Res(1, cycle.get(i).sex == cycle.get(i - 1).sex ? 0 : 1));
                if (res.compareTo(d[i]) > 0) {
                    d[i] = res;
                    pp[i] = 1;
                }
            }
            List<Node> cycle2 = new ArrayList<Node>();
            for (int i = 1; i < cycle.size(); i++) cycle2.add(cycle.get(i));
            cycle2.add(cycle.get(0));
            Res rr = d[cycle.size() -1];
            int[] pp2 = new int[cycle.size()];
            d[0] = cycle2.get(0).best;
            for (int i = 1; i < cycle.size(); i++) {
                d[i] = cycle2.get(i).best.sum(d[i - 1]);
                Res res = cycle2.get(i).bestEmpty.sum(cycle2.get(i - 1).bestEmpty);
                if (i > 1) {
                    res = res.sum(d[i - 2]);
                }
                res = res.sum(new Res(1, cycle2.get(i).sex == cycle2.get(i - 1).sex ? 0 : 1));
                if (res.compareTo(d[i]) > 0) {
                    d[i] = res;
                    pp2[i] = 1;
                }
            }
            Res rr2 = d[cycle.size() -1];
            if (rr2.compareTo(rr) > 0) {
                pp = pp2;
                cycle = cycle2;
            }
            int j = cycle.size() - 1;
            while (j >= 0) {
                if (pp[j] == 1) {
                    result.add(new Pair(cycle.get(j).id, cycle.get(j - 1).id));
                    cycle.get(j).addEmpty(result);
                    cycle.get(j - 1).addEmpty(result);
                    j -= 2;
                } else {
                    cycle.get(j).add(result);
                    j--;
                }
            }

        }

        int sx = 0;

        for (Pair pair : result) {
            if (s[pair.a] != s[pair.b]) sx++;
        }
        out.println(result.size() + " " + sx);

        for (Pair pair : result) {
            out.println((pair.a + 1) + " " + (pair.b + 1));
        }


        //To change body of created methods use File | Settings | File Templates.
    }

    class Pair {
        int a, b;

        Pair(int a, int b) {
            this.a = a;
            this.b = b;
        }
    }

    class Node {
        int id;
        int sex;
        List<Node> children = new ArrayList<Node>();
        Res best;
        Res bestEmpty;
        Node bestTake;

        Node(int id, int sex) {
            this.id = id;
            this.sex = sex;
        }

        public void precalc() {
            bestEmpty = new Res(0, 0);
            for (Node child : children) {
                child.precalc();
                bestEmpty.pairs += child.best.pairs;
                bestEmpty.sexpairs += child.best.sexpairs;
            }
            best = bestEmpty;
            for (Node child : children) {
                Res res = new Res(
                        bestEmpty.pairs - child.best.pairs + child.bestEmpty.pairs + 1,
                        bestEmpty.sexpairs - child.best.sexpairs + child.bestEmpty.sexpairs + (sex == child.sex ? 0 : 1)
                );
                if (res.compareTo(best) > 0) {
                    best = res;
                    bestTake = child;
                }
            }
        }

        public void addEmpty(List<Pair> result) {
            for (Node child : children) {
                child.add(result);
            }
        }

        public void add(List<Pair> result) {
            if (bestTake == null) {
                addEmpty(result);
            } else {
                result.add(new Pair(this.id, bestTake.id));
                for (Node child : children) {
                    if (child == bestTake) {
                        child.addEmpty(result);
                    } else {
                        child.add(result);
                    }
                }
            }
        }
    }

    class Res implements Comparable<Res> {
        int pairs;
        int sexpairs;

        Res(int pairs, int sexpairs) {
            this.pairs = pairs;
            this.sexpairs = sexpairs;
        }

        public int compareTo(Res res) {
            if (res.pairs != pairs) {
                return pairs - res.pairs;
            }
            return sexpairs - res.sexpairs;
        }

        public Res sum(Res res) {
            return new Res(pairs + res.pairs, sexpairs + res.sexpairs);
        }
    }

    BufferedReader br;
    StringTokenizer st;
    PrintWriter out;

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

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


    public static void main(String[] args) throws IOException {
        new E().run();
    }

    private void run() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        out.close();
        br.close();
    }

}