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;
pair<int, int> operator+(const pair<int, int> &a, const pair<int, int> &b) {
  return pair<int, int>(a.first + b.first, a.second + b.second);
}
pair<int, int> operator-(const pair<int, int> &a, const pair<int, int> &b) {
  return pair<int, int>(a.first - b.first, a.second - b.second);
}
const int MAXN = 101 * 1000;
int n, bf[MAXN], gender[MAXN], best_child[MAXN];
vector<int> child[MAXN];
bool mark[MAXN], done[MAXN];
pair<pair<int, int>, pair<int, int> > do_substree(int pos) {
  done[pos] = true;
  if (((int)(child[pos]).size()) == 0)
    return make_pair(pair<int, int>(0, 0), pair<int, int>(0, 0));
  vector<pair<pair<int, int>, pair<int, int> > > V;
  for (__typeof((child[pos]).begin()) it = (child[pos]).begin();
       it != (child[pos]).end(); ++it)
    V.push_back(do_substree(*it));
  pair<pair<int, int>, pair<int, int> > res(pair<int, int>(0, 0),
                                            pair<int, int>(0, 0));
  pair<pair<int, int>, int> tmp(pair<int, int>(-1, -1), -1);
  for (register int i = (0); i < (int)(((int)(V).size())); ++i) {
    res.second = res.second + V[i].first;
    tmp = max(tmp, make_pair(V[i].second - V[i].first +
                                 pair<int, int>(
                                     1, gender[pos] != gender[child[pos][i]]),
                             i));
  }
  res.first = res.second;
  if (tmp.first > pair<int, int>(0, 0)) {
    res.first = res.second + tmp.first;
    best_child[pos] = tmp.second;
  }
  return res;
}
void print_subtree(vector<pair<int, int> > &out, int pos, int used) {
  for (register int i = (0); i < (int)(((int)(child[pos]).size())); ++i)
    if (used == 1 || best_child[pos] != i) print_subtree(out, child[pos][i], 0);
  if (used == 0 && best_child[pos] != -1) {
    out.push_back(pair<int, int>(pos, child[pos][best_child[pos]]));
    print_subtree(out, child[pos][best_child[pos]], 1);
  }
}
void solve_line(vector<pair<int, int> > &out, const vector<int> &V,
                const vector<pair<pair<int, int>, pair<int, int> > > &val) {
  vector<pair<int, int> > dp(1, pair<int, int>(0, 0));
  vector<int> par(1, -1);
  for (register int i = (0); i < (int)(((int)(V).size())); ++i) {
    if (i > 0 && dp[((int)(dp).size()) - 1] + val[i].first <
                     dp[((int)(dp).size()) - 2] + val[i].second +
                         val[i - 1].second +
                         pair<int, int>(1, gender[V[i]] != gender[V[i - 1]])) {
      dp.push_back(dp[((int)(dp).size()) - 2] + val[i].second +
                   val[i - 1].second +
                   pair<int, int>(1, gender[V[i]] != gender[V[i - 1]]));
      par.push_back(2);
    } else {
      dp.push_back(dp[((int)(dp).size()) - 1] + val[i].first);
      par.push_back(1);
    }
  }
  int pos = ((int)(dp).size()) - 1;
  while (par[pos] != -1) {
    if (par[pos] == 2) {
      out.push_back(pair<int, int>(V[pos - 1], V[pos - 2]));
      print_subtree(out, V[pos - 1], 1);
      print_subtree(out, V[pos - 2], 1);
    } else {
      print_subtree(out, V[pos - 1], 0);
    }
    pos -= par[pos];
  }
}
pair<int, int> score(const vector<pair<int, int> > &edge) {
  pair<int, int> tmp(((int)(edge).size()), 0);
  for (__typeof((edge).begin()) it = (edge).begin(); it != (edge).end(); ++it)
    if (gender[it->first] != gender[it->second]) tmp.second++;
  return tmp;
}
int main() {
  ios::sync_with_stdio(false);
  memset(best_child, -1, sizeof best_child);
  cin >> n;
  for (register int i = (0); i < (int)(n); ++i) {
    cin >> bf[i] >> gender[i];
    bf[i]--;
    child[bf[i]].push_back(i);
  }
  vector<pair<int, int> > total;
  for (register int cur = (0); cur < (int)(n); ++cur)
    if (!done[cur]) {
      vector<int> c(1, cur);
      while (!mark[c.back()]) {
        mark[c.back()] = true;
        c.push_back(bf[c.back()]);
      }
      reverse((c).begin(), (c).end());
      while (c.back() != c[0]) c.pop_back();
      c.pop_back();
      vector<pair<pair<int, int>, pair<int, int> > > nums;
      for (register int i = (0); i < (int)(((int)(c).size())); ++i) {
        child[c[i]].erase(find((child[c[i]]).begin(), (child[c[i]]).end(),
                               c[(i + 1) % ((int)(c).size())]));
        nums.push_back(do_substree(c[i]));
      }
      vector<pair<int, int> > V1, V2;
      solve_line(V1, c, nums);
      if (((int)(nums).size()) >= 2) {
        print_subtree(V2, c[0], 1);
        print_subtree(V2, c.back(), 1);
        V2.push_back(pair<int, int>(c[0], c.back()));
        c.pop_back();
        c.erase(c.begin());
        nums.pop_back();
        nums.erase(nums.begin());
        solve_line(V2, c, nums);
        if (score(V2) > score(V1)) V1 = V2;
      }
      for (__typeof((V1).begin()) it = (V1).begin(); it != (V1).end(); ++it)
        total.push_back(*it);
    }
  pair<int, int> sc = score(total);
  cout << sc.first << " " << sc.second << endl;
  for (__typeof((total).begin()) it = (total).begin(); it != (total).end();
       ++it)
    cout << it->first + 1 << " " << it->second + 1 << endl;
  return 0;
}