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 inf = 1000000000;
int a[100000], s[100000], fc[100000], gc[100000], pre[100000];
pair<int, int> f[100000][2], g[100000][2];
bool v[100000], v2[100000];
vector<int> ge[100000];
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);
}
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);
}
void dfs(int i, int x, int y) {
  v[i] = true;
  f[i][0] = make_pair(0, 0);
  if (i == x || i == y)
    f[i][1] = make_pair(-inf, 0);
  else
    f[i][1] = make_pair(1, 0);
  pair<int, int> m1 = make_pair(-inf, 0);
  fc[i] = -1;
  g[i][0] = make_pair(0, 0);
  g[i][1] = make_pair(1, 0);
  pair<int, int> m2 = make_pair(-inf, 0);
  gc[i] = -1;
  for (int j = 0; j < ge[i].size(); j++) {
    int t = ge[i][j];
    if (v[t]) continue;
    if (i == x && t == y || i == y && t == x) continue;
    pre[t] = i;
    dfs(t, x, y);
    pair<int, int> t1 = max(f[t][0], f[t][1]);
    f[i][0] = f[i][0] + t1;
    if (i != x && i != y) {
      f[i][1] = f[i][1] + t1;
      if (t != x && t != y) {
        pair<int, int> tt = f[t][0] - t1 + make_pair(0, s[i] ^ s[t]);
        if (tt > m1) {
          m1 = tt;
          fc[i] = t;
        }
      }
    }
    pair<int, int> t2 = max(g[t][0], g[t][1]);
    g[i][0] = g[i][0] + t2;
    g[i][1] = g[i][1] + t2;
    pair<int, int> tt = g[t][0] - t2 + make_pair(0, s[i] ^ s[t]);
    if (tt > m2) {
      m2 = tt;
      gc[i] = t;
    }
  }
  if (i != x && i != y) f[i][1] = f[i][1] + m1;
  g[i][1] = g[i][1] + m2;
}
void getAnswer(int i, int c, pair<int, int> f[][2], int fc[],
               vector<pair<int, int> > &ans) {
  v2[i] = true;
  for (int j = 0; j < ge[i].size(); j++) {
    int t = ge[i][j];
    if (pre[t] != i || v2[t]) continue;
    if (c == 0 || t != fc[i]) {
      getAnswer(t, f[t][1] > f[t][0], f, fc, ans);
    } else {
      ans.push_back(make_pair(i, t));
      getAnswer(t, 0, f, fc, ans);
    }
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &a[i], &s[i]);
    a[i]--;
    s[i]--;
    ge[i].push_back(a[i]);
    ge[a[i]].push_back(i);
  }
  memset(v, false, sizeof(v));
  memset(v2, false, sizeof(v2));
  memset(pre, -1, sizeof(pre));
  vector<pair<int, int> > ans;
  for (int i = 0; i < n; i++) {
    if (v[i]) continue;
    int j = a[i];
    v[i] = true;
    while (!v[j]) {
      v[j] = true;
      j = a[j];
    }
    v[i] = false;
    int k = a[i];
    while (v[k]) {
      v[k] = false;
      k = a[k];
    }
    if (a[a[j]] != j)
      dfs(j, j, a[j]);
    else
      dfs(j, -1, -1);
    if (f[j][0] + make_pair(1, s[j] ^ s[a[j]]) > max(g[j][0], g[j][1])) {
      getAnswer(j, 0, f, fc, ans);
      if (a[a[j]] != j) ans.push_back(make_pair(j, a[j]));
    } else {
      getAnswer(j, g[j][1] > g[j][0], g, gc, ans);
    }
  }
  int cnt = 0;
  for (int i = 0; i < ans.size(); i++)
    cnt += s[ans[i].first] ^ s[ans[i].second];
  printf("%d %d\n", (int)ans.size(), cnt);
  for (int i = 0; i < ans.size(); i++)
    printf("%d %d\n", ans[i].first + 1, ans[i].second + 1);
  return 0;
}