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>
const int INF = int(1e9);
const int N = 100000 + 10;
int n;
int f[N], s[N], c[N], state[N];
std::vector<int> adj[N];
inline std::pair<int, int> operator+(const std::pair<int, int>& a,
                                     const std::pair<int, int>& b) {
  return std::pair<int, int>(a.first + b.first, a.second + b.second);
}
std::pair<int, int> dp[N][2], max[N][2];
std::vector<std::pair<int, int> > record[N][2];
void solve(int x) {
  dp[x][0] = dp[x][1] = std::pair<int, int>(0, 0);
  record[x][0].push_back(dp[x][0]);
  record[x][1].push_back(dp[x][1]);
  for (int itr = 0; itr < ((int)(adj[x]).size()); itr++) {
    int y = adj[x][itr];
    solve(y);
    dp[x][1] =
        std::max(dp[x][0] + dp[y][0] + std::pair<int, int>(1, s[x] ^ s[y]),
                 dp[x][1] + std::max(dp[y][0], dp[y][1]));
    dp[x][0] = dp[x][0] + std::max(dp[y][0], dp[y][1]);
    record[x][0].push_back(dp[x][0]);
    record[x][1].push_back(dp[x][1]);
  }
}
void merge(int x, int t, std::vector<std::pair<int, int> >& project) {
  int k = ((int)(adj[x]).size()) - 1;
  for (int itr = ((int)(adj[x]).size()) - 1; itr >= 0; itr--) {
    int y = adj[x][itr];
    if (t == 0) {
      merge(y, dp[y][0] < dp[y][1], project);
    } else {
      if (record[x][0][k] + dp[y][0] + std::pair<int, int>(1, s[x] ^ s[y]) >
          record[x][1][k] + std::max(dp[y][0], dp[y][1])) {
        project.push_back(std::make_pair(x, y));
        merge(y, 0, project);
        t = 0;
      } else {
        merge(y, dp[y][0] < dp[y][1], project);
      }
    }
    k--;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", f + i, s + i);
    f[i]--, s[i]--;
    c[f[i]]++;
  }
  std::vector<int> queue;
  for (int i = 0; i < n; i++) {
    if (c[i] == 0) {
      queue.push_back(i);
    }
  }
  for (int front = 0; front < ((int)(queue).size()); front++) {
    int x = queue[front];
    state[x] = 1;
    adj[f[x]].push_back(x);
    if (c[f[x]]-- == 1) {
      queue.push_back(f[x]);
    }
  }
  for (int i = 0; i < n; i++) {
    if (state[i] == 0) {
      solve(i);
    }
  }
  std::pair<int, int> result(0, 0);
  std::vector<std::pair<int, int> > project;
  for (int i = 0; i < n; i++) {
    if (state[i] == 0) {
      std::vector<int> list;
      for (int j = i; state[j] == 0; j = f[j]) {
        state[j] = 1;
        list.push_back(j);
      }
      max[0][0] = dp[list[0]][0];
      max[0][1] = dp[list[0]][1];
      for (int j = 1; j < ((int)(list).size()); j++) {
        max[j][0] = std::max(max[j - 1][0], max[j - 1][1]) + dp[list[j]][0];
        max[j][1] = max[j - 1][0] + dp[list[j]][0] +
                    std::pair<int, int>(1, s[list[j]] ^ s[list[j - 1]]);
        max[j][1] = std::max(
            max[j][1], std::max(max[j - 1][0], max[j - 1][1]) + dp[list[j]][1]);
      }
      std::pair<int, int> tmp = std::max(max[((int)(list).size()) - 1][0],
                                         max[((int)(list).size()) - 1][1]);
      max[0][0] = std::pair<int, int>(-INF, -INF);
      max[0][1] = dp[list[0]][0] +
                  std::pair<int, int>(1, s[list.front()] ^ s[list.back()]);
      for (int j = 1; j < ((int)(list).size()); j++) {
        max[j][0] = std::max(max[j - 1][0], max[j - 1][1]) + dp[list[j]][0];
        max[j][1] = max[j - 1][0] + dp[list[j]][0] +
                    std::pair<int, int>(1, s[list[j]] ^ s[list[j - 1]]);
        max[j][1] = std::max(
            max[j][1], std::max(max[j - 1][0], max[j - 1][1]) + dp[list[j]][1]);
      }
      if (max[((int)(list).size()) - 1][0] > tmp) {
        tmp = max[((int)(list).size()) - 1][0];
        for (int j = ((int)(list).size()) - 1, k = 0; j > 0; j--) {
          if (k == 0) {
            merge(list[j], 0, project);
            k = max[j - 1][0] < max[j - 1][1];
          } else {
            if (max[j - 1][0] + dp[list[j]][0] +
                    std::pair<int, int>(1, s[list[j]] ^ s[list[j - 1]]) >
                std::max(max[j - 1][0], max[j - 1][1]) + dp[list[j]][1]) {
              merge(list[j], 0, project);
              project.push_back(std::make_pair(list[j - 1], list[j]));
              k = 0;
            } else {
              merge(list[j], 1, project);
              k = max[j - 1][0] < max[j - 1][1];
            }
          }
        }
        project.push_back(std::make_pair(list.front(), list.back()));
        merge(list.front(), 0, project);
      } else {
        max[0][0] = dp[list[0]][0];
        max[0][1] = dp[list[0]][1];
        for (int j = 1; j < ((int)(list).size()); j++) {
          max[j][0] = std::max(max[j - 1][0], max[j - 1][1]) + dp[list[j]][0];
          max[j][1] = max[j - 1][0] + dp[list[j]][0] +
                      std::pair<int, int>(1, s[list[j]] ^ s[list[j - 1]]);
          max[j][1] =
              std::max(max[j][1],
                       std::max(max[j - 1][0], max[j - 1][1]) + dp[list[j]][1]);
        }
        int j = ((int)(list).size()) - 1;
        int k = max[j][0] < max[j][1];
        for (; j > 0; j--) {
          if (k == 0) {
            merge(list[j], 0, project);
            k = max[j - 1][0] < max[j - 1][1];
          } else {
            if (max[j - 1][0] + dp[list[j]][0] +
                    std::pair<int, int>(1, s[list[j]] ^ s[list[j - 1]]) >
                std::max(max[j - 1][0], max[j - 1][1]) + dp[list[j]][1]) {
              merge(list[j], 0, project);
              project.push_back(std::make_pair(list[j - 1], list[j]));
              k = 0;
            } else {
              merge(list[j], 1, project);
              k = max[j - 1][0] < max[j - 1][1];
            }
          }
        }
        merge(list[j], k, project);
      }
      result = result + tmp;
    }
  }
  printf("%d %d\n", result.first, result.second);
  for (int i = 0; i < ((int)(project).size()); i++) {
    printf("%d %d\n", project[i].first + 1, project[i].second + 1);
  }
}