#include <bits/stdc++.h>
using namespace std;
const int N = 101000;
complex<int> f[N][2], ans;
int g[N], rt, _[N], s[N], v[N], n;
vector<int> c[N];
vector<complex<int> > p, q;
bool operator<(const complex<int>& a, const complex<int>& b) {
  return real(a) < real(b) || (real(a) == real(b) && imag(a) < imag(b));
}
void dfs(int u) {
  f[u][0] = f[u][1] = complex<int>(0, 0);
  g[u] = 0;
  v[u] = 1;
  for (int j : c[u]) {
    if (j == rt) continue;
    dfs(j);
    f[u][0] += f[j][1];
    complex<int> t = complex<int>(1, s[u] ^ s[j]) - f[j][1] + f[j][0];
    if (f[u][1] < t) f[u][1] = t, g[u] = j;
  }
  f[u][1] += f[u][0];
}
void gans(int u, bool b) {
  for (int j : c[u]) {
    if (j == rt) continue;
    if (!b || g[u] != j)
      gans(j, 1);
    else
      p.push_back(complex<int>(u, j)), gans(j, 0);
  }
}
void solve(int u) {
  for (; !v[u]; u = _[u]) v[u] = 1;
  complex<int> r(0, 0);
  for (int t = 0; t < 2; t++) {
    rt = u;
    dfs(u);
    if (r < f[u][1]) r = f[u][1], p.clear(), gans(u, 1);
    u = _[u];
  }
  for (complex<int> j : p) q.push_back(j);
  ans += r;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n + 1; i++)
    scanf("%d%d", _ + i, s + i), --s[i], c[_[i]].push_back(i);
  for (int i = 1; i < n + 1; i++)
    if (!v[i]) solve(i);
  printf("%d %d\n", real(ans), imag(ans));
  for (complex<int> j : q) printf("%d %d\n", real(j), imag(j));
}
