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;
struct rec {
  int x;
  rec *next;
};
int s[200000];
int p[200000];
int r[200000];
int ind[200000];
int used[200000];
rec d[200000];
int cho[200000];
pair<int, int> dp[200000][2];
int main() {
  int i, j, k, x, y, min, n, top, tmp, bq, b1;
  pair<int, int> best, ans;
  vector<pair<int, int> > sol;
  vector<pair<int, int> > tmps;
  rec *q;
  vector<int> a;
  scanf("%d", &n);
  for (i = 0; i < n; i++) d[i].next = NULL;
  for (i = 0; i < n; i++) {
    scanf("%d%d", &x, &y);
    x--;
    p[i] = x;
    s[i] = y;
    q = new rec();
    q->x = i;
    q->next = d[x].next;
    d[x].next = q;
  }
  memset(used, 0, sizeof(used));
  memset(r, -1, sizeof(r));
  for (i = 0; i < n; i++)
    if (used[i] == 0) {
      a.clear();
      j = i;
      while (used[j] == 0) {
        a.push_back(j);
        used[j] = 1;
        j = p[j];
      }
      if (r[j] == -1) {
        k = p[j];
        min = j;
        while (k != j) {
          if (k < min) min = k;
          k = p[k];
        }
        r[j] = min;
      }
      for (k = 0; k < a.size(); k++) r[a[k]] = r[j];
    }
  memset(used, 0, sizeof(used));
  ans = make_pair(0, 0);
  for (i = 0; i < n; i++)
    if (used[i] == 0) {
      a.clear();
      a.push_back(r[i]);
      ind[r[i]] = 0;
      top = 0;
      used[r[i]] = 1;
      while (top < a.size()) {
        q = d[a[top]].next;
        while (q != NULL) {
          if (used[q->x] == 0) {
            ind[q->x] = a.size();
            a.push_back(q->x);
            used[q->x] = 1;
          }
          q = q->next;
        }
        top++;
      }
      tmp = p[r[i]];
      p[r[i]] = -1;
      for (j = a.size() - 1; j >= 0; j--) {
        pair<int, int> delta;
        dp[j][0].first = 0;
        dp[j][0].second = 0;
        dp[j][1].first = 0;
        dp[j][1].second = 0;
        cho[j] = -1;
        delta = make_pair(-1, -1);
        q = d[a[j]].next;
        while (q != NULL) {
          if ((q->x != p[a[j]]) && (q->x != r[i])) {
            pair<int, int> pp = dp[ind[q->x]][0];
            if (dp[ind[q->x]][1] > pp) pp = dp[ind[q->x]][1];
            dp[j][0].first += pp.first;
            dp[j][0].second += pp.second;
            pp.first = dp[ind[q->x]][0].first - pp.first;
            pp.second = dp[ind[q->x]][0].second - pp.second;
            pp.first++;
            if (s[q->x] != s[a[j]]) pp.second++;
            if (pp > delta) {
              delta = pp;
              bq = ind[q->x];
            }
          }
          q = q->next;
        }
        if (delta.first != -1) {
          dp[j][1].first = dp[j][0].first + delta.first;
          dp[j][1].second = dp[j][0].second + delta.second;
          cho[j] = bq;
        }
      }
      best = dp[0][0];
      if (dp[0][1] > best) best = dp[0][1];
      for (j = a.size() - 1; j >= 0; j--) {
        pair<int, int> delta;
        dp[j][0].first = 0;
        dp[j][0].second = 0;
        dp[j][1].first = 0;
        dp[j][1].second = 0;
        cho[j] = -1;
        delta = make_pair(-1, -1);
        q = d[a[j]].next;
        while (q != NULL) {
          if ((q->x != p[a[j]]) && (q->x != r[i])) {
            pair<int, int> pp = dp[ind[q->x]][0];
            if (dp[ind[q->x]][1] > pp) pp = dp[ind[q->x]][1];
            dp[j][0].first += pp.first;
            dp[j][0].second += pp.second;
            pp.first = dp[ind[q->x]][0].first - pp.first;
            pp.second = dp[ind[q->x]][0].second - pp.second;
            pp.first++;
            if (s[q->x] != s[a[j]]) pp.second++;
            if (q->x != tmp) {
              if (pp > delta) {
                delta = pp;
                bq = ind[q->x];
              }
            }
          }
          q = q->next;
        }
        if ((a[j] != a[0]) && (a[j] != tmp)) {
          if (delta.first != -1) {
            dp[j][1].first = dp[j][0].first + delta.first;
            dp[j][1].second = dp[j][0].second + delta.second;
            cho[j] = bq;
          }
        }
      }
      dp[0][0].first++;
      if (s[a[0]] != s[tmp]) {
        dp[0][0].second++;
      }
      b1 = 1;
      if (dp[0][0] > best) {
        best = dp[0][0];
        b1 = 2;
      }
      ans.first += best.first;
      ans.second += best.second;
      tmps.clear();
      if (b1 == 1) {
        for (j = a.size() - 1; j >= 0; j--) {
          pair<int, int> delta;
          dp[j][0].first = 0;
          dp[j][0].second = 0;
          dp[j][1].first = 0;
          dp[j][1].second = 0;
          cho[j] = -1;
          delta = make_pair(-1, -1);
          q = d[a[j]].next;
          while (q != NULL) {
            if ((q->x != p[a[j]]) && (q->x != r[i])) {
              pair<int, int> pp = dp[ind[q->x]][0];
              if (dp[ind[q->x]][1] > pp) pp = dp[ind[q->x]][1];
              dp[j][0].first += pp.first;
              dp[j][0].second += pp.second;
              pp.first = dp[ind[q->x]][0].first - pp.first;
              pp.second = dp[ind[q->x]][0].second - pp.second;
              pp.first++;
              if (s[q->x] != s[a[j]]) pp.second++;
              if (pp > delta) {
                delta = pp;
                bq = ind[q->x];
              }
            }
            q = q->next;
          }
          if (delta.first != -1) {
            dp[j][1].first = dp[j][0].first + delta.first;
            dp[j][1].second = dp[j][0].second + delta.second;
            cho[j] = bq;
          }
        }
        if (dp[0][1] > dp[0][0])
          tmps.push_back(make_pair(0, 1));
        else
          tmps.push_back(make_pair(0, 0));
      } else {
        tmps.push_back(make_pair(0, 0));
        sol.push_back(make_pair(r[i], tmp));
      }
      top = 0;
      while (top < tmps.size()) {
        if (tmps[top].second == 0) {
          q = d[a[tmps[top].first]].next;
          while (q != NULL) {
            if ((q->x != p[a[tmps[top].first]]) && (ind[q->x] != 0)) {
              if (dp[ind[q->x]][0] > dp[ind[q->x]][1])
                tmps.push_back(make_pair(ind[q->x], 0));
              else
                tmps.push_back(make_pair(ind[q->x], 1));
            }
            q = q->next;
          }
        } else {
          bq = cho[tmps[top].first];
          if (bq != -1) {
            sol.push_back(make_pair(a[tmps[top].first], a[bq]));
            q = d[a[tmps[top].first]].next;
            while (q != NULL) {
              if ((q->x != p[a[tmps[top].first]]) && (ind[q->x] != 0)) {
                if (ind[q->x] != bq) {
                  if (dp[ind[q->x]][0] > dp[ind[q->x]][1])
                    tmps.push_back(make_pair(ind[q->x], 0));
                  else
                    tmps.push_back(make_pair(ind[q->x], 1));
                } else {
                  tmps.push_back(make_pair(bq, 0));
                }
              }
              q = q->next;
            }
          }
        }
        top++;
      }
    }
  printf("%d %d\n", ans.first, ans.second);
  for (i = 0; i < sol.size(); i++)
    printf("%d %d\n", sol[i].first + 1, sol[i].second + 1);
}