Problem: There are n students in Polycarp's class (including himself). A few days ago all students wrote an essay "My best friend". Each student's essay was dedicated to one of the students of class, to his/her best friend. Note that student b's best friend is not necessarily student a, if a's best friend is b.

And now the teacher leads the whole class to the museum of the history of sports programming. Exciting stories of legendary heroes await the students: tourist, Petr, tomek, SnapDragon — that's who they will hear about!

The teacher decided to divide students into pairs so that each pair consisted of a student and his best friend. She may not be able to split all the students into pairs, it's not a problem — she wants to pick out the maximum number of such pairs. If there is more than one variant of doing so, she wants to pick out the pairs so that there were as much boy-girl pairs as possible. Of course, each student must not be included in more than one pair.

Input

The first line contains an integer n (2 ≤ n ≤ 105), n is the number of students per class. Next, n lines contain information about the students, one per line. Each line contains two integers fi, si (1 ≤ fi ≤ n, fi ≠ i, 1 ≤ si ≤ 2), where fi is the number of i-th student's best friend and si denotes the i-th pupil's sex (si = 1 for a boy and si = 2 for a girl).

Output

Print on the first line two numbers t, e, where t is the maximum number of formed pairs, and e is the maximum number of boy-girl type pairs among them. Then print t lines, each line must contain a pair ai, bi (1 ≤ ai, bi ≤ n), they are numbers of pupils in the i-th pair. Print the pairs in any order. Print the numbers in pairs in any order. If there are several solutions, output any of them.

Examples

Input

5
5 2
3 2
5 1
2 1
4 2


Output

2 2
5 3
4 2


Input

6
5 2
3 2
5 1
2 1
4 2
3 1


Output

3 1
4 2
5 1
3 6


Input

8
2 2
3 2
5 1
3 1
6 1
5 1
8 2
7 1


Output

4 1
5 6
3 4
2 1
7 8

Note

The picture corresponds to the first sample. On the picture rhomb stand for boys, squares stand for girls, arrows lead from a pupil to his/her best friend. Bold non-dashed arrows stand for pairs in the answer. 

<image>

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int Imx = 2147483647;
const long long Lbig = 2e18;
const int mod = 1e9 + 7;
struct fastio {
  char s[100000];
  int it, len;
  fastio() { it = len = 0; }
  inline char get() {
    if (it < len) return s[it++];
    it = 0;
    len = fread(s, 1, 100000, stdin);
    if (len == 0)
      return EOF;
    else
      return s[it++];
  }
  bool notend() {
    char c = get();
    while (c == ' ' || c == '\n') c = get();
    if (it > 0) it--;
    return c != EOF;
  }
} _buff;
inline long long getnum() {
  long long r = 0;
  bool ng = 0;
  char c;
  c = _buff.get();
  while (c != '-' && (c < '0' || c > '9')) c = _buff.get();
  if (c == '-') ng = 1, c = _buff.get();
  while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = _buff.get();
  return ng ? -r : r;
}
template <class T>
inline void putnum(T x) {
  if (x < 0) putchar('-'), x = -x;
  register short a[20] = {}, sz = 0;
  while (x) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
inline char getreal() {
  char c = _buff.get();
  while (c <= 32) c = _buff.get();
  return c;
}
long long qpow(long long x, long long k) {
  return k == 0 ? 1
                : 1ll * qpow(1ll * x * x % mod, k >> 1) * (k & 1 ? x : 1) % mod;
}
int n, m;
int to[100111], ty[100111];
vector<int> con[100111];
vector<int> g[100111];
int f[100111];
int gf(int x) { return f[x] == x ? x : f[x] = gf(f[x]); }
vector<pair<int, int> > ansv;
int ans;
bool vis[100111], onc[100111], inst[100111];
vector<int> cyc;
void findcycle(int x) {
  vis[x] = 1;
  inst[x] = 1;
  if (inst[to[x]]) {
    int p = to[x];
    while (true) {
      cyc.push_back(p);
      onc[p] = 1;
      p = to[p];
      if (p == to[x]) break;
    }
  } else if (!vis[to[x]])
    findcycle(to[x]);
  inst[x] = 0;
}
pair<int, int> dp[100111][2];
int mt[100111];
void dfs(int x) {
  vis[x] = 1;
  dp[x][1] = dp[x][0] = make_pair(0, 0);
  for (int i = 0; i < con[x].size(); i++) {
    int u = con[x][i];
    if (onc[u]) continue;
    if (!vis[u]) dfs(u);
    dp[x][0].first += dp[u][0].first;
    dp[x][0].second += dp[u][0].second;
    pair<int, int> tmp =
        make_pair(dp[x][1].first + dp[u][1].first + 1,
                  dp[x][1].second + dp[u][1].second + (ty[x] != ty[u]));
    if (tmp > dp[x][0]) mt[x] = u, dp[x][0] = tmp;
    dp[x][1].first += dp[u][0].first;
    dp[x][1].second += dp[u][0].second;
  }
}
pair<int, int> dp2[100111];
int from[100111], mt2[100111];
pair<int, int> solve2(vector<int> v) {
  dp2[v[0]] = dp[v[0]][0];
  from[0] = -1;
  for (int i = 1; i < v.size(); i++) {
    pair<int, int> tmp2 = make_pair(
        (i >= 2 ? dp2[v[i - 2]].first : 0) + 1,
        (i >= 2 ? dp2[v[i - 2]].second : 0) + (ty[v[i]] != ty[v[i - 1]]));
    tmp2.first += dp[v[i]][1].first + dp[v[i - 1]][1].first;
    tmp2.second += dp[v[i]][1].second + dp[v[i - 1]][1].second;
    pair<int, int> tmp1 = dp2[v[i - 1]];
    tmp1.first += dp[v[i]][0].first;
    tmp1.second += dp[v[i]][0].second;
    if (tmp2 >= tmp1)
      dp2[v[i]] = tmp2, from[i] = i - 2;
    else
      dp2[v[i]] = tmp1, from[i] = i - 1;
  }
  int p = int(v.size()) - 1;
  assert(p >= 0);
  while (p >= 0) {
    if (from[p] == p - 2)
      mt2[v[p]] = v[p - 1], mt2[v[p - 1]] = -v[p];
    else
      mt2[v[p]] = 0;
    p = from[p];
  }
  return dp2[v[int(v.size()) - 1]];
}
void calcans(int x) {
  if (mt[x]) {
    ansv.push_back(make_pair(x, mt[x]));
  }
  for (int i = 0; i < con[x].size(); i++) {
    int u = con[x][i];
    if (onc[u]) continue;
    if (u == mt[x]) {
      for (int j = 0; j < con[u].size(); j++) calcans(con[u][j]);
    } else
      calcans(u);
  }
}
void solve(vector<int> v) {
  if (!v.size()) return;
  cyc.clear();
  for (int i = 0; i < v.size(); i++) {
    if (!vis[v[i]]) {
      findcycle(v[i]);
    }
  }
  for (int i = 0; i < v.size(); i++) vis[v[i]] = 0;
  for (int i = 0; i < v.size(); i++) {
    if (!vis[v[i]] && onc[v[i]]) {
      dfs(v[i]);
    }
  }
  vector<int> ncyc;
  for (int i = 1; i < cyc.size(); i++) ncyc.push_back(cyc[i]);
  ncyc.push_back(cyc[0]);
  pair<int, int> cans1 = solve2(cyc);
  pair<int, int> cans2 = solve2(ncyc);
  if (cans1 >= cans2) solve2(cyc);
  for (int i = 0; i < cyc.size(); i++) {
    int x = cyc[i];
    if (mt2[x] > 0) {
      ansv.push_back(make_pair(x, mt2[x]));
    }
    if (mt2[x] == 0)
      calcans(x);
    else {
      for (int j = 0; j < con[x].size(); j++) {
        if (!onc[con[x][j]]) {
          calcans(con[x][j]);
        }
      }
    }
  }
  ans += max(cans1, cans2).first;
}
int main() {
  n = getnum();
  for (int i = 1; i <= n; i++) {
    to[i] = getnum(), ty[i] = getnum();
    f[i] = i;
    con[to[i]].push_back(i);
  }
  for (int i = 1; i <= n; i++) f[gf(i)] = gf(to[i]);
  for (int i = 1; i <= n; i++) g[gf(i)].push_back(i);
  for (int i = 1; i <= n; i++) solve(g[i]);
  int cnt = 0;
  for (int i = 0; i < ansv.size(); i++)
    cnt += ty[ansv[i].first] != ty[ansv[i].second];
  assert(ans == ansv.size());
  putnum(ansv.size()), putchar(' '), putnum(cnt), putchar('\n');
  for (int i = 0; i < ansv.size(); i++)
    putnum(ansv[i].first), putchar(' '), putnum(ansv[i].second), putchar('\n');
  return 0;
}