#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);
}
pair<int, int> dp[100010][2];
int vis[100010], bestchoice[100010], ty[100010], pre[100010];
pair<int, int> ffinal;
vector<int> e[100010];
vector<pair<int, int> > fres;
void solve(int root, int cur) {
  vis[cur] = 1;
  dp[cur][0] = make_pair(0, 0);
  for (typeof((e[cur]).begin()) it = (e[cur]).begin(); it != (e[cur]).end();
       it++)
    if (*it != root) {
      solve(root, *it);
      dp[cur][0] = dp[cur][0] + dp[*it][1];
    }
  dp[cur][1] = dp[cur][0];
  bestchoice[cur] = 0;
  for (typeof((e[cur]).begin()) it = (e[cur]).begin(); it != (e[cur]).end();
       it++)
    if (*it != root) {
      pair<int, int> tmp = dp[cur][0] - dp[*it][1] + dp[*it][0] +
                           make_pair(1, ty[cur] ^ ty[*it]);
      if (tmp > dp[cur][1]) {
        dp[cur][1] = tmp;
        bestchoice[cur] = *it;
      }
    }
}
void gen(int root, int cur, int c) {
  if (c == 0 || bestchoice[cur] == 0) {
    for (typeof((e[cur]).begin()) it = (e[cur]).begin(); it != (e[cur]).end();
         it++)
      if (*it != root) gen(root, *it, 1);
  } else {
    fres.push_back(make_pair(cur, bestchoice[cur]));
    gen(root, bestchoice[cur], 0);
    for (typeof((e[cur]).begin()) it = (e[cur]).begin(); it != (e[cur]).end();
         it++)
      if (*it != root && *it != bestchoice[cur]) gen(root, *it, 1);
  }
}
void work(int x) {
  pair<int, int> final = make_pair(-1, 0);
  int bestx;
  while (!vis[x]) vis[x] = 1, x = pre[x];
  for (int i = (0); i <= (1); i++) {
    solve(x, x);
    if (dp[x][1] > final) {
      final = dp[x][1];
      bestx = x;
    }
    x = pre[x];
  }
  solve(bestx, bestx);
  gen(bestx, bestx, 1);
  ffinal = ffinal + final;
}
void lemon() {
  int n;
  scanf("%d", &n);
  for (int i = (1); i <= (n); i++) {
    int x;
    scanf("%d%d", &x, &ty[i]);
    ty[i]--;
    e[x].push_back(i);
    pre[i] = x;
  }
  memset(vis, 0, sizeof vis);
  for (int i = (1); i <= (n); i++)
    if (!vis[i]) work(i);
  printf("%d %d\n", ffinal.first, ffinal.second);
  for (typeof((fres).begin()) it = (fres).begin(); it != (fres).end(); it++)
    printf("%d %d\n", it->first, it->second);
}
int main() {
  ios::sync_with_stdio(true);
  lemon();
  return 0;
}
