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;
int n, num = 1, fst[100010], gen[100010], from[100010], pre[100010],
       gp[100010][2];
struct edge {
  int x, y, n;
} e[200010];
bool tf[100010], oncir[100010], v[100010];
pair<int, int> f[100010][2], g[100010][2], ans(0, 0);
vector<pair<int, int> > anse;
pair<int, int> operator+(pair<int, int> x, pair<int, int> y) {
  return make_pair(x.first + y.first, x.second + y.second);
}
pair<int, int> operator-(pair<int, int> x, pair<int, int> y) {
  return make_pair(x.first - y.first, x.second - y.second);
}
pair<int, int> &operator+=(pair<int, int> &x, pair<int, int> y) {
  return x = x + y;
}
void ins(int x, int y) {
  e[++num] = {x, y, fst[x]};
  fst[x] = num;
}
void mark(int x) {
  tf[x] = 1;
  for (int i = fst[x]; i; i = e[i].n) {
    int y = e[i].y;
    if (tf[y]) continue;
    mark(y);
  }
}
bool dfs(int x, int f, vector<int> &cir) {
  v[x] = 1;
  for (int i = fst[x]; i; i = e[i].n) {
    if (i == (f ^ 1)) continue;
    int y = e[i].y;
    if (v[y]) {
      int hh = x;
      while (x != y) {
        cir.push_back(x);
        oncir[x] = 1;
        x = from[x];
      }
      cir.push_back(y);
      oncir[y] = 1;
      return 1;
    }
    from[y] = x;
    if (dfs(y, i, cir)) return 1;
  }
  return 0;
}
void dp(int x, int fa) {
  f[x][0] = f[x][1] = {0, 0};
  for (int i = fst[x]; i; i = e[i].n) {
    int y = e[i].y;
    if (y == fa || oncir[y]) continue;
    dp(y, x);
    f[x][0] += max(f[y][0], f[y][1]);
  }
  for (int i = fst[x]; i; i = e[i].n) {
    int y = e[i].y;
    if (y == fa || oncir[y]) continue;
    pair<int, int> hh = f[x][0] - max(f[y][0], f[y][1]) + f[y][0] +
                        make_pair(1, gen[x] != gen[y]);
    if (hh > f[x][1]) {
      pre[x] = y;
      f[x][1] = hh;
    }
  }
}
void clr_g(int size) {
  for (int i = 0; i < size; i++) {
    g[i][0] = g[i][1] = make_pair(1, 1);
    gp[i][0] = gp[i][1] = -1;
  }
}
void get(int x, int fa, int type, vector<pair<int, int> > &anse) {
  if (type) anse.emplace_back(x, pre[x]);
  for (int i = fst[x]; i; i = e[i].n) {
    int y = e[i].y;
    if (y == fa || oncir[y]) continue;
    if (type && y == pre[x])
      get(y, x, 0, anse);
    else
      get(y, x, f[y][0] < f[y][1], anse);
  }
}
void path(vector<pair<int, int> > &tmp, vector<int> &cir, int type, bool tf) {
  for (int i = cir.size() - 1; i > 0; i--) {
    if (!type) {
      get(cir[i], 0, 0, tmp);
      type = g[i - 1][0] < g[i - 1][1];
    } else {
      if (max(g[i - 1][0], g[i - 1][1]) + f[cir[i]][1] >
          g[i - 1][0] + f[cir[i]][0] +
              make_pair(1, gen[cir[i]] != gen[cir[i - 1]])) {
        get(cir[i], 0, 1, tmp);
        type = g[i - 1][0] < g[i - 1][1];
      } else {
        tmp.emplace_back(cir[i], cir[i - 1]);
        get(cir[i], 0, 0, tmp);
        type = 0;
      }
    }
  }
  if (tf)
    get(cir[0], 0, type, tmp);
  else
    get(cir[0], 0, 0, tmp);
}
void wk(int x) {
  mark(x);
  vector<int> cir;
  dfs(x, 0, cir);
  for (int x : cir) dp(x, 0);
  clr_g(cir.size());
  g[0][0] = f[cir[0]][0];
  g[0][1] = f[cir[0]][1];
  for (int i = 1; i < cir.size(); i++) {
    g[i][0] = max(g[i - 1][0], g[i - 1][1]) + f[cir[i]][0];
    g[i][1] = max(max(g[i - 1][0], g[i - 1][1]) + f[cir[i]][1],
                  g[i - 1][0] + f[cir[i]][0] +
                      make_pair(1, gen[cir[i]] != gen[cir[i - 1]]));
  }
  pair<int, int> s = max(g[cir.size() - 1][0], g[cir.size() - 1][1]);
  vector<pair<int, int> > tmp;
  int type = g[cir.size() - 1][0] < g[cir.size() - 1][1];
  path(tmp, cir, type, 1);
  clr_g(cir.size());
  g[0][0] = make_pair(-0x3f3f3f3f, 0);
  g[0][1] = make_pair(1, gen[cir[0]] != gen[cir.back()]) + f[cir[0]][0];
  for (int i = 1; i < cir.size(); i++) {
    g[i][0] = max(g[i - 1][0], g[i - 1][1]) + f[cir[i]][0];
    g[i][1] = max(max(g[i - 1][0], g[i - 1][1]) + f[cir[i]][1],
                  g[i - 1][0] + f[cir[i]][0] +
                      make_pair(1, gen[cir[i]] != gen[cir[i - 1]]));
  }
  if (g[cir.size() - 1][0] > s) {
    s = g[cir.size() - 1][0];
    tmp.clear();
    type = 0;
    path(tmp, cir, type, 0);
    tmp.emplace_back(cir[0], cir.back());
  }
  ans += s;
  for (pair<int, int> x : tmp) anse.push_back(x);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d%d", &x, &gen[i]);
    ins(i, x);
    ins(x, i);
  }
  for (int i = 1; i <= n; i++)
    if (!tf[i]) wk(i);
  printf("%d %d\n", ans.first, ans.second);
  for (pair<int, int> x : anse) printf("%d %d\n", x.first, x.second);
}