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;
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);
}
int n;
int f[100010], s[100010];
vector<int> ke[100010];
int cur, ROOT;
pair<int, int> F[2][100010][2];
bool dt[2][100010][2];
pair<int, int> calc(int i, bool b) {
  if (dt[cur][i][b]) return F[cur][i][b];
  dt[cur][i][b] = true;
  pair<int, int> res = make_pair(0, 0);
  if (!b) {
    for (int k = 0; k < (ke[i].size()); ++k) {
      int j = ke[i][k];
      if (j != ROOT && f[j] == i) {
        res = res + max(calc(j, 0), calc(j, 1));
      }
    }
  } else {
    pair<int, int> total = make_pair(0, 0);
    for (int k = 0; k < (ke[i].size()); ++k) {
      int j = ke[i][k];
      if (j != ROOT && f[j] == i) {
        total = total + max(calc(j, 0), calc(j, 1));
      }
    }
    res = total;
    for (int k = 0; k < (ke[i].size()); ++k) {
      int j = ke[i][k];
      if (j != ROOT && f[j] == i) {
        res = max(res, total - max(calc(j, 0), calc(j, 1)) + calc(j, 0) +
                           make_pair(1, s[j] != s[i] ? 1 : 0));
      }
    }
  }
  return F[cur][i][b] = res;
}
void truy(int i, int b, vector<pair<int, int> > &v) {
  pair<int, int> res = calc(i, b);
  if (!b) {
    for (int k = 0; k < (ke[i].size()); ++k) {
      int j = ke[i][k];
      if (j != ROOT && f[j] == i) {
        pair<int, int> p0 = calc(j, 0);
        pair<int, int> p1 = calc(j, 1);
        if (p0 > p1)
          truy(j, 0, v);
        else
          truy(j, 1, v);
      }
    }
  } else {
    pair<int, int> total = make_pair(0, 0);
    for (int k = 0; k < (ke[i].size()); ++k) {
      int j = ke[i][k];
      if (j != ROOT && f[j] == i) {
        total = total + max(calc(j, 0), calc(j, 1));
      }
    }
    if (total == res) {
      for (int k = 0; k < (ke[i].size()); ++k) {
        int j = ke[i][k];
        if (j != ROOT && f[j] == i) {
          pair<int, int> p0 = calc(j, 0);
          pair<int, int> p1 = calc(j, 1);
          if (p0 > p1)
            truy(j, 0, v);
          else
            truy(j, 1, v);
        }
      }
    } else {
      for (int k = 0; k < (ke[i].size()); ++k) {
        int j = ke[i][k];
        if (j != ROOT && f[j] == i) {
          pair<int, int> z = total - max(calc(j, 0), calc(j, 1)) + calc(j, 0) +
                             make_pair(1, s[j] != s[i] ? 1 : 0);
          if (z == res) {
            v.push_back(make_pair(i, j));
            for (int t = 0; t < (ke[i].size()); ++t) {
              int j = ke[i][t];
              if (j != ROOT && f[j] == i) {
                if (t == k)
                  truy(j, 0, v);
                else {
                  pair<int, int> p0 = calc(j, 0);
                  pair<int, int> p1 = calc(j, 1);
                  if (p0 > p1)
                    truy(j, 0, v);
                  else
                    truy(j, 1, v);
                }
              }
            }
            break;
          }
        }
      }
    }
  }
}
void solve(int root, pair<int, int> &p, vector<pair<int, int> > &v) {
  ROOT = root;
  pair<int, int> p0 = calc(root, 0);
  pair<int, int> p1 = calc(root, 1);
  if (p0 > p1) {
    p = p0;
    truy(root, 0, v);
  } else {
    p = p1;
    truy(root, 1, v);
  }
}
bool mark[100010];
bool vs[100010];
void dfs(int i) {
  vs[i] = true;
  for (int k = 0; k < (ke[i].size()); ++k) {
    int j = ke[i][k];
    if (!vs[j]) dfs(j);
  }
}
void process(int si, pair<int, int> &p, vector<pair<int, int> > &v) {
  dfs(si);
  int tr = -1;
  while (!mark[si]) {
    mark[si] = true;
    tr = si;
    si = f[si];
  }
  pair<int, int> p1, p2;
  vector<pair<int, int> > v1, v2;
  cur = 1 - cur;
  solve(tr, p1, v1);
  p = p1;
  v = v1;
  cur = 1 - cur;
  solve(si, p2, v2);
  if (p1 < p2) {
    p = p2, v = v2;
  } else {
    p = p1, v = v1;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = (1); i <= (n); ++i) scanf("%d%d", f + i, s + i);
  memset(F, -1, sizeof(F));
  for (int i = (1); i <= (n); ++i) {
    if (f[f[i]] != i) ke[i].push_back(f[i]), ke[f[i]].push_back(i);
  }
  for (int i = (1); i <= (n); ++i) {
    if (f[f[i]] == i) ke[i].push_back(f[i]);
  }
  pair<int, int> res = make_pair(0, 0);
  vector<pair<int, int> > vr;
  for (int i = (1); i <= (n); ++i)
    if (!vs[i]) {
      pair<int, int> p;
      vector<pair<int, int> > v;
      process(i, p, v);
      res = res + p;
      for (int j = 0; j < (v.size()); ++j) vr.push_back(v[j]);
    }
  printf("%d %d\n", res.first, res.second);
  for (int i = 0; i < (vr.size()); ++i) {
    printf("%d %d\n", vr[i].first, vr[i].second);
  }
  return 0;
}