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 = 100000 + 10;
vector<int> e[maxn];
pair<int, int> opt[maxn][2];
int f[maxn], s[maxn], nxt[maxn];
int n;
bool flag[maxn];
int flag2[maxn];
int que[maxn];
pair<int, int> res;
vector<pair<int, int> > info, _, __;
pair<int, int> operator+(pair<int, int> a, pair<int, int> b) {
  return make_pair(a.first + b.first, a.second + b.second);
}
pair<int, int> operator-(pair<int, int> a, pair<int, int> b) {
  return make_pair(a.first - b.first, a.second - b.second);
}
void dp(int u) {
  nxt[u] = 0;
  opt[u][0] = opt[u][1] = make_pair(0, 0);
  pair<int, int> tmp = make_pair(0, 0);
  for (int v = 0; v < e[u].size(); v++) {
    dp(e[u][v]);
    tmp = tmp + opt[e[u][v]][0];
  }
  opt[u][1] = tmp;
  for (int v = 0; v < e[u].size(); v++) {
    pair<int, int> _tmp = tmp - opt[e[u][v]][0];
    _tmp = _tmp + opt[e[u][v]][1];
    _tmp = _tmp + make_pair(1, s[u] != s[e[u][v]]);
    if (_tmp > opt[u][0]) {
      opt[u][0] = _tmp;
      nxt[u] = e[u][v];
    }
  }
  if (opt[u][1] > opt[u][0]) {
    opt[u][0] = opt[u][1];
    nxt[u] = -1;
  }
}
void getinfo(int u, int v) {
  if (v == 0 && nxt[u] > 0) _.push_back(make_pair(u, nxt[u]));
  for (int w = 0; w < e[u].size(); w++)
    if (v == 1 || nxt[u] != e[u][w])
      getinfo(e[u][w], 0);
    else
      getinfo(e[u][w], 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d%d", &f[i], &s[i]);
  for (int i = 1; i <= n; i++)
    if (!flag2[i]) {
      flag2[i] = i;
      que[0] = i;
      for (int l = 0, r = 0; l <= r; l++) {
        if (!flag2[f[que[l]]]) {
          que[++r] = f[que[l]];
          flag2[que[r]] = i;
        } else if (flag2[f[que[l]]] != i)
          break;
        else {
          for (int k = r;; k--) {
            flag[que[k]] = 1;
            if (que[k] == f[que[l]]) break;
          }
          break;
        }
      }
    }
  for (int i = 1; i <= n; i++)
    if (!flag[i]) e[f[i]].push_back(i);
  for (int i = 1; i <= n; i++) flag2[i] = 0;
  for (int i = 1; i <= n; i++)
    if (flag[i] && !flag2[i]) {
      int cnt = 1;
      que[1] = i;
      que[2] = f[i];
      while (que[cnt + 1] != i) {
        cnt++;
        que[cnt + 1] = f[que[cnt]];
      }
      for (int j = 1; j <= cnt; j++) flag2[que[j]] = 1;
      pair<int, int> A, B;
      A = B = make_pair(0, 0);
      _.clear();
      for (int j = 4; j <= cnt; j++) e[que[j - 1]].push_back(que[j]);
      if (cnt > 2) {
        dp(que[3]);
        A = opt[que[3]][0];
        getinfo(que[3], 0);
      }
      for (int k = 1; k <= 2; k++)
        for (int j = 0; j < e[que[k]].size(); j++) {
          dp(e[que[k]][j]);
          A = A + opt[e[que[k]][j]][0];
          getinfo(e[que[k]][j], 0);
        }
      A = A + make_pair(1, s[que[1]] != s[que[2]]);
      _.push_back(make_pair(que[1], que[2]));
      B = A;
      __ = _;
      A = make_pair(0, 0);
      _.clear();
      if (cnt > 2) e[que[2]].push_back(que[3]);
      e[que[cnt]].push_back(que[1]);
      dp(que[2]);
      A = opt[que[2]][0];
      getinfo(que[2], 0);
      if (A < B) {
        swap(A, B);
        swap(_, __);
      }
      res = res + A;
      for (int j = 0; j < _.size(); j++) info.push_back(_[j]);
    }
  printf("%d %d\n", res.first, res.second);
  for (int i = 0; i < info.size(); i++)
    printf("%d %d\n", info[i].first, info[i].second);
}