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 MAX = 1e5 + 20;
vector<int> adj[MAX], adj2[MAX];
struct node {
  int parent;
  int ranks;
};
node trees[MAX];
int n, i, j, k;
void ini() {
  for (i = 1; i <= n; i++) trees[i].parent = i, trees[i].ranks = 0;
}
int findSet(int x) {
  if (trees[x].parent != x)
    return trees[x].parent = findSet(trees[x].parent);
  else
    return x;
}
void unions(int x, int y) {
  int rootX = findSet(x), rootY = findSet(y);
  if (rootX == rootY) return;
  if (trees[rootX].ranks > trees[rootY].ranks) swap(rootX, rootY);
  trees[rootX].parent = rootY;
  if (trees[rootX].ranks == trees[rootY].ranks) trees[rootY].ranks++;
}
pair<int, int> noEd[MAX];
int nonum;
struct nn {
  pair<int, int> val;
  int sel;
};
nn an1[MAX];
nn an2[MAX];
struct nn2 {
  vector<int> invl;
  pair<int, int> val;
};
nn2 ty2[MAX];
int rr[MAX], rr2[MAX], cn, cn2;
vector<int> child1[MAX], child2[MAX];
int f[MAX], s[MAX];
bool vist[MAX];
int dep[MAX];
int cc;
void dfs1(int v) {
  vist[v] = 1;
  for (int i = 0; i < adj[v].size(); i++) {
    int u = adj[v][i];
    if (!vist[u]) {
      child1[v].push_back(u);
      unions(u, v);
      dep[u] = dep[v] + 1;
      dfs1(u);
    } else if (dep[v] < dep[u])
      noEd[cc] = {v, u};
  }
}
void dfs2(int v) {
  vist[v] = 1;
  for (int i = 0; i < adj2[v].size(); i++) {
    int u = adj2[v][i];
    if (!vist[u]) {
      child2[v].push_back(u);
      dfs2(u);
    }
  }
}
inline pair<int, int> operator+(const pair<int, int> &a,
                                const pair<int, int> &b) {
  return {a.first + b.first, a.second + b.second};
}
inline pair<int, int> operator-(const pair<int, int> &a,
                                const pair<int, int> &b) {
  return {a.first - b.first, a.second - b.second};
}
inline bool operator>(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first != b.first)
    return a.first > b.first;
  else
    return a.second > b.second;
}
pair<int, int> sol1(int v, int ty) {
  vector<int> *child;
  nn *an;
  if (ty == 1)
    child = child1, an = an1;
  else
    child = child2, an = an2;
  if (an[v].val != (pair<int, int>){-1, -1}) return an[v].val;
  pair<int, int> t1 = {0, 0};
  for (int i = 0; i < child[v].size(); i++) {
    int c1 = child[v][i];
    t1 = t1 + sol1(c1, ty);
  }
  pair<int, int> t2 = {0, 0};
  int optc = -1;
  for (int i = 0; i < child[v].size(); i++) {
    int c1 = child[v][i];
    pair<int, int> tmp = t1;
    tmp = tmp - sol1(c1, ty);
    pair<int, int> sing;
    sing.first = 1;
    sing.second = s[v] != s[c1] ? 1 : 0;
    tmp = tmp + sing;
    for (int j = 0; j < child[c1].size(); j++) {
      int s1 = child[c1][j];
      tmp = tmp + sol1(s1, ty);
    }
    if (tmp > t2) {
      optc = i;
      t2 = tmp;
    }
  }
  if (t1 > t2 || optc == -1) {
    an[v].sel = -1;
    return an[v].val = t1;
  } else {
    an[v].sel = optc;
    return an[v].val = t2;
  }
}
void print(int v, int ty) {
  nn *an = ty == 1 ? an1 : an2;
  vector<int> *child = ty == 1 ? child1 : child2;
  if (an[v].sel == -1) {
    for (int i = 0; i < child[v].size(); i++) {
      print(child[v][i], ty);
    }
  } else {
    for (int i = 0; i < child[v].size(); i++) {
      if (i != an[v].sel) print(child[v][i], ty);
    }
    int c1 = child[v][an[v].sel];
    printf("%d %d\n", v, c1);
    for (int i = 0; i < child[c1].size(); i++) {
      print(child[c1][i], ty);
    }
  }
}
void printAns(int idx, int ty) {
  if (ty == 1) {
    print(rr[idx], ty);
  } else {
    printf("%d %d\n", noEd[idx].first, noEd[idx].second);
    for (int i = 0; i < ty2[idx].invl.size(); i++) {
      int root = ty2[idx].invl[i];
      print(root, ty);
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &f[i], &s[i]), adj[i].push_back(f[i]), adj[f[i]].push_back(i);
  memset(vist, 0, sizeof(vist));
  cc = 0;
  ini();
  for (int i = 1; i <= n; i++) {
    if (!vist[i]) {
      rr[cc] = i;
      dep[i] = 0;
      dfs1(i);
      cc++;
    }
  }
  cn = cc;
  for (int i = 1; i <= n; i++) an1[i].val = {-1, -1}, an2[i].val = {-1, -1};
  for (int i = 0; i < cn; i++) {
    sol1(rr[i], 1);
  }
  unordered_set<int> noSel;
  for (int i = 0; i < cn; i++) {
    noSel.insert(noEd[i].first), noSel.insert(noEd[i].second);
  }
  for (int i = 1; i <= n; i++) {
    if (noSel.find(i) != noSel.end()) continue;
    for (int j = 0; j < adj[i].size(); j++) {
      int u = adj[i][j];
      if (noSel.find(u) != noSel.end()) continue;
      if (i < u) adj2[i].push_back(u), adj2[u].push_back(i);
    }
  }
  memset(vist, 0, sizeof(vist)), cc = 0;
  for (int i = 1; i <= n; i++) {
    if (!vist[i]) {
      rr2[cc] = i;
      dfs2(i);
      cc++;
    }
  }
  cn2 = cc;
  for (int i = 0; i < cn2; i++) {
    sol1(rr2[i], 2);
  }
  unordered_map<int, int> soi;
  for (int i = 0; i < cn; i++) {
    int v = noEd[i].first;
    int p = findSet(v);
    soi[p] = i;
    ty2[i].val.first = 1;
    ty2[i].val.second = s[noEd[i].first] != s[noEd[i].second] ? 1 : 0;
  }
  for (int i = 0; i < cn2; i++) {
    pair<int, int> val = sol1(rr2[i], 2);
    assert(soi.find(findSet(rr2[i])) != soi.end());
    int idx = soi[findSet(rr2[i])];
    assert(idx < cn);
    ty2[idx].val = ty2[idx].val + val;
    ty2[idx].invl.push_back(rr2[i]);
  }
  pair<int, int> ans = {0, 0};
  for (int i = 0; i < cn; i++) {
    pair<int, int> t1 = an1[rr[i]].val;
    pair<int, int> t2 = ty2[i].val;
    if (t1 > t2)
      ans = ans + t1;
    else
      ans = ans + t2;
  }
  printf("%d %d\n", ans.first, ans.second);
  for (int i = 0; i < cn; i++) {
    pair<int, int> t1 = an1[rr[i]].val;
    pair<int, int> t2 = ty2[i].val;
    if (t1 > t2)
      printAns(i, 1);
    else
      printAns(i, 2);
  }
  return 0;
}