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;
clock_t __stt;
inline void TStart() { __stt = clock(); }
inline void TReport() {
  printf("\nTaken Time : %.3lf sec\n",
         (double)(clock() - __stt) / CLOCKS_PER_SEC);
}
template <typename T>
T MIN(T a, T b) {
  return a < b ? a : b;
}
template <typename T>
T MAX(T a, T b) {
  return a > b ? a : b;
}
template <typename T>
T ABS(T a) {
  return a > 0 ? a : (-a);
}
template <typename T>
void UMIN(T &a, T b) {
  if (b < a) a = b;
}
template <typename T>
void UMAX(T &a, T b) {
  if (b > a) a = b;
}
int n, f[100005], s[100005], vis[100005], vis2[100005], pf[2][100005],
    ext[100005], cu, cv, cc;
pair<int, int> dp[100005][2], dp2[100005][2];
vector<int> cur;
vector<pair<int, int> > adj[100005], son[100005], as1, as2;
vector<vector<pair<int, int> > > fstp;
pair<int, int> operator+(pair<int, int> a, pair<int, int> b) {
  return make_pair(a.first + b.first, a.second + b.second);
}
pair<int, int> operator-(pair<int, int> a, pair<int, int> b) {
  return make_pair(a.first - b.first, a.second - b.second);
}
void dfs(int ver, int prev) {
  vis[ver] = 1;
  cur.push_back(ver);
  int xb;
  pair<int, int> addon = make_pair(-2000000000, -2000000000);
  bool vs = 0;
  for (xb = 0; xb < adj[ver].size(); ++xb) {
    if (adj[ver][xb].first != prev) {
      if (!vis[adj[ver][xb].first]) {
        vs = 1;
        son[ver].push_back(adj[ver][xb]);
        dfs(adj[ver][xb].first, ver);
        dp[ver][0] = dp[ver][0] +
                     MAX(dp[adj[ver][xb].first][1], dp[adj[ver][xb].first][0]);
        if (make_pair(1, adj[ver][xb].second) + dp[adj[ver][xb].first][0] -
                MAX(dp[adj[ver][xb].first][1], dp[adj[ver][xb].first][0]) >
            addon) {
          addon = make_pair(1, adj[ver][xb].second) +
                  dp[adj[ver][xb].first][0] -
                  MAX(dp[adj[ver][xb].first][1], dp[adj[ver][xb].first][0]);
          pf[0][ver] = adj[ver][xb].first;
        }
      } else if (cu == -1) {
        cu = ver;
        cv = adj[ver][xb].first;
        cc = adj[ver][xb].second;
      }
    }
  }
  if (vs == 0) {
    dp[ver][0] = dp[ver][1] = make_pair(0, 0);
  } else {
    dp[ver][1] = dp[ver][0] + addon;
  }
}
void pick(int ver, int type) {
  int xb;
  if (type == -1) type = (dp[ver][0] > dp[ver][1] ? 0 : 1);
  for (xb = 0; xb < son[ver].size(); ++xb) {
    if (type == 0) {
      pick(son[ver][xb].first, -1);
    } else if (son[ver][xb].first != pf[0][ver])
      pick(son[ver][xb].first, -1);
    else {
      as1.push_back(make_pair(ver + 1, son[ver][xb].first + 1));
      pick(son[ver][xb].first, 0);
    }
  }
  son[ver].clear();
}
void dfs2(int ver, int prev) {
  vis2[ver] = 1;
  int xb;
  pair<int, int> addon = make_pair(-2000000000, -2000000000);
  bool vs = 0;
  for (xb = 0; xb < adj[ver].size(); ++xb) {
    if (adj[ver][xb].first != prev && ext[adj[ver][xb].first]) {
      vs = 1;
      son[ver].push_back(adj[ver][xb]);
      dfs2(adj[ver][xb].first, ver);
      dp2[ver][0] = dp2[ver][0] +
                    MAX(dp2[adj[ver][xb].first][1], dp2[adj[ver][xb].first][0]);
      if (make_pair(1, adj[ver][xb].second) + dp2[adj[ver][xb].first][0] -
              MAX(dp2[adj[ver][xb].first][1], dp2[adj[ver][xb].first][0]) >
          addon) {
        addon = make_pair(1, adj[ver][xb].second) + dp2[adj[ver][xb].first][0] -
                MAX(dp2[adj[ver][xb].first][1], dp2[adj[ver][xb].first][0]);
        pf[1][ver] = adj[ver][xb].first;
      }
    }
  }
  if (!vs) {
    dp2[ver][0] = dp2[ver][1] = make_pair(0, 0);
  } else {
    dp2[ver][1] = dp2[ver][0] + addon;
  }
}
void pick2(int ver, int type) {
  int xb;
  if (type == -1) type = (dp2[ver][0] > dp2[ver][1] ? 0 : 1);
  for (xb = 0; xb < son[ver].size(); ++xb) {
    if (type == 0)
      pick2(son[ver][xb].first, -1);
    else if (son[ver][xb].first != pf[1][ver])
      pick2(son[ver][xb].first, -1);
    else {
      as2.push_back(make_pair(ver + 1, son[ver][xb].first + 1));
      pick2(son[ver][xb].first, 0);
    }
  }
  son[ver].clear();
}
int main() {
  scanf("%d", &n);
  int xb, x2;
  for (xb = 0; xb < n; ++xb) {
    scanf("%d%d", f + xb, s + xb);
    --f[xb];
  }
  pair<int, int> fres = make_pair(0, 0);
  for (xb = 0; xb < n; ++xb) {
    if (s[f[xb]] != s[xb]) {
      adj[xb].push_back(make_pair(f[xb], 1));
      adj[f[xb]].push_back(make_pair(xb, 1));
    } else {
      adj[xb].push_back(make_pair(f[xb], 0));
      adj[f[xb]].push_back(make_pair(xb, 0));
    }
  }
  cu = -1;
  fill(ext, ext + n, 1);
  for (xb = 0; xb < n; ++xb) {
    if (!vis[xb]) {
      cu = cv = -1;
      cc = 0;
      as1.clear();
      as2.clear();
      cur.clear();
      dfs(xb, -1);
      pick(xb, -1);
      pair<int, int> curs = MAX(dp[xb][0], dp[xb][1]);
      ext[cu] = ext[cv] = 0;
      pair<int, int> ncr = make_pair(0, 0);
      for (x2 = 0; x2 < cur.size(); ++x2) {
        if (!vis2[cur[x2]] && ext[cur[x2]]) {
          dfs2(cur[x2], -1);
          pick2(cur[x2], -1);
          ncr = ncr + MAX(dp2[cur[x2]][0], dp2[cur[x2]][1]);
        }
      }
      ncr = ncr + make_pair(1, cc);
      as2.push_back(make_pair(cu + 1, cv + 1));
      if (ncr > curs) {
        fres = fres + ncr;
        fstp.push_back(as2);
      } else {
        fres = fres + curs;
        fstp.push_back(as1);
      }
    }
  }
  printf("%d %d\n", fres.first, fres.second);
  for (xb = 0; xb < fstp.size(); ++xb) {
    for (x2 = 0; x2 < fstp[xb].size(); ++x2) {
      printf("%d %d\n", fstp[xb][x2].first, fstp[xb][x2].second);
    }
  }
  return 0;
}