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.Arrays;
import java.util.List;
import java.util.StringTokenizer;

public class Museum implements Runnable {
    static class Student {
        int mark = 0;
        Student friend;
        List<Student> friendOf = new ArrayList<Student>(1);
        boolean gender;
        int index;
    }

    static abstract class Answer implements Comparable<Answer> {
        final int numPairs;
        final int numGoodPairs;

        Answer(int numPairs, int numGoodPairs) {
            this.numPairs = numPairs;
            this.numGoodPairs = numGoodPairs;
        }

        public int compareTo(Answer answer) {
            if (numPairs != answer.numPairs)
                return numPairs - answer.numPairs;
            else
                return numGoodPairs - answer.numGoodPairs;
        }

        abstract void outputPairs(PrintWriter writer);
    }

    static class EmptyAnswer extends Answer {
        EmptyAnswer() {
            super(0, 0);
        }

        @Override
        void outputPairs(PrintWriter writer) {
            // Do nothing.
        }
    }

    static class SinglePairAnswer extends Answer {
        Student first;
        Student second;

        SinglePairAnswer(Student first, Student second) {
            super(1, first.gender == second.gender ? 0 : 1);
            this.first = first;
            this.second = second;
        }

        @Override
        void outputPairs(PrintWriter writer) {
            writer.println(first.index + " " + second.index);
        }
    }

    static Answer combine(final Answer a, final Answer b) {
        return new Answer(a.numPairs + b.numPairs, a.numGoodPairs + b.numGoodPairs){
            @Override
            void outputPairs(PrintWriter writer) {
                a.outputPairs(writer);
                b.outputPairs(writer);
            }
        };
    }

    private void solve() throws IOException {
        int n = nextInt();
        Student[] s = new Student[n];
        for (int i = 0; i < n; ++i) s[i] = new Student();
        for (int i = 0; i < n; ++i) {
            s[i].friend = s[nextInt() - 1];
            s[i].friend.friendOf.add(s[i]);
            s[i].gender = nextInt() == 1;
            s[i].index = i + 1;
        }
        List<Answer> bestAnswers = new ArrayList<Answer>();
        for (int i = 0; i < n; ++i) if (s[i].mark == 0) {
            Student root = s[i];
            while (root.mark == 0) {
                root.mark = 1;
                root = root.friend;
            }
            Answer ans1 = solveForTree(root, 2)[0];
            root = root.friend;
            Answer ans2 = solveForTree(root, 3)[0];
            if (ans1.compareTo(ans2) > 0) {
                bestAnswers.add(ans1);
            } else {
                bestAnswers.add(ans2);
            }
        }
        Answer allTogether = new EmptyAnswer();
        for (Answer ans : bestAnswers)
            allTogether = combine(allTogether, ans);
        writer.println(allTogether.numPairs + " " + allTogether.numGoodPairs);
        allTogether.outputPairs(writer);
    }

    private Answer[] solveForTree(Student root, int markToUse) {
        if (root.mark >= markToUse) throw new RuntimeException();
        root.mark = markToUse;
        List<Answer[]> children = new ArrayList<Answer[]>(root.friendOf.size());
        long[] bestChildScore = new long[2];
        Arrays.fill(bestChildScore, Long.MIN_VALUE);
        Answer[][] bestChildAnswer = new Answer[2][];
        Student[] bestChildFor = new Student[2];
        for (Student child : root.friendOf) {
            if (child.mark < markToUse) {
                Answer[] childAnswer = solveForTree(child, markToUse);
                children.add(childAnswer);
                long childScore = (childAnswer[1].numPairs - childAnswer[0].numPairs) * 1000000L + (childAnswer[1].numGoodPairs - childAnswer[0].numGoodPairs);
                int genderId = child.gender ? 1 : 0;
                if (childScore > bestChildScore[genderId]) {
                    bestChildScore[genderId] = childScore;
                    bestChildAnswer[genderId] = childAnswer;
                    bestChildFor[genderId] = child;
                }
            }
        }
        Answer notTake = new EmptyAnswer();
        for (Answer[] childAnswer : children) notTake = combine(notTake, childAnswer[0]);
        Answer take = notTake;
        for (int genderId = 0; genderId < 2; ++genderId) {
            Answer[] best = bestChildAnswer[genderId];
            if (best != null) {
                Answer cur = new SinglePairAnswer(root, bestChildFor[genderId]);
                cur = combine(cur, best[1]);
                for (Answer[] childAnswer : children)
                    if (childAnswer != best)
                        cur = combine(cur, childAnswer[0]);
                if (cur.compareTo(take) > 0)
                    take = cur;
            }
        }
        return new Answer[]{take, notTake};
    }

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

    BufferedReader reader;
    StringTokenizer tokenizer;
    PrintWriter writer;

    public void run() {
        try {
            reader = new BufferedReader(new InputStreamReader(System.in));
            tokenizer = null;
            writer = new PrintWriter(System.out);
            solve();
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

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

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

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

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