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;
using namespace std::rel_ops;
const int N = 100050, inf = ~0u >> 2;
struct state {
  int c, sc;
  static state minimal() { return (state){-inf, -inf}; }
  state(int x = 0, int y = 0) : c(x), sc(y) {}
};
state operator+(state a, state b) { return (state){a.c + b.c, a.sc + b.sc}; }
state operator-(state a, state b) { return (state){a.c - b.c, a.sc - b.sc}; }
bool operator<(state a, state b) {
  return a.c < b.c || (a.c == b.c && a.sc < b.sc);
}
bool operator>(state a, state b) {
  return a.c > b.c || (a.c == b.c && a.sc > b.sc);
}
bool operator==(state a, state b) { return a.c == b.c && a.sc == b.sc; }
state dp[N][2];
int pal[N][2], gender[N], invalid[N], n;
vector<int> e[N];
vector<int> ring;
vector<pair<int, int> > reslist;
int pre[N], vis[N], sel[N];
bool findRing(int x, int ax) {
  if (vis[x]) {
    for (int y = ax; y != pre[x]; y = pre[y]) invalid[y] = 1, ring.push_back(y);
    return true;
  }
  vis[x] = true;
  pre[x] = ax;
  bool res = false;
  for (int y : e[x])
    if (y != ax && !invalid[y] && findRing(y, x))
      res = true;
    else if (y == ax)
      ax = -1;
  return res;
}
void dfs(int x, int ax) {
  state mdel = state::minimal(), cdel;
  int z = 0;
  for (int y : e[x])
    if (!invalid[y] && y != ax) {
      dfs(y, x);
      dp[x][1] = dp[x][1] + max(dp[y][1], dp[y][0]);
      dp[x][0] = dp[x][0] + max(dp[y][1], dp[y][0]);
      cdel =
          dp[y][1] - max(dp[y][1], dp[y][0]) + state(1, gender[y] != gender[x]);
      if (cdel > mdel) mdel = cdel, z = y;
    }
  if (mdel != state::minimal()) {
    dp[x][0] = dp[x][0] + mdel;
    pal[x][0] = z;
  }
}
void tgenerate(int x, int ax, int cp) {
  if (!cp && pal[x][0]) reslist.push_back(make_pair(x, pal[x][0]));
  for (int y : e[x])
    if (y != ax && !invalid[y])
      if (!cp && y == pal[x][0])
        tgenerate(y, x, 1);
      else
        tgenerate(y, x, dp[y][1] >= dp[y][0]);
}
state dpRing(int l, int r, bool gen = false) {
  static state dp[N][2];
  if (l > r) return (state){0, 0};
  dp[ring[l]][0] = ::dp[ring[l]][0];
  dp[ring[l]][1] = ::dp[ring[l]][1];
  for (int i = l; i + 1 <= r; ++i) {
    int x = ring[i], y = ring[i + 1];
    dp[y][0] = ::dp[y][0];
    dp[y][1] = ::dp[y][1];
    state a = dp[y][0] + max(dp[x][0], dp[x][1]),
          b = dp[y][1] + dp[x][1] + state(1, gender[y] != gender[x]);
    dp[y][0] = max(a, b);
    if (gen) pal[y][0] = (dp[y][0] == a) ? pal[y][0] : x;
    dp[y][1] = max(dp[x][0], dp[x][1]) + dp[y][1];
  }
  state a = dp[ring[r]][0], b = dp[ring[r]][1];
  if (!gen) return max(a, b);
  for (int i = r, cp = (a > b ? 0 : 1); i >= l; --i)
    if (cp == 0 && i != l && pal[ring[i]][cp] == ring[i - 1]) {
      reslist.push_back(make_pair(ring[i], ring[i - 1]));
      tgenerate(ring[i], 0, 1);
      cp = 1;
    } else {
      tgenerate(ring[i], 0, cp);
      if (i != l) cp = dp[ring[i - 1]][0] < dp[ring[i - 1]][1];
    }
  return max(a, b);
}
inline state solveRing(int u) {
  ring.clear();
  if (!findRing(u, 0) || ring.size() == 1) throw;
  for (int y : ring) dfs(y, 0);
  int f = ring.front(), r = ring.back();
  state res1 = dpRing(0, ring.size() - 1), res2,
        cd2 = (state){1, gender[f] != gender[r]} + dp[f][1] + dp[r][1];
  res2 = dpRing(1, ring.size() - 2) + cd2;
  if (res1 > res2) {
    dpRing(0, ring.size() - 1, true);
    return res1;
  } else {
    dpRing(1, ring.size() - 2, true);
    reslist.push_back(make_pair(f, r));
    tgenerate(f, 0, 1);
    tgenerate(r, 0, 1);
    return res2;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1, a; i <= n; ++i) {
    scanf("%d%d", &a, gender + i);
    e[a].push_back(i);
    e[i].push_back(a);
  }
  state res = (state){0, 0};
  for (int u = 1; u <= n; ++u) {
    if (vis[u]) continue;
    res = res + solveRing(u);
  }
  printf("%d %d\n", res.c, res.sc);
  if (reslist.size() != res.c) throw;
  for (auto r : reslist) printf("%d %d\n", r.first, r.second);
  return 0;
}