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;
struct Pair {
  int a, b;
  Pair() {}
  Pair(int a_, int b_) : a(a_), b(b_) {}
  bool operator<(const Pair &t) const { return (a != t.a ? a < t.a : b < t.b); }
};
Pair operator+(const Pair &a, const Pair &b) {
  return (Pair(a.a + b.a, a.b + b.b));
}
Pair operator-(const Pair &a, const Pair &b) {
  return (Pair(a.a - b.a, a.b - b.b));
}
Pair mp(int a, int b) { return Pair(a, b); }
const int kMaxN = 100000;
int n, f[kMaxN], s[kMaxN], ind[kMaxN];
int q[kMaxN], qh, qt;
bool on[kMaxN], vis[kMaxN];
vector<int> t[kMaxN];
Pair g[kMaxN][2], h[kMaxN][2][2], ans;
int rg[kMaxN][2];
Pair rh[kMaxN][2][2], sol[kMaxN];
int idx;
void Solve(int st) {
  static int q[kMaxN];
  int qh = 0, qt = 0;
  q[qt++] = st;
  while (qh < qt) {
    int u = q[qh++];
    rg[u][1] = -1;
    for (vector<int>::iterator it = t[u].begin(); it != t[u].end(); ++it)
      if (on[*it]) q[qt++] = *it;
  }
  for (int i = qt - 1; i >= 0; --i) {
    int u = q[i];
    for (vector<int>::iterator it = t[u].begin(); it != t[u].end(); ++it)
      if (on[*it]) g[u][0] = g[u][0] + max(g[*it][0], g[*it][1]);
    for (vector<int>::iterator it = t[u].begin(); it != t[u].end(); ++it)
      if (on[*it]) {
        Pair tmp = g[u][0] - max(g[*it][0], g[*it][1]) + g[*it][0] +
                   mp(1, s[u] ^ s[*it]);
        if (g[u][1] < tmp) {
          g[u][1] = tmp;
          rg[u][1] = *it;
        }
      }
  }
}
void Get(int st, int a) {
  static int q[kMaxN], v[kMaxN];
  int qh = 0, qt = 0;
  v[qt] = a, q[qt++] = st;
  while (qh < qt) {
    int u = q[qh], b = v[qh++];
    if (b) sol[idx++] = Pair(u, rg[u][1]);
    for (vector<int>::iterator it = t[u].begin(); it != t[u].end(); ++it)
      if (on[*it]) {
        if (b == 0 || *it != rg[u][1]) {
          if (g[*it][0] < g[*it][1]) {
            v[qt] = 1;
            q[qt++] = *it;
          } else {
            v[qt] = 0;
            q[qt++] = *it;
          }
        } else {
          v[qt] = 0;
          q[qt++] = *it;
        }
      }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &f[i], &s[i]);
    t[--f[i]].push_back(i);
    ++ind[f[i]];
    --s[i];
  }
  for (int i = 0; i < n; ++i)
    if (!ind[i]) on[q[qt++] = i] = true;
  while (qh < qt) {
    int u = q[qh++];
    if (!(--ind[f[u]])) on[q[qt++] = f[u]] = true;
  }
  for (int i = 0; i < n; ++i)
    if (!on[i] && !vis[i]) {
      qh = qt = 0;
      vis[q[qt++] = i] = true;
      while (qh < qt) {
        int u = q[qh++];
        if (!vis[f[u]]) vis[q[qt++] = f[u]] = true;
      }
      Solve(q[0]);
      Solve(q[1]);
      rh[q[0]][0][0] = Pair(-1, -1);
      rh[q[0]][1][1] = Pair(-1, -1);
      h[q[0]][0][0] = g[q[0]][0];
      h[q[0]][1][1] = g[q[0]][1];
      h[q[0]][1][0] = h[q[0]][0][1] = mp(-0x3f3f3f3f, 0);
      h[q[1]][0][0] = h[q[0]][0][0] + g[q[1]][0], rh[q[1]][0][0] = Pair(0, 0);
      h[q[1]][0][1] = h[q[0]][1][1] + g[q[1]][0], rh[q[1]][0][1] = Pair(1, 1);
      h[q[1]][1][0] = h[q[0]][0][0] + g[q[1]][1], rh[q[1]][1][0] = Pair(0, 0);
      Pair aa = h[q[0]][1][1] + g[q[1]][1],
           bb = h[q[0]][0][0] + g[q[1]][0] + mp(1, s[q[0]] ^ s[q[1]]);
      if (aa < bb) {
        h[q[1]][1][1] = bb;
        rh[q[1]][1][1] = Pair(0, 0);
      } else {
        h[q[1]][1][1] = aa;
        rh[q[1]][1][1] = Pair(1, 1);
      }
      for (int i = 2; i < qt; ++i) {
        Solve(q[i]);
        for (int j = 0; j < 2; ++j)
          if (h[q[i - 1]][0][j] < h[q[i - 1]][1][j]) {
            h[q[i]][0][j] = h[q[i - 1]][1][j] + g[q[i]][0];
            rh[q[i]][0][j] = Pair(1, j);
          } else {
            h[q[i]][0][j] = h[q[i - 1]][0][j] + g[q[i]][0];
            rh[q[i]][0][j] = Pair(0, j);
          }
        for (int j = 0; j < 2; ++j) {
          if (h[q[i]][1][j] < h[q[i - 1]][0][j] + g[q[i]][1]) {
            h[q[i]][1][j] = h[q[i - 1]][0][j] + g[q[i]][1];
            rh[q[i]][1][j] = Pair(0, j);
          }
          if (h[q[i]][1][j] < h[q[i - 1]][1][j] + g[q[i]][1]) {
            h[q[i]][1][j] = h[q[i - 1]][1][j] + g[q[i]][1];
            rh[q[i]][1][j] = Pair(1, j);
          }
          Pair aa = h[q[i - 1]][0][j] + mp(1, s[q[i]] ^ s[q[i - 1]]) +
                    g[q[i]][0],
               bb = h[q[i - 1]][1][j] + g[q[i]][0];
          if (h[q[i]][1][j] < aa) {
            h[q[i]][1][j] = aa;
            rh[q[i]][1][j] = Pair(0, j);
          }
          if (h[q[i]][1][j] < bb) {
            h[q[i]][1][j] = bb;
            rh[q[i]][1][j] = Pair(1, j);
          }
        }
      }
      int a = 0, b = 0;
      Pair add = h[q[qt - 1]][0][0] + mp(1, s[q[qt - 1]] ^ s[q[0]]);
      if (add < h[q[qt - 1]][0][1]) {
        add = h[q[qt - 1]][0][1];
        a = 0, b = 1;
      }
      if (add < h[q[qt - 1]][1][0]) {
        add = h[q[qt - 1]][1][0];
        a = 1, b = 0;
      }
      if (add < h[q[qt - 1]][1][1]) {
        add = h[q[qt - 1]][1][1];
        a = 1, b = 1;
      }
      ans = ans + add;
      if (a == 0 && b == 0) sol[idx++] = Pair(q[0], q[qt - 1]);
      for (int i = qt - 1; i >= 0; --i) {
        Pair re = rh[q[i]][a][b];
        if (a == 0)
          Get(q[i], a);
        else {
          if (i == 0)
            Get(q[i], a);
          else {
            Pair tmp = h[q[i]][a][b] - g[q[i]][a];
            if (tmp.a == h[q[i - 1]][re.a][re.b].a &&
                tmp.b == h[q[i - 1]][re.a][re.b].b)
              Get(q[i], a);
            else {
              Get(q[i], 0);
              sol[idx++] = Pair(q[i], q[i - 1]);
            }
          }
        }
        a = re.a, b = re.b;
      }
    }
  printf("%d %d\n", ans.a, ans.b);
  for (int i = 0; i < idx; ++i) printf("%d %d\n", sol[i].a + 1, sol[i].b + 1);
  return 0;
}