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 = 100000;
const int CHILD = 1;
const int PREV = 2;
int timestamp[MAXN + 10];
bool onCircle[MAXN + 10];
int f[MAXN + 10], s[MAXN + 10];
int n;
vector<int> ch[MAXN + 10];
void find(int x, int time) {
  vector<int> stk;
  while (!timestamp[x]) {
    timestamp[x] = time;
    stk.push_back(x);
    x = f[x];
  }
  if (timestamp[x] == time) {
    int j;
    do {
      j = stk.back();
      stk.pop_back();
      onCircle[j] = true;
      1;
    } while (j != x);
  }
}
vector<int> seq;
void lu(int st) {
  vector<int> q;
  q.push_back(st);
  while (!q.empty()) {
    int now = q.back();
    q.pop_back();
    seq.push_back(now);
    for (__typeof(ch[now].begin()) i = ch[now].begin(); i != ch[now].end(); ++i)
      q.push_back(*i);
  }
}
pair<int, int> operator+(pair<int, int> x, pair<int, int> y) {
  return make_pair(x.first + y.first, x.second + y.second);
}
pair<int, int> operator-(pair<int, int> x, pair<int, int> y) {
  return make_pair(x.first - y.first, x.second - y.second);
}
pair<int, int>& operator+=(pair<int, int>& a, pair<int, int> b) {
  a.first += b.first;
  a.second += b.second;
  return a;
}
pair<int, int> dpTree[MAXN + 10][2];
int treeChoose[MAXN + 10];
void DpTree() {
  for (__typeof(seq.begin()) x = seq.begin(); x != seq.end(); ++x) {
    dpTree[*x][true] = make_pair(0, 0);
    for (__typeof(ch[*x].begin()) i = ch[*x].begin(); i != ch[*x].end(); ++i)
      dpTree[*x][true] += dpTree[*i][false];
    dpTree[*x][false] = dpTree[*x][true];
    treeChoose[*x] = -1;
    for (__typeof(ch[*x].begin()) i = ch[*x].begin(); i != ch[*x].end(); ++i) {
      pair<int, int> cur = dpTree[*x][true] - dpTree[*i][false] +
                           dpTree[*i][true] + make_pair(1, s[*x] != s[*i]);
      if (cur > dpTree[*x][false]) {
        dpTree[*x][false] = cur;
        treeChoose[*x] = *i;
      }
    }
  }
}
void FangTree(int x, bool used, vector<pair<int, int> >& fang) {
  vector<pair<int, bool> > stk;
  stk.push_back(make_pair(x, used));
  while (!stk.empty()) {
    pair<int, bool> now = stk.back();
    stk.pop_back();
    if (now.second) {
      for (__typeof(ch[now.first].begin()) i = ch[now.first].begin();
           i != ch[now.first].end(); ++i)
        stk.push_back(make_pair(*i, false));
    } else {
      for (__typeof(ch[now.first].begin()) i = ch[now.first].begin();
           i != ch[now.first].end(); ++i)
        if (treeChoose[now.first] == *i)
          stk.push_back(make_pair(*i, true)),
              fang.push_back(make_pair(*i, now.first));
        else
          stk.push_back(make_pair(*i, false));
    }
  }
}
void FangCir(int* cirChoose, vector<int>& cir, vector<pair<int, int> >& fang) {
  bool flag = true;
  for (int i = (cir.size()) - 1; i >= 0; i--) {
    if (flag) {
      if (cirChoose[i] == CHILD)
        FangTree(cir[i], false, fang), flag = true;
      else {
        FangTree(cir[i], true, fang), flag = false;
        fang.push_back(make_pair(cir[i], cir[i - 1]));
      }
    } else {
      FangTree(cir[i], true, fang);
      flag = true;
    }
  }
}
pair<int, int> DpCircle(vector<int>& cir, vector<pair<int, int> >& fang) {
  static pair<int, int> dp[MAXN + 10][2];
  static int cirChoose[MAXN + 10];
  dp[0][false] = dpTree[cir[0]][true];
  dp[0][true] = dpTree[cir[0]][false];
  cirChoose[0] = CHILD;
  for (int j = (1); j <= (cir.size() - 1); j++) {
    dp[j][false] = dpTree[cir[j]][true] + dp[j - 1][true];
    pair<int, int> ifCh = dpTree[cir[j]][false] + dp[j - 1][true];
    pair<int, int> ifPr = dpTree[cir[j]][true] +
                          make_pair(1, s[cir[j]] != s[cir[j - 1]]) +
                          dp[j - 1][false];
    if (ifCh > ifPr) {
      cirChoose[j] = CHILD;
      dp[j][true] = ifCh;
    } else {
      cirChoose[j] = PREV;
      dp[j][true] = ifPr;
    }
  }
  cir.push_back(cir.front());
  cir.erase(cir.begin());
  static pair<int, int> dp2[MAXN + 10][2];
  static int cirChoose2[MAXN + 10];
  dp2[0][false] = dpTree[cir[0]][true];
  dp2[0][true] = dpTree[cir[0]][false];
  cirChoose2[0] = CHILD;
  for (int j = (1); j <= (cir.size() - 1); j++) {
    dp2[j][false] = dpTree[cir[j]][true] + dp2[j - 1][true];
    pair<int, int> ifCh = dpTree[cir[j]][false] + dp2[j - 1][true];
    pair<int, int> ifPr = dpTree[cir[j]][true] +
                          make_pair(1, s[cir[j]] != s[cir[j - 1]]) +
                          dp2[j - 1][false];
    if (ifCh > ifPr) {
      cirChoose2[j] = CHILD;
      dp2[j][true] = ifCh;
    } else {
      cirChoose2[j] = PREV;
      dp2[j][true] = ifPr;
    }
  }
  if (dp[cir.size() - 1][true] > dp2[cir.size() - 1][true]) {
    cir.insert(cir.begin(), cir.back());
    cir.pop_back();
    FangCir(cirChoose, cir, fang);
    return dp[cir.size() - 1][true];
  } else {
    FangCir(cirChoose2, cir, fang);
    return dp2[cir.size() - 1][true];
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (n); i++) scanf("%d%d", &f[i], &s[i]), f[i]--;
  for (int i = 0; i < (n); i++) find(i, i + 1);
  for (int i = 0; i < (n); i++)
    if (!onCircle[i]) ch[f[i]].push_back(i);
  for (int i = 0; i < (n); i++)
    if (onCircle[i]) lu(i);
  reverse(seq.begin(), seq.end());
  DpTree();
  memset(timestamp, false, sizeof(timestamp));
  pair<int, int> ans = make_pair(0, 0);
  vector<pair<int, int> > fang;
  for (int i = 0; i < (n); i++)
    if (onCircle[i] && !timestamp[i]) {
      vector<int> cir;
      for (int x = i; !timestamp[x]; x = f[x])
        cir.push_back(x), timestamp[x] = true;
      ans += DpCircle(cir, fang);
    }
  cout << ans.first << ' ' << ans.second << endl;
  for (__typeof(fang.begin()) i = fang.begin(); i != fang.end(); ++i)
    printf("%d %d\n", i->first + 1, i->second + 1);
  return 0;
}