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 + 10, MAXM = 2e5 + 10;
int n, u, v, color[MAXN], vis[MAXN], fa[MAXN], depth[MAXN];
int pre[MAXN];
pair<int, int> f[MAXN], g[MAXN], ans;
set<int> e[MAXN];
vector<pair<int, int> > sol;
pair<int, int> operator+(const pair<int, int>& p1, const pair<int, int>& p2) {
  return make_pair(p1.first + p2.first, p1.second + p2.second);
}
pair<int, int> operator-(const pair<int, int>& p1, const pair<int, int>& p2) {
  return make_pair(p1.first - p2.first, p1.second - p2.second);
}
struct Dsu {
  int fa[MAXN];
  pair<int, int> info[MAXN];
  void Init() {
    for (long long i = (1); i <= (n); i++) fa[i] = i;
  }
  int Find(int x) {
    if (fa[x] == x) return x;
    return fa[x] = Find(fa[x]);
  }
  void Merge(int x, int y) {
    if (!info[Find(y)].first) info[Find(y)] = info[Find(x)];
    fa[Find(x)] = Find(y);
  }
} dsu;
void predfs(int u) {
  vis[u] = 1;
  depth[u] = depth[fa[u]] + 1;
  for (set<int>::iterator it = e[u].begin(); it != e[u].end(); it++) {
    if (vis[*it]) continue;
    fa[*it] = u;
    predfs(*it);
  }
}
void clr(int u, int fa) {
  f[u] = g[u] = make_pair(0, 0);
  pre[u] = 0;
  for (set<int>::iterator it = e[u].begin(); it != e[u].end(); it++) {
    if (fa == *it) continue;
    clr(*it, u);
  }
}
void dfs(int u, int fa) {
  for (set<int>::iterator it = e[u].begin(); it != e[u].end(); it++) {
    if (fa == *it) continue;
    dfs(*it, u);
    g[u] = g[u] + f[*it];
  }
  f[u] = g[u];
  for (set<int>::iterator it = e[u].begin(); it != e[u].end(); it++) {
    if (fa == *it) continue;
    if (g[u] - f[*it] + g[*it] + make_pair(1, color[u] != color[*it]) > f[u]) {
      pre[u] = *it;
      f[u] = g[u] - f[*it] + g[*it] + make_pair(1, color[u] != color[*it]);
    }
  }
}
pair<int, int> dp(int u) {
  clr(u, u);
  dfs(u, u);
  return f[u];
}
void solve(int u, int fa) {
  if (pre[u]) {
    sol.push_back(make_pair(u, pre[u]));
    for (set<int>::iterator it = e[pre[u]].begin(); it != e[pre[u]].end();
         it++) {
      if (*it == pre[u] || *it == u) continue;
      solve(*it, pre[u]);
    }
  }
  for (set<int>::iterator it = e[u].begin(); it != e[u].end(); it++) {
    if (*it == pre[u] || *it == fa) continue;
    solve(*it, u);
  }
}
int main() {
  cin >> n;
  dsu.Init();
  for (long long u = (1); u <= (n); u++) {
    cin >> v >> color[u];
    if (dsu.Find(u) != dsu.Find(v)) {
      dsu.Merge(u, v);
      e[u].insert(v), e[v].insert(u);
    } else {
      dsu.info[dsu.Find(u)] = make_pair(u, v);
    }
  }
  for (long long i = (1); i <= (n); i++) {
    if (vis[i]) continue;
    vis[i] = 1;
    predfs(i);
    pair<int, int> p = dsu.info[dsu.Find(i)], ret = make_pair(0, 0),
                   tmp = make_pair(0, 0);
    int u, v, w;
    if (depth[p.first] > depth[p.second])
      u = p.first, v = fa[p.first], w = p.second;
    else
      u = p.second, v = fa[p.second], w = p.first;
    e[u].erase(v), e[v].erase(u);
    e[u].insert(w), e[w].insert(u);
    ret = dp(i);
    e[u].erase(w), e[w].erase(u);
    e[u].insert(v), e[v].insert(u);
    tmp = dp(i);
    ans = ans + (ret > tmp ? ret : tmp);
    if (tmp < ret) {
      e[u].erase(v), e[v].erase(u);
      e[u].insert(w), e[w].insert(u);
    }
    dp(i);
    solve(i, i);
  }
  printf("%d %d\n", ans.first, ans.second);
  int sz = sol.size();
  for (long long i = (0); i <= (sz - 1); i++) {
    printf("%d %d\n", sol[i].first, sol[i].second);
  }
  return 0;
}