#include <bits/stdc++.h>
using namespace std;
int v[100005], son[100005], ed[100005], nt[100005], fa[100005], s[100005],
    g[100005], n, w, len, root;
pair<int, int> e[100005], o[100005], f[100005][2], ans;
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);
}
void dfs(int first) {
  v[first] = 1, f[first][1] = f[first][0] = make_pair(0, 0), g[first] = 0;
  for (int second = son[first]; second; second = nt[second])
    if (second - root) {
      dfs(second), f[first][0] = f[first][0] + f[second][1];
      pair<int, int> t =
          f[second][0] + make_pair(1, s[first] ^ s[second]) - f[second][1];
      if (t > f[first][1]) f[first][1] = t, g[first] = second;
    }
  f[first][1] = f[first][1] + f[first][0];
}
void get(int first, int flag) {
  for (int second = son[first]; second; second = nt[second])
    if (second - root)
      if (flag && g[first] == second)
        e[w++] = make_pair(first, second), get(second, 0);
      else
        get(second, 1);
}
void match(int first) {
  for (; !v[first]; first = fa[first]) v[first] = 1;
  pair<int, int> res(0, 0);
  for (int T = 2; T; T--, first = fa[first]) {
    root = first, dfs(first);
    if (f[first][1] > res) res = f[first][1], w = 0, get(first, 1);
  }
  ans = ans + res;
  for (int i = 0; i < w; i++) o[len++] = e[i];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", fa + i, s + i), s[i]--, nt[i] = son[fa[i]], son[fa[i]] = i;
  for (int i = 1; i <= n; i++)
    if (!v[i]) match(i);
  printf("%d %d\n", ans.first, ans.second);
  for (int i = 0; i < len; i++) printf("%d %d\n", o[i].first, o[i].second);
  return 0;
}
