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 MAXN = 100000 + 10;
vector<int> e[MAXN];
vector<vector<int> > cycle;
int gender[MAXN], pal[MAXN], on_cycle[MAXN], n;
int father[MAXN], vis[MAXN];
void type_dfs(int v) {
  vis[v] = 1;
  for (int i = 0; i < ((int)(e[v]).size()); ++i) {
    if (vis[e[v][i]]) {
      if (vis[e[v][i]] == 2) {
        continue;
      }
      vector<int> vec;
      for (int j = v; 1; j = father[j]) {
        vec.push_back(j);
        on_cycle[j] = 1;
        if (j == e[v][i]) {
          break;
        }
      }
      reverse(vec.begin(), vec.end());
      cycle.push_back(vec);
    } else {
      father[e[v][i]] = v;
      type_dfs(e[v][i]);
    }
  }
  vis[v] = 2;
}
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);
}
int visdp[MAXN][2];
pair<int, int> f[MAXN][2];
pair<int, int> g[MAXN], h[MAXN];
pair<int, int> dp(int i, int j) {
  if (visdp[i][j]) {
    return f[i][j];
  }
  visdp[i][j] = 1;
  f[i][j] = make_pair(0, 0);
  for (int k = 0; k < ((int)(e[i]).size()); ++k) {
    int v = e[i][k];
    if (on_cycle[v]) {
      continue;
    }
    f[i][j] = f[i][j] + dp(v, 0);
  }
  if (j == 0) {
    pair<int, int> best = f[i][j];
    for (int k = 0; k < ((int)(e[i]).size()); ++k) {
      int v = e[i][k];
      pair<int, int> w = make_pair(1, gender[i] != gender[v]);
      if (on_cycle[v]) {
        continue;
      }
      best = max(best, f[i][j] - dp(v, 0) + dp(v, 1) + w);
    }
    f[i][j] = best;
  }
  return f[i][j];
}
vector<pair<int, int> > final_ans;
void construct(int i, int j) {
  pair<int, int> sum = make_pair(0, 0);
  for (int k = 0; k < ((int)(e[i]).size()); ++k) {
    int v = e[i][k];
    if (on_cycle[v]) {
      continue;
    }
    sum = sum + dp(v, 0);
  }
  if (sum == f[i][j]) {
    for (int k = 0; k < ((int)(e[i]).size()); ++k) {
      int v = e[i][k];
      if (on_cycle[v]) {
        continue;
      }
      construct(v, 0);
    }
  } else {
    bool found = 0;
    for (int k = 0; k < ((int)(e[i]).size()); ++k) {
      int v = e[i][k];
      if (on_cycle[v]) {
        continue;
      }
      pair<int, int> w = make_pair(1, gender[i] != gender[v]);
      if (!found && sum - dp(v, 0) + dp(v, 1) + w == f[i][j]) {
        final_ans.push_back(make_pair(v, i));
        construct(v, 1);
        found = 1;
      } else {
        construct(v, 0);
      }
    }
  }
}
void pre_construct(vector<int> c, pair<int, int> g[], int n) {
  int i = n - 1;
  while (i >= 1) {
    if (g[i] == g[i - 1] + dp(c[i], 0)) {
      construct(c[i], 0);
      --i;
    } else {
      final_ans.push_back(make_pair(c[i], c[i - 1]));
      construct(c[i], 1);
      construct(c[i - 1], 1);
      i -= 2;
    }
  }
  if (i == 0) {
    construct(c[0], 0);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &pal[i], &gender[i]);
    e[pal[i]].push_back(i);
  }
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      type_dfs(i);
    }
  }
  pair<int, int> ans;
  for (int i = 0; i < ((int)(cycle).size()); ++i) {
    vector<int> c = cycle[i];
    g[0] = dp(c[0], 0);
    for (int j = 1; j < ((int)(c).size()); ++j) {
      g[j] = g[j - 1] + dp(c[j], 0);
      pair<int, int> x = j > 1 ? g[j - 2] : make_pair(0, 0);
      pair<int, int> y = make_pair(1, gender[c[j - 1]] != gender[c[j]]);
      pair<int, int> z = dp(c[j], 1) + dp(c[j - 1], 1);
      if (x + y + z > g[j]) {
        g[j] = x + y + z;
      }
    }
    vector<int> d(c.begin() + 1, c.end());
    d.push_back(c[0]);
    h[0] = dp(d[0], 0);
    for (int j = 1; j < ((int)(d).size()); ++j) {
      h[j] = h[j - 1] + dp(d[j], 0);
      pair<int, int> x = j > 1 ? h[j - 2] : make_pair(0, 0);
      pair<int, int> y = make_pair(1, gender[d[j - 1]] != gender[d[j]]);
      pair<int, int> z = dp(d[j], 1) + dp(d[j - 1], 1);
      if (x + y + z > h[j]) {
        h[j] = x + y + z;
      }
    }
    if (g[((int)(c).size()) - 1] >= h[((int)(d).size()) - 1]) {
      pre_construct(c, g, ((int)(c).size()));
    } else {
      pre_construct(d, h, ((int)(d).size()));
    }
    ans = ans + max(g[((int)(c).size()) - 1], h[((int)(d).size()) - 1]);
  }
  printf("%d %d\n", ans.first, ans.second);
  for (int i = 0; i < ((int)(final_ans).size()); ++i) {
    printf("%d %d\n", final_ans[i].first, final_ans[i].second);
  }
  return 0;
}