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>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
inline pair<int, int> operator+=(pair<int, int> &a, const pair<int, int> &b) {
  a.first += b.first;
  a.second += b.second;
  return a;
}
inline pair<int, int> operator+(const pair<int, int> &a,
                                const pair<int, int> &b) {
  return pair<int, int>(a.first + b.first, a.second + b.second);
}
inline pair<int, int> operator-(const pair<int, int> &a,
                                const pair<int, int> &b) {
  return pair<int, int>(a.first - b.first, a.second - b.second);
}
int a, b, c, d, i, j, n, m, k;
int p[100001], pr[2][100001];
pair<int, int> dp[2][100001];
bool inCycle[100001], used[100001], boy[100001], take[100001];
vector<int> cycle[100001], sm[100001];
pair<int, int> ans;
vector<pair<int, int> > match;
pair<int, int> rec(int v, bool can) {
  if (dp[can][v] != make_pair(-1, -1)) return dp[can][v];
  pair<int, int> ans = make_pair(0, 0);
  pair<int, int> sum = make_pair(0, 0);
  for (int _n(((int)((sm[v]).size())) - 1), i(0); i <= _n; i++)
    if (!inCycle[sm[v][i]]) {
      pair<int, int> r = rec(sm[v][i], 1);
      sum += r;
    }
  if (sum > ans) {
    ans = sum;
    pr[can][v] = -1;
  }
  if (can)
    for (int _n(((int)((sm[v]).size())) - 1), i(0); i <= _n; i++) {
      if (inCycle[sm[v][i]]) continue;
      pair<int, int> r = sum - rec(sm[v][i], 1) + rec(sm[v][i], 0) +
                         make_pair(1, boy[v] != boy[sm[v][i]]);
      if (r > ans) {
        ans = r;
        pr[can][v] = sm[v][i];
      }
    }
  return dp[can][v] = ans;
}
void build(int v, bool can) {
  for (int _n(((int)((sm[v]).size())) - 1), i(0); i <= _n; i++)
    if (!inCycle[sm[v][i]]) {
      if (sm[v][i] != pr[can][v]) build(sm[v][i], 1);
    }
  if (pr[can][v] != -1) {
    build(pr[can][v], 0);
    match.push_back(make_pair(v, pr[can][v]));
  }
}
int main() {
  scanf("%d", &n);
  for (int _n((n)-1), i(0); i <= _n; i++)
    scanf("%d%d", &p[i], &b), --p[i], sm[p[i]].push_back(i), boy[i] = b - 1;
  for (int _n((n)-1), i(0); i <= _n; i++) {
    if (used[i]) continue;
    vector<int> cur(0), o(0);
    a = i;
    while (!used[a]) {
      cur.push_back(a);
      used[a] = 1;
      a = p[a];
    }
    while (!cur.empty() && cur.back() != a)
      o.push_back(cur.back()), cur.pop_back();
    if ((int)((cur).size()) && cur.back() == a) {
      cur.pop_back();
      o.push_back(a);
      for (int _n(((int)((o).size())) - 1), i(0); i <= _n; i++)
        inCycle[o[i]] = 1;
      cycle[m++] = o;
    }
  }
  memset(dp, -1, sizeof(dp));
  memset(pr, -1, sizeof(pr));
  for (int _n((m)-1), h(0); h <= _n; h++) {
    vector<bool> old;
    pair<int, int> best = make_pair(0, 0);
    bool fl = 0;
    for (int _n((2) - 1), firstLast(0); firstLast <= _n; firstLast++) {
      vector<pair<int, int> > dp_1((int)((cycle[h]).size()));
      vector<bool> pr_1((int)((cycle[h]).size()), false);
      for (int _n(((int)((cycle[h]).size())) - 1), i(0); i <= _n; i++) {
        bool can = 1;
        if (firstLast && (!i || i == (int)((cycle[h]).size()) - 1)) can = 0;
        dp_1[i] = rec(cycle[h][i], can);
        if (i) dp_1[i] += dp_1[i - 1];
        if (i &&
            (!firstLast || (i != (int)((cycle[h]).size()) - 1 && i != 1))) {
          pair<int, int> r = rec(cycle[h][i], 0);
          r += rec(cycle[h][i - 1], 0);
          r += make_pair(1, boy[cycle[h][i]] != boy[cycle[h][i - 1]]);
          if (i > 1) r += dp_1[i - 2];
          if (r > dp_1[i]) {
            dp_1[i] = r;
            pr_1[i] = 1;
          }
        }
      }
      pair<int, int> now = dp_1.back();
      if (firstLast)
        now += make_pair(1, boy[cycle[h][0]] != boy[cycle[h].back()]);
      if ((int)((old).size()) == 0 || now > best) {
        best = now;
        old = pr_1;
        fl = firstLast;
      }
    }
    ans += best;
    a = (int)((cycle[h]).size()) - 1;
    while (a >= 0) {
      bool cur = old[a] ^ 1;
      if (a == (int)((cycle[h]).size()) - 1 && fl) cur = 0;
      if (!a && fl) cur = 0;
      build(cycle[h][a], cur);
      if (old[a]) {
        build(cycle[h][a - 1], 0);
        match.push_back(make_pair(cycle[h][a], cycle[h][a - 1]));
        a -= 2;
      } else
        --a;
    }
    if (fl) match.push_back(make_pair(cycle[h][0], cycle[h].back()));
  }
  printf("%d %d\n", ans.first, ans.second);
  for (int _n(((int)((match).size())) - 1), i(0); i <= _n; i++) {
    printf("%d %d\n", match[i].first + 1, match[i].second + 1);
  }
}