#include <bits/stdc++.h>
using namespace std;
long long int n;
vector<long long int> g[100010];
bool found = 0;
bool sex[100010];
long long int frnd[100010];
long long int x1, x2, x3;
map<pair<long long int, long long int>, long long int> m1;
long long int offset = 1000000;
long long int nxt = 1;
long long int dp[1000000];
vector<pair<long long int, long long int> > op;
void DFSH(long long int id, bool visited[], long long int p) {
  visited[id] = 1;
  for (auto v : g[id]) {
    if (visited[v] == 1 && v != p && found == 0) {
      x1 = p;
      x2 = id;
      x3 = v;
      found = 1;
    }
    if (visited[v] == 0) DFSH(v, visited, id);
  }
}
long long int fun(long long int u, long long int p) {
  long long int index = m1[make_pair(u, p)];
  if (index != 0) {
    return dp[index];
  }
  long long int a1 = 0;
  for (auto v : g[u]) {
    if (v != p && v != -1) {
      a1 += fun(v, u);
    }
  }
  long long int a2;
  long long int mx = a1;
  for (auto v : g[u]) {
    if (v != p && v != -1) {
      if (sex[u] != sex[v])
        a2 = offset + 1;
      else
        a2 = offset;
      a2 += a1 - fun(v, u);
      for (auto v1 : g[v]) {
        if (v1 != u && v1 != -1) {
          a2 += fun(v1, v);
        }
      }
      mx = max(mx, a2);
    }
  }
  m1[make_pair(u, p)] = nxt;
  dp[nxt] = mx;
  nxt++;
  return mx;
}
long long int pfun(long long int u, long long int p) {
  long long int index = m1[make_pair(u, p)];
  long long int xyz = dp[index];
  long long int a1 = 0;
  for (auto v : g[u]) {
    if (v != p && v != -1) {
      a1 += fun(v, u);
    }
  }
  if (xyz == a1) {
    for (auto v : g[u]) {
      if (v != p && v != -1) {
        pfun(v, u);
      }
    }
    return dp[index];
  }
  long long int a2;
  for (auto v : g[u]) {
    if (v != p && v != -1) {
      if (sex[u] != sex[v])
        a2 = offset + 1;
      else
        a2 = offset;
      a2 += a1 - fun(v, u);
      for (auto v1 : g[v]) {
        if (v1 != u && v1 != -1) {
          a2 += fun(v1, v);
        }
      }
      if (a2 == xyz) {
        op.push_back(make_pair(u + 1, v + 1));
        for (auto v1 : g[u]) {
          if (v1 != p && v1 != -1 && v1 != v) {
            pfun(v1, u);
          }
        }
        for (auto v1 : g[v]) {
          if (v1 != u && v1 != -1) {
            a2 += pfun(v1, v);
          }
        }
        return dp[index];
      }
    }
  }
  return dp[index];
}
void solve() {
  long long int ans = 0, final = 0;
  bool visited[100010] = {0};
  for (int i = 0; i < n; i++) {
    if (visited[i] == 1) continue;
    found = 0;
    DFSH(i, visited, -1);
    ans = 0;
    if (found == 1) {
      for (auto &v : g[x1]) {
        if (v == x2) {
          v = -1;
          break;
        }
      }
      for (auto &v : g[x2]) {
        if (v == x1) {
          v = -1;
          break;
        }
      }
      m1.clear();
      ans = max(fun(i, -1), ans);
      m1.clear();
      for (auto &v : g[x1]) {
        if (v == -1) {
          v = x2;
          break;
        }
      }
      for (auto &v : g[x2]) {
        if (v == -1) {
          v = x1;
        } else if (v == x3) {
          v = -1;
        }
      }
      for (auto &v : g[x3]) {
        if (v == x2) {
          v = -1;
        }
      }
      ans = max(ans, fun(i, -1));
      if (ans == fun(i, -1)) {
        pfun(i, -1);
      } else {
        for (auto &v : g[x3]) {
          if (v == -1) {
            v = x2;
            break;
          }
        }
        for (auto &v : g[x2]) {
          if (v == -1) {
            v = x3;
          } else if (v == x1) {
            v = -1;
          }
        }
        for (auto &v : g[x1]) {
          if (v == x2) {
            v = -1;
          }
        }
        m1.clear();
        fun(i, -1);
        pfun(i, -1);
      }
    } else {
      m1.clear();
      ans = max(ans, fun(i, -1));
      pfun(i, -1);
    }
    final += ans;
  }
  cout << final / offset << " ";
  cout << final % offset << endl;
  long long int sss = op.size();
  for (int i = 0; i < sss; i++) {
    cout << op[i].first << " " << op[i].second << endl;
  }
}
int main() {
  long long int x, s;
  cin >> n;
  map<pair<long long int, long long int>, long long int> m3;
  for (long long int i = 0; i < n; i++) {
    cin >> x >> s;
    x--;
    s--;
    sex[i] = s;
    if (m3[make_pair(i, x)] == 0) {
      m3[make_pair(i, x)] = 1;
      m3[make_pair(x, i)] = 1;
      g[i].push_back(x);
      g[x].push_back(i);
    }
    frnd[i] = x;
  }
  solve();
  return 0;
}
