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 Maxn = 1e5 + 5;
bool vis[Maxn], lp[Maxn];
int bf[Maxn], sx[Maxn], stk[Maxn];
vector<int> adj[Maxn];
vector<pair<int, int> > ansp, cansp[2];
int dp1[Maxn][2];
int dp2[Maxn][2];
int ps[Maxn][2];
int croot, cans[2], cp[2], flag;
int ans, p;
int n;
void findloop(int now) {
  stk[now] = 1;
  for (auto v : adj[now]) {
    if (stk[v] == 1) {
      lp[v] = true;
    } else
      findloop(v);
  }
  stk[now] = 2;
}
void dfs1(int now) {
  vis[now] = true;
  dp1[now][0] = dp1[now][1] = 0;
  int rez = 0;
  for (auto v : adj[now]) {
    if (v != croot) {
      dfs1(v);
      dp1[now][0] += max(dp1[v][0], dp1[v][1]);
    }
  }
  for (auto v : adj[now]) {
    if (v != croot) {
      rez = max(rez, dp1[now][0] - max(dp1[v][0], dp1[v][1]) + dp1[v][0] + 1);
    }
  }
  dp1[now][1] = rez;
}
void dfs2(int now) {
  dp2[now][0] = dp2[now][1] = 0;
  int rez1 = 0, rez2 = 0;
  for (auto v : adj[now]) {
    if (v != croot) {
      dfs2(v);
      if (dp1[v][0] > dp1[v][1])
        dp2[now][0] += dp2[v][0];
      else if (dp1[v][0] < dp1[v][1])
        dp2[now][0] += dp2[v][1];
      else
        dp2[now][0] += max(dp2[v][0], dp2[v][1]);
    }
  }
  for (auto v : adj[now]) {
    if (v != croot) {
      int x;
      if (dp1[v][0] > dp1[v][1]) {
        rez1 = dp1[now][0] + 1;
        x = dp2[now][0] + (sx[now] == sx[v] ? 0 : 1);
      } else if (dp1[v][0] < dp1[v][1]) {
        rez1 = dp1[now][0] - dp1[v][1] + dp1[v][0] + 1;
        x = dp2[now][0] - dp2[v][1] + dp2[v][0] + (sx[now] == sx[v] ? 0 : 1);
      } else {
        rez1 = dp1[now][0] + 1;
        x = dp2[now][0] - max(dp2[v][0], dp2[v][1]) + dp2[v][0] +
            (sx[now] == sx[v] ? 0 : 1);
      }
      if (rez1 == dp1[now][1]) {
        if (rez2 <= x) {
          rez2 = x;
          ps[now][flag] = v;
        }
      }
    }
  }
  dp2[now][1] = rez2;
}
void dfs3(int now) {
  if (dp1[now][0] > dp1[now][1]) {
    for (auto v : adj[now]) {
      if (v != croot) dfs3(v);
    }
    return;
  }
  if (dp1[now][0] < dp1[now][1]) {
    for (auto v : adj[now]) {
      if (v != croot) {
        if (v == ps[now][flag]) {
          cansp[flag].push_back(pair<int, int>(now, v));
          for (auto u : adj[v]) {
            if (u != croot) dfs3(u);
          }
        } else
          dfs3(v);
      }
    }
    return;
  }
  if (dp2[now][0] > dp2[now][1]) {
    for (auto v : adj[now]) {
      if (v != croot) dfs3(v);
    }
    return;
  } else {
    for (auto v : adj[now]) {
      if (v != croot) {
        if (v == ps[now][flag]) {
          cansp[flag].push_back(pair<int, int>(now, v));
          for (auto u : adj[v]) {
            if (u != croot) dfs3(u);
          }
        } else
          dfs3(v);
      }
    }
    return;
  }
  return;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", bf + i, sx + i);
    adj[bf[i]].push_back(i);
  }
  memset(vis, 0, sizeof vis);
  memset(stk, 0, sizeof stk);
  for (int i = 1; i <= n; i++) {
    if (!stk[i]) findloop(i);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i] && lp[i]) {
      flag = 0;
      croot = i;
      dfs1(i);
      dfs2(i);
      dfs3(i);
      if (dp1[i][0] > dp1[i][1]) {
        cans[flag] = dp1[i][0];
        cp[flag] = dp2[i][0];
      } else if (dp1[i][0] < dp1[i][1]) {
        cans[flag] = dp1[i][1];
        cp[flag] = dp2[i][1];
      } else {
        cans[flag] = dp1[i][0];
        cp[flag] = max(dp2[i][0], dp2[i][1]);
      }
      croot = bf[i];
      flag = 1;
      dfs1(bf[i]);
      dfs2(bf[i]);
      dfs3(bf[i]);
      if (dp1[bf[i]][0] > dp1[bf[i]][1]) {
        cans[flag] = dp1[bf[i]][0];
        cp[flag] = dp2[bf[i]][0];
      } else if (dp1[bf[i]][0] < dp1[bf[i]][1]) {
        cans[flag] = dp1[bf[i]][1];
        cp[flag] = dp2[bf[i]][1];
      } else {
        cans[flag] = dp1[bf[i]][0];
        cp[flag] = max(dp2[bf[i]][0], dp2[bf[i]][1]);
      }
      if (cans[0] > cans[1]) {
        ans += cans[0];
        for (auto v : cansp[0]) {
          if (sx[v.first] != sx[v.second]) p++;
          ansp.push_back(v);
        }
      } else if (cans[0] < cans[1]) {
        ans += cans[1];
        for (auto v : cansp[1]) {
          if (sx[v.first] != sx[v.second]) p++;
          ansp.push_back(v);
        }
      } else if (cp[0] >= cp[1]) {
        ans += cans[0];
        for (auto v : cansp[0]) {
          if (sx[v.first] != sx[v.second]) p++;
          ansp.push_back(v);
        }
      } else {
        ans += cans[1];
        for (auto v : cansp[1]) {
          if (sx[v.first] != sx[v.second]) p++;
          ansp.push_back(v);
        }
      }
      cansp[0].clear();
      cansp[1].clear();
    }
  }
  printf("%d %d\n", ans, p);
  for (auto v : ansp) {
    printf("%d %d\n", v.first, v.second);
  }
  return 0;
}