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;
inline pair<int, int> operator+(const pair<int, int> &x,
                                const pair<int, int> &y) {
  return make_pair(x.first + y.first, x.second + y.second);
}
inline pair<int, int> operator-(const pair<int, int> &x,
                                const pair<int, int> &y) {
  return make_pair(x.first - y.first, x.second - y.second);
}
inline pair<int, int> operator+=(pair<int, int> &x, const pair<int, int> &y) {
  x.first += y.first;
  x.second += y.second;
  return x;
}
inline bool operator<(const pair<int, int> &x, const pair<int, int> &y) {
  return (x.first < y.first || (x.first == y.first && x.second < y.second));
}
int n;
const int MAXN = 100005;
int f[MAXN], type[MAXN];
bool vis[MAXN];
bool incyc[MAXN];
vector<int> G[MAXN];
pair<int, int> dp[MAXN][2];
int who[MAXN];
int anc;
inline void dfsDP(int u, int pa) {
  vis[u] = 1;
  dp[u][0] = dp[u][1] = make_pair(0, 0);
  pair<int, int> best = make_pair(0, 0);
  for (int i = 0; i < G[u].size(); ++i) {
    int v = G[u][i];
    if (v == pa || v == anc) continue;
    dfsDP(v, u);
    dp[u][0] += dp[v][1];
    if (best < dp[v][0] - dp[v][1] + make_pair(1, type[u] ^ type[v])) {
      best = dp[v][0] - dp[v][1] + make_pair(1, type[u] ^ type[v]);
      who[u] = v;
    }
  }
  dp[u][1] = dp[u][0] + best;
}
vector<pair<int, int> > res, vec;
inline void dfsPath(int u, int pa, int cho) {
  if (!cho || dp[u][0] == dp[u][1]) {
    for (int i = 0; i < G[u].size(); ++i) {
      int v = G[u][i];
      if (v == pa || v == anc) continue;
      dfsPath(v, u, 1);
    }
  } else {
    vec.push_back(make_pair(u, who[u]));
    for (int i = 0; i < G[u].size(); ++i) {
      int v = G[u][i];
      if (v == pa || v == anc) continue;
      if (v == who[u])
        dfsPath(v, u, 0);
      else
        dfsPath(v, u, 1);
    }
  }
}
pair<int, int> ans = make_pair(0, 0);
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= (n); ++i) {
    cin >> f[i] >> type[i];
    --type[i];
    G[f[i]].push_back(i);
    G[i].push_back(f[i]);
  }
  for (int x = 1; x <= (n); ++x)
    if (!vis[x]) {
      int u = x;
      vis[u] = 1;
      while (!vis[f[u]]) {
        u = f[u];
        vis[u] = 1;
      }
      incyc[u] = 1;
      while (!incyc[f[u]]) {
        u = f[u];
        incyc[u] = 1;
      }
      vector<int> tmpV = G[u];
      G[u].clear();
      int cntCyc = 0;
      for (int i = 0; i < tmpV.size(); ++i) {
        if (!incyc[tmpV[i]])
          G[u].push_back(tmpV[i]);
        else
          ++cntCyc;
      }
      anc = u;
      assert(cntCyc == 2);
      for (int i = 0; i < tmpV.size(); ++i)
        if (incyc[tmpV[i]]) {
          G[u].push_back(tmpV[i]);
          break;
        }
      dfsDP(u, 0);
      pair<int, int> tmp = dp[u][1];
      vec.clear();
      dfsPath(u, 0, 1);
      G[u].pop_back();
      for (int i = tmpV.size() - 1; i >= 0; --i)
        if (incyc[tmpV[i]]) {
          G[u].push_back(tmpV[i]);
          break;
        }
      dfsDP(u, 0);
      if (tmp < dp[u][1]) {
        ans += dp[u][1];
        vec.clear();
        dfsPath(u, 0, 1);
      } else
        ans += tmp;
      for (int i = 0; i < vec.size(); ++i) res.push_back(vec[i]);
    }
  cout << ans.first << ' ' << ans.second << endl;
  for (int i = 1; i <= (ans.first); ++i)
    cout << res[i - 1].first << ' ' << res[i - 1].second << endl;
  return 0;
}