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;
const int maxn = 100005;
pair<int, int> f[maxn][2][2];
bool u[maxn][2][2];
vector<int> root;
int col[maxn], p[maxn];
vector<int> g[maxn];
int n;
vector<pair<int, int> > ans;
int par[maxn];
int croot;
inline pair<int, int> operator+(const pair<int, int>& a,
                                const pair<int, int>& b) {
  return make_pair(a.first + b.first, a.second + b.second);
}
inline pair<int, int> operator-(const pair<int, int>& a,
                                const pair<int, int>& b) {
  return make_pair(a.first - b.first, a.second - b.second);
}
int parent(int x) {
  if (par[x] != x) par[x] = parent(par[x]);
  return par[x];
}
void merge(int x, int y) {
  x = parent(x), y = parent(y);
  par[x] = y;
}
pair<int, int> func(int x, int lst, int cyc) {
  if (u[x][lst][cyc]) return f[x][lst][cyc];
  pair<int, int>& res = f[x][lst][cyc];
  u[x][lst][cyc] = true;
  res = make_pair(0, 0);
  if (lst) {
    for (__typeof((g[x]).begin()) it = (g[x]).begin(); it != (g[x]).end(); ++it)
      res = res + func(*it, cyc && *it == p[croot], cyc);
  } else {
    pair<int, int> tot(0, 0);
    for (__typeof((g[x]).begin()) it = (g[x]).begin(); it != (g[x]).end(); ++it)
      tot = tot + func(*it, cyc && *it == p[croot], cyc);
    res = max(res, tot);
    for (__typeof((g[x]).begin()) it = (g[x]).begin(); it != (g[x]).end(); ++it)
      if (!(cyc && *it == p[croot])) {
        pair<int, int> cur = tot - func(*it, 0, cyc);
        cur = cur + func(*it, 1, cyc) + pair<int, int>(1, col[x] != col[*it]);
        res = max(res, cur);
      }
  }
  return res;
}
void build(int x, int lst, int cyc) {
  if (lst) {
    for (__typeof((g[x]).begin()) it = (g[x]).begin(); it != (g[x]).end(); ++it)
      build(*it, cyc && *it == p[croot], cyc);
  } else {
    pair<int, int> tot(0, 0);
    for (__typeof((g[x]).begin()) it = (g[x]).begin(); it != (g[x]).end(); ++it)
      tot = tot + func(*it, cyc && *it == p[croot], cyc);
    if (tot == func(x, lst, cyc)) {
      for (__typeof((g[x]).begin()) it = (g[x]).begin(); it != (g[x]).end();
           ++it)
        build(*it, cyc && *it == p[croot], cyc);
      return;
    }
    for (__typeof((g[x]).begin()) it = (g[x]).begin(); it != (g[x]).end(); ++it)
      if (!(cyc && *it == p[croot])) {
        pair<int, int> cur = tot - func(*it, 0, cyc);
        cur = cur + func(*it, 1, cyc) + pair<int, int>(1, col[x] != col[*it]);
        if (cur == func(x, lst, cyc)) {
          ans.push_back(make_pair(x, *it));
          build(*it, 1, cyc);
          for (__typeof((g[x]).begin()) jt = (g[x]).begin(); jt != (g[x]).end();
               ++jt)
            if (*it != *jt) build(*jt, cyc && *jt == p[croot], cyc);
          break;
        }
      }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (n); ++i) scanf("%d %d", p + i, col + i), --p[i];
  for (int i = 0; i < (n); ++i) par[i] = i;
  for (int i = 0; i < (n); ++i) {
    if (parent(i) != parent(p[i]))
      merge(i, p[i]), g[p[i]].push_back(i);
    else
      root.push_back(i);
  }
  pair<int, int> res(0, 0);
  for (__typeof((root).begin()) it = (root).begin(); it != (root).end(); ++it) {
    croot = *it;
    pair<int, int> cur =
        max(func(*it, 0, 0),
            func(*it, 1, 1) + pair<int, int>(1, col[*it] != col[p[*it]]));
    res = res + cur;
    if (cur == func(*it, 0, 0))
      build(*it, 0, 0);
    else
      build(*it, 1, 1), ans.push_back(make_pair(*it, p[*it]));
  }
  printf("%d %d\n", res.first, res.second);
  for (__typeof((ans).begin()) it = (ans).begin(); it != (ans).end(); ++it)
    printf("%d %d\n", it->first + 1, it->second + 1);
  return 0;
}