#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
int n, u, v, v1, v2, v3, x, s, w[N], d[N], vis[N], mark[N];
pair<int, int> ans, dp1[N], dp2[N];
vector<pair<int, int> > pairs, e;
vector<int> g[N];
void del_edge(int x, int y) {
  for (int i = 0; i < g[x].size(); i++)
    if (g[x][i] == y) {
      g[x].erase(g[x].begin() + i);
      break;
    }
  for (int i = 0; i < g[y].size(); i++)
    if (g[y][i] == x) {
      g[y].erase(g[y].begin() + i);
      break;
    }
}
void dfs(int x, int par) {
  pair<int, int> p, e;
  dp1[x].first = dp1[x].second = dp2[x].first = dp2[x].second = 0;
  p.first = p.second = 0;
  for (int i = 0; i < g[x].size(); i++)
    if (g[x][i] != par) {
      dfs(g[x][i], x);
      e = make_pair(
          dp2[g[x][i]].first - dp1[g[x][i]].first + 1,
          dp2[g[x][i]].second - dp1[g[x][i]].second + (w[x] != w[g[x][i]]));
      if (e > p) p = e, d[x] = g[x][i];
      dp2[x].first += dp1[g[x][i]].first, dp2[x].second += dp1[g[x][i]].second;
      ;
    }
  dp1[x] = dp2[x];
  dp1[x].first += p.first, dp1[x].second += p.second;
  ;
}
void dfs1(int x, int par) {
  int io = 0;
  vis[x] = 1;
  for (int i = 0; i < g[x].size(); i++) {
    if ((vis[g[x][i]] && (g[x][i] != par || io == 1)))
      v1 = par, v2 = x, v3 = g[x][i];
    io += (g[x][i] == par);
  }
  for (int i = 0; i < g[x].size(); i++)
    if (!vis[g[x][i]]) dfs1(g[x][i], x);
}
void calc_pairs(int x, int par, int src) {
  if (dp1[x] == dp2[x]) src = 1;
  for (int i = 0; i < g[x].size(); i++)
    if (g[x][i] != par) {
      if (g[x][i] == d[x] && src == 0) {
        e.push_back(make_pair(x, g[x][i]));
        calc_pairs(g[x][i], x, 1);
      } else
        calc_pairs(g[x][i], x, 0);
    }
}
void solve(int root) {
  pair<int, int> res;
  res.first = res.second = 0, v1 = v2 = v3 = 0;
  e.clear();
  dfs1(root, 0);
  del_edge(v1, v2);
  dfs(root, 0);
  calc_pairs(root, 0, 0);
  res = dp1[root];
  g[v1].push_back(v2), g[v2].push_back(v1);
  del_edge(v2, v3);
  dfs(root, 0);
  if (dp1[root] > res) {
    e.clear();
    res = dp1[root];
    calc_pairs(root, 0, 0);
  }
  for (int i = 0; i < e.size(); i++) pairs.push_back(e[i]);
  ans.first += res.first, ans.second += res.second;
  ;
}
int main() {
  cin >> n;
  for (int i = 1; i < n + 1; i++) {
    cin >> u >> w[i];
    g[i].push_back(u);
    g[u].push_back(i);
    w[i]--;
  }
  for (int i = 1; i < n + 1; i++)
    if (!vis[i]) solve(i);
  cout << ans.first << " " << ans.second << endl;
  for (int i = 0; i < pairs.size(); i++)
    cout << pairs[i].first << " " << pairs[i].second << endl;
}
