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

Solution:
#include <bits/stdc++.h>
using namespace std;
int n;
int gender[200000];
int parent[200000];
vector<int> con[200000];
char used[200000];
int forbid1, forbid2;
long long dp[200000][2][2];
int froot;
int fc[2];
void mark_used(int x) {
  used[x] = 2;
  int i, a;
  for (i = 0; i < con[x].size(); i++) {
    a = con[x][i];
    if (used[a] != 2) mark_used(a);
  }
}
long long doit1(int x, int p, int fid, int taken) {
  long long& res = dp[x][fid][taken];
  if (res != -1) return res;
  int i, a;
  vector<int> child;
  for (i = 0; i < con[x].size(); i++) {
    a = con[x][i];
    if (a == p) continue;
    if (x == froot && a == fc[fid]) continue;
    if (x == fc[fid] && a == froot) continue;
    child.push_back(a);
  }
  if (x == froot && fc[0] == fc[1]) child.push_back(fc[0]);
  if (child.empty()) return res = 0;
  long long v1 = 0;
  for (i = 0; i < child.size(); i++) {
    a = child[i];
    v1 += doit1(a, x, fid, 0);
  }
  res = v1;
  if (!taken) {
    for (i = 0; i < child.size(); i++) {
      a = child[i];
      long long v2 = v1 - doit1(a, x, fid, 0) + doit1(a, x, fid, 1);
      v2 += 1000000;
      if (gender[x] != gender[a]) v2++;
      if (v2 > res) res = v2;
    }
  }
  return res;
}
vector<pair<int, int> > the_pairs;
void trace(int x, int p, int fid, int taken) {
  long long res = dp[x][fid][taken];
  int i, a;
  vector<int> child;
  for (i = 0; i < con[x].size(); i++) {
    a = con[x][i];
    if (a == p) continue;
    if (x == froot && a == fc[fid]) continue;
    if (x == fc[fid] && a == froot) continue;
    child.push_back(a);
  }
  if (x == froot && fc[0] == fc[1]) child.push_back(fc[0]);
  if (child.empty()) return;
  long long v1 = 0;
  for (i = 0; i < child.size(); i++) {
    a = child[i];
    v1 += doit1(a, x, fid, 0);
  }
  if (res == v1) {
    for (i = 0; i < child.size(); i++) trace(child[i], x, fid, 0);
    return;
  }
  for (i = 0; i < child.size(); i++) {
    a = child[i];
    long long v2 = v1 - doit1(a, x, fid, 0) + doit1(a, x, fid, 1);
    v2 += 1000000;
    if (gender[x] != gender[a]) v2++;
    if (v2 == res) break;
  }
  int z = i;
  the_pairs.push_back(pair<int, int>(x, child[z]));
  trace(child[z], x, fid, 1);
  for (i = 0; i < child.size(); i++)
    if (i != z) trace(child[i], x, fid, 0);
}
long long doit(int x) {
  int root = x;
  int prev = -1;
  while (!used[root]) {
    used[root] = 1;
    prev = root;
    root = parent[root];
  }
  mark_used(root);
  froot = root;
  fc[0] = parent[root];
  fc[1] = prev;
  long long v1 = doit1(root, -1, 0, 0);
  long long v2 = doit1(root, -1, 1, 0);
  if (v1 > v2) {
    trace(root, -1, 0, 0);
    return v1;
  } else {
    trace(root, -1, 1, 0);
    return v2;
  }
}
int main() {
  scanf("%d", &n);
  int i, a;
  for (i = 0; i < n; i++) con[i].clear();
  for (i = 0; i < n; i++) {
    scanf("%d %d", &a, &gender[i]);
    a--;
    parent[i] = a;
    con[i].push_back(a);
    con[a].push_back(i);
  }
  memset(used, 0, sizeof(used));
  memset(dp, -1, sizeof(dp));
  the_pairs.clear();
  long long res = 0;
  for (i = 0; i < n; i++)
    if (used[i] == 0) {
      res += doit(i);
    }
  printf("%I64d %I64d\n", res / 1000000, res % 1000000);
  for (i = 0; i < the_pairs.size(); i++)
    printf("%d %d\n", the_pairs[i].first + 1, the_pairs[i].second + 1);
  return 0;
}