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;
vector<int> E[300005];
struct tp {
  int A, B;
} match[300005], res;
int A, B, C, D, n, l, r, x, y, a, b, xx = 0, tmp, u, v;
int in[300005], p[300005], Q[300005], s[300005], choice[300005];
int f[300005][2], g[300005][2];
int F[300005][2][2], G[300005][2][2], H[300005][2][2], W[300005][2][2];
void Add(int u, int v) {
  match[++xx].A = u;
  match[xx].B = v;
}
void dfs(int u, int t) {
  if (t == 0) {
    for (int j = 0, v; j < E[u].size(); ++j) {
      v = E[u][j];
      if (f[v][0] > f[v][1] || f[v][0] == f[v][1] && g[v][0] > g[v][1])
        dfs(v, 0);
      else
        dfs(v, 1);
    }
  } else {
    Add(u, choice[u]);
    for (int j = 0, v; j < E[u].size(); ++j) {
      v = E[u][j];
      if (v == choice[u])
        dfs(v, 0);
      else {
        if (f[v][0] > f[v][1] || f[v][0] == f[v][1] && g[v][0] > g[v][1])
          dfs(v, 0);
        else
          dfs(v, 1);
      }
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &p[i], &s[i]);
    ++in[p[i]];
  }
  l = 1;
  for (int i = 1; i <= n; ++i)
    if (!in[i]) Q[++r] = i;
  for (; l <= r; ++l) {
    u = Q[l];
    v = p[u];
    E[v].push_back(u);
    --in[v];
    if (!in[v]) Q[++r] = v;
  }
  for (int i = 1; i <= n; ++i)
    if (in[i]) Q[++r] = i;
  for (int i = 1; i <= r; ++i) {
    u = Q[i];
    if (E[u].empty())
      f[u][1] = g[u][1] = -1;
    else {
      A = B = 0;
      for (int j = 0; j < E[u].size(); ++j) {
        v = E[u][j];
        if (f[v][0] > f[v][1])
          A += f[v][0], B += g[v][0];
        else if (f[v][0] < f[v][1])
          A += f[v][1], B += g[v][1];
        else
          A += f[v][0], B += max(g[v][0], g[v][1]);
      }
      f[u][0] = A, g[u][0] = B;
      for (int j = 0; j < E[u].size(); ++j) {
        v = E[u][j];
        if (f[v][0] > f[v][1])
          C = A - f[v][0], D = B - g[v][0];
        else if (f[v][0] < f[v][1])
          C = A - f[v][1], D = B - g[v][1];
        else
          C = A - f[v][0], D = B - max(g[v][0], g[v][1]);
        C += f[v][0], D += g[v][0];
        tmp = s[u] == s[v] ? 0 : 1;
        if (C + 1 > f[u][1] || C + 1 == f[u][1] && D + tmp > g[u][1]) {
          f[u][1] = C + 1, g[u][1] = D + tmp;
          choice[u] = v;
        }
      }
    }
  }
  for (int i = 1; i <= n; ++i)
    if (in[i]) {
      r = 0;
      for (u = i; in[u]; u = p[u]) {
        in[u] = 0;
        Q[++r] = u;
      }
      for (int j = 1; j <= r; ++j) {
        u = Q[j];
        if (j == 1) {
          F[u][0][1] = -1;
          G[u][0][1] = -1;
          F[u][1][0] = f[u][0];
          G[u][1][0] = g[u][0];
          F[u][0][0] = f[u][0], G[u][0][0] = g[u][0];
          F[u][1][1] = f[u][1], G[u][1][1] = g[u][1];
        } else {
          v = Q[j - 1];
          for (int t = 0; t < 2; ++t) {
            if (t == 1) {
              int xp = 0;
            }
            if (F[v][t][0] > -1) {
              if (F[v][t][0] + f[u][0] > F[u][t][0] ||
                  F[v][t][0] + f[u][0] == F[u][t][0] &&
                      G[v][t][0] + g[u][0] > G[u][t][0]) {
                F[u][t][0] = F[v][t][0] + f[u][0];
                G[u][t][0] = G[v][t][0] + g[u][0];
                H[u][t][0] = 0, W[u][t][0] = 0;
              }
              tmp = s[u] == s[v] ? 0 : 1;
              if (t || v != Q[1])
                if (F[v][t][0] + f[u][0] + 1 > F[u][t][1] ||
                    F[v][t][0] + f[u][0] + 1 == F[u][t][1] &&
                        G[v][t][0] + g[u][0] + tmp > G[u][t][1]) {
                  F[u][t][1] = F[v][t][0] + f[u][0] + 1;
                  G[u][t][1] = G[v][t][0] + g[u][0] + tmp;
                  H[u][t][1] = 0, W[u][t][1] = 0;
                }
              if (F[v][t][0] + f[u][1] > F[u][t][1] ||
                  F[v][t][0] + f[u][1] == F[u][t][1] &&
                      G[v][t][0] + g[u][1] > G[u][t][1]) {
                F[u][t][1] = F[v][t][0] + f[u][1];
                G[u][t][1] = G[v][t][0] + g[u][1];
                H[u][t][1] = 0, W[u][t][1] = 1;
              }
            }
            if (F[v][t][1] > -1) {
              if (F[v][t][1] + f[u][0] > F[u][t][0] ||
                  F[v][t][1] + f[u][0] == F[u][t][0] &&
                      G[v][t][1] + g[u][0] > G[u][t][0]) {
                F[u][t][0] = F[v][t][1] + f[u][0];
                G[u][t][0] = G[v][t][1] + g[u][0];
                H[u][t][0] = 1, W[u][t][0] = 0;
              }
              if (F[v][t][1] + f[u][1] > F[u][t][1] ||
                  F[v][t][1] + f[u][1] == F[u][t][1] &&
                      G[v][t][1] + g[u][1] > G[u][t][1]) {
                F[u][t][1] = F[v][t][1] + f[u][1];
                G[u][t][1] = G[v][t][1] + g[u][1];
                H[u][t][1] = 1, W[u][t][1] = 1;
              }
            }
          }
        }
      }
      u = Q[r];
      A = B = 0;
      for (int aa = 0; aa < 2; ++aa)
        for (int bb = 0; bb < 2; ++bb)
          if (F[u][aa][bb] > A || F[u][aa][bb] == A && G[u][aa][bb] > B) {
            A = F[u][aa][bb], B = G[u][aa][bb];
            x = aa, y = bb;
          }
      v = Q[1];
      tmp = s[u] == s[v] ? 0 : 1;
      if (F[u][0][0] + 1 > A || F[u][0][0] + 1 == A && G[u][0][0] + tmp > B) {
        A = F[u][0][0] + 1;
        B = G[u][0][0] + tmp;
        x = 0, y = 0;
        Add(u, v);
      }
      res.A += A, res.B += B;
      for (int j = r; j > 1; --j) {
        u = Q[j];
        a = H[u][x][y], b = W[u][x][y];
        dfs(u, b);
        if (!a && !b && y) Add(u, Q[j - 1]);
        y = a;
      }
      if (x && !y) x = 0;
      dfs(Q[1], x);
    }
  printf("%d %d\n", res.A, res.B);
  for (int i = 1; i <= xx; ++i) printf("%d %d\n", match[i].A, match[i].B);
  return 0;
}