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 inf = 0x0f0f0f0f;
template <class S, class T>
inline pair<S, T> MP(S a, T b) {
  return make_pair(a, b);
}
template <class T>
inline auto LX(T &X) -> decltype((X.first)) {
  return X.first;
}
template <class T>
inline auto LX(const T &X) -> const decltype((X.first)) {
  return X.first;
}
template <class T>
inline auto RX(T &X) -> decltype((X.second)) {
  return X.second;
}
template <class T>
inline auto RX(const T &X) -> const decltype((X.second)) {
  return X.second;
}
template <class T>
inline void RST(T &a) {
  memset(a, 0, sizeof(a));
}
template <class T>
inline void RST1(T &a) {
  memset(a, -1, sizeof(a));
}
template <class T>
inline void SINF(T &a) {
  memset(a, 0x0f, sizeof(a));
}
template <class S, class T>
inline void RST(S &a, T b) {
  fill((T *)a, (T *)a + (sizeof(a) / sizeof(b)), b);
}
template <class T>
inline void CLR(T &a) {
  a.clear();
}
template <class S, class T>
inline void CPY(S &a, T &b) {
  memcpy(a, b, sizeof(a));
}
template <class S, class T>
inline bool chkmin(S &a, T b) {
  return b < a ? a = b, 1 : 0;
}
template <class S, class T>
inline bool chkmax(S &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
inline T sqr(T x) {
  return x * x;
}
template <class T>
inline int SZ(T &a) {
  return (int)(a.size());
}
template <class T>
inline bool EPT(T &a) {
  return a.empty();
}
template <class T>
inline auto FRT(T &a) -> decltype((a.front())) {
  return a.front();
}
template <class T>
inline auto FRT(const T &a) -> const decltype((a.front())) {
  return a.front();
}
template <class T>
inline auto BK(T &a) -> decltype(a.back()) & {
  return a.back();
}
template <class T>
inline auto BK(const T &a) -> const decltype((a.back())) {
  return a.back();
}
template <class T>
inline auto TOP(T &a) -> decltype((a.top())) {
  return a.top();
}
template <class T>
inline auto TOP(const T &a) -> const decltype((a.top())) {
  return a.top();
}
int n;
int s[100010];
unordered_set<int> g[100010];
int p, q;
int vis[100010], nn;
pair<int, int> operator+(const pair<int, int> &a, const pair<int, int> &b) {
  return MP(LX(a) + LX(b), RX(a) + RX(b));
}
pair<int, int> operator-(const pair<int, int> &a, const pair<int, int> &b) {
  return MP(LX(a) - LX(b), RX(a) - RX(b));
}
pair<int, int> &operator+=(pair<int, int> &a, const pair<int, int> &b) {
  return LX(a) += LX(b), RX(a) += RX(b), a;
}
int fa[100010], d[100010];
void dfs(int u, int f) {
  fa[u] = f;
  d[u] = d[f] + 1;
  vis[u] = 1;
  for (int v : g[u])
    if (!vis[v])
      dfs(v, u);
    else if (v != f)
      p = u, q = v;
}
pair<int, int> f[100010][3];
int pre[100010][3];
void dp(int u, int fa) {
  RST(f[u]), RST(pre[u]);
  for (int v : g[u])
    if (v != fa) {
      dp(v, u);
      f[u][0] += f[v][2];
    }
  for (int v : g[u])
    if (v != fa) {
      pair<int, int> x = f[u][0] - f[v][2] + f[v][0] + MP(1, s[u] ^ s[v]);
      if (chkmax(f[u][1], x)) pre[u][1] = v;
    }
  f[u][2] = f[u][0];
  if (chkmax(f[u][2], f[u][1])) pre[u][2] = 1;
}
vector<pair<int, int> > ans;
pair<int, int> ret;
void print(int u, int x, int fa) {
  if (x == 2) {
    print(u, pre[u][2], fa);
    return;
  }
  if (x == 1) {
    ans.push_back(MP(u, pre[u][1]));
    print(pre[u][1], 0, u);
    for (int v : g[u])
      if (v != pre[u][1] && v != fa) print(v, 2, u);
  } else
    for (int v : g[u])
      if (v != fa) print(v, 2, u);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d%d", &x, s + i);
    s[i]--;
    g[i].insert(x), g[x].insert(i);
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      p = q = 0;
      dfs(i, 0);
      if (p && q) {
        g[p].erase(q), g[q].erase(p);
        if (d[p] > d[q]) swap(p, q);
      }
      dp(i, 0);
      pair<int, int> x = f[i][2];
      if (p && q) {
        int t = q;
        while (fa[t] != p) t = fa[t];
        g[p].erase(t), g[t].erase(p);
        g[p].insert(q), g[q].insert(p);
        dp(i, 0);
        pair<int, int> y = f[i][2];
        if (x > y) {
          g[p].erase(q), g[q].erase(p);
          g[p].insert(t), g[t].insert(p);
          dp(i, 0);
        } else
          x = y;
      }
      ret += x;
      print(i, 2, 0);
    }
  printf("%d %d\n", LX(ret), RX(ret));
  for (auto x : ans) printf("%d %d\n", LX(x), RX(x));
  return 0;
}