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 head[100100], to[2 * 100100], nxt[2 * 100100];
int deg[100100], dp0[100100], dp1[100100], bg0[100100], bg1[100100],
    from[100100][2];
int cir[100100], siz, forbid;
int sex[100100];
bool vis[100100];
int cnt, n;
queue<int> q;
vector<pair<int, int> > sol;
void init() { memset(head, -1, sizeof(head)), cnt = -1; }
void add_e(int a, int b, bool id) {
  nxt[++cnt] = head[a], head[a] = cnt, to[cnt] = b, deg[b] += id;
  if (id) add_e(b, a, 0);
}
void topo() {
  for (int i = (1); i <= (n); i++)
    if (!deg[i]) q.push(i);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int i = head[cur]; ~i; i = nxt[i]) {
      if (--deg[to[i]] == 0) q.push(to[i]);
    }
  }
}
void dfs(int x, int fa, int id) {
  int mx = -0x3f3f3f3f, node, mat = 0;
  int tot1 = 0, tot2 = 0;
  vis[x] = true;
  for (int i = head[x], y; ~i; i = nxt[i]) {
    if ((y = to[i]) == fa || i == forbid || (i ^ 1) == forbid) continue;
    dfs(y, x, id);
    tot1 += dp0[y], tot2 += bg0[y];
    if (dp1[y] - dp0[y] > mx ||
        (dp1[y] - dp0[y] == mx && bg1[y] - bg0[y] + (sex[x] != sex[y]) > mat)) {
      mx = dp1[y] - dp0[y], node = y,
      mat = bg1[y] - bg0[y] + (sex[x] != sex[y]);
    }
  }
  dp0[x] = dp1[x] = tot1, bg0[x] = bg1[x] = tot2;
  mx++;
  if (mx > 0 || (mx == 0 && mat > 0)) {
    dp0[x] = tot1 + mx;
    bg0[x] += mat, from[x][id] = node;
  }
}
void path(int x, int fa, int id, bool tf) {
  if (from[x][id] && tf) sol.push_back(make_pair(x, from[x][id]));
  for (int i = head[x], y; ~i; i = nxt[i]) {
    if ((y = to[i]) == fa || i == forbid || (i ^ 1) == forbid) continue;
    path(y, x, id, y != from[x][id] || !tf);
  }
}
int main() {
  cin >> n;
  int x;
  init();
  for (int i = (1); i <= (n); i++) cin >> x, add_e(i, x, 1), cin >> sex[i];
  topo();
  int e1, e2;
  int ans = 0, match = 0;
  for (int amid = (1); amid <= (n); amid++)
    if (deg[amid] > 0 && !vis[amid]) {
      e1 = -1, e2 = 0;
      for (int i = head[amid]; ~i; i = nxt[i]) {
        if (deg[to[i]])
          if (~e1)
            e2 = i;
          else
            e1 = i;
      }
      forbid = e1;
      dfs(amid, 0, 0);
      int del_ans = dp0[amid], del_match = bg0[amid], type = 0;
      forbid = e2;
      dfs(amid, 0, 1);
      if (del_ans < dp0[amid] ||
          (del_ans == dp0[amid] && del_match < bg0[amid]))
        del_ans = dp0[amid], type = 1, del_match = bg0[amid];
      if (!type) forbid = e1;
      path(amid, 0, type, true);
      ans += del_ans, match += del_match;
    }
  cout << ans << " " << match << endl;
  for (int i = (0); i <= ((int)sol.size() - 1); i++)
    cout << sol[i].first << " " << sol[i].second << endl;
  return 0;
}