#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100100;
const int INF = 1000000007;
int f[MAXN], s[MAXN], tag[MAXN], pre[MAXN];
vector<int> c[MAXN];
pair<int, int> dp[MAXN][2];
pair<int, int> operator+(const pair<int, int>& lhs, const pair<int, int>& rhs) {
  return make_pair(lhs.first + rhs.first, lhs.second + rhs.second);
}
pair<int, int> operator-(const pair<int, int>& lhs, const pair<int, int>& rhs) {
  return make_pair(lhs.first - rhs.first, lhs.second - rhs.second);
}
void gao(int v) {
  if (tag[v] == 0) {
    tag[v] = 1;
  }
  dp[v][0] = make_pair(0, 0);
  dp[v][1] = make_pair(0, 0);
  pre[v] = -1;
  for (vector<int>::const_iterator w = c[v].begin(); w != c[v].end(); ++w) {
    if (tag[*w] == -1) {
      continue;
    }
    gao(*w);
    dp[v][0] = dp[v][0] + dp[*w][1];
    pair<int, int> tmp =
        make_pair(1, s[v] == s[*w] ? 0 : 1) - (dp[*w][1] - dp[*w][0]);
    if (dp[v][1] < tmp) {
      dp[v][1] = tmp;
      pre[v] = *w;
    }
  }
  dp[v][1] = dp[v][1] + dp[v][0];
}
void dump(int v, bool f, vector<pair<int, int> >& ret) {
  for (vector<int>::const_iterator w = c[v].begin(); w != c[v].end(); ++w) {
    if (tag[*w] == -1) {
      continue;
    }
    if (!f || *w != pre[v]) {
      dump(*w, true, ret);
    } else {
      ret.push_back(make_pair(v, *w));
      dump(*w, false, ret);
    }
  }
}
pair<int, int> cdp[MAXN][2];
int cpre[MAXN];
pair<int, int> ans;
vector<pair<int, int> > todo;
void solve(int v) {
  while (tag[v] == 0) {
    tag[v] = 1;
    v = f[v];
  }
  vector<int> circ(1, v);
  for (int w = f[v]; w != v; w = f[w]) {
    circ.push_back(w);
  }
  pair<int, int> best = make_pair(0, 0);
  vector<pair<int, int> > how;
  for (int i = 0; i < 2; ++i) {
    int w = f[v];
    c[w].erase(find(c[w].begin(), c[w].end(), v));
    gao(v);
    if (best < dp[v][1]) {
      best = dp[v][1];
      how.clear();
      dump(v, true, how);
    }
    c[w].push_back(v);
    v = w;
  }
  ans = ans + best;
  todo.insert(todo.end(), how.begin(), how.end());
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &f[i], &s[i]);
    c[f[i]].push_back(i);
  }
  ans = make_pair(0, 0);
  todo.clear();
  for (int i = 1; i <= n; ++i) {
    if (tag[i] == 0) {
      solve(i);
    }
  }
  printf("%d %d\n", ans.first, ans.second);
  for (vector<pair<int, int> >::const_iterator it = todo.begin();
       it != todo.end(); ++it) {
    printf("%d %d\n", it->first, it->second);
  }
  return 0;
}
