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 long long linf = 1e18 + 5;
const int mod = (int)1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 2e5 + 5;
int hh2[N][2], s, n, m, x, y, z, a[N], root[N], h[N], hh[N][2];
vector<int> v[N];
vector<pair<int, int> > ans;
class data {
 public:
  int ans, cnt;
  data() { ans = 0, cnt = 0; }
  bool operator<(data x) {
    if (ans < x.ans) return 1;
    if (x.ans == ans && cnt < x.cnt) return 1;
    return 0;
  }
  bool operator>(data x) {
    if (x.ans < ans) return 1;
    if (x.ans == ans && x.cnt < cnt) return 1;
    return 0;
  }
  bool operator==(data x) {
    if (x.ans == ans && x.cnt == cnt) return 1;
    return 0;
  }
  data operator+(data x) {
    data t;
    t.ans = ans + x.ans;
    t.cnt = cnt + x.cnt;
    return t;
  }
  data operator-(data x) {
    data t;
    t.ans = ans - x.ans;
    t.cnt = cnt - x.cnt;
    return t;
  }
} dp[N][2], dp2[N][2];
data g(int x, int y) {
  data temp;
  temp.ans = 1;
  temp.cnt = a[x] != a[y];
  return temp;
}
data dfs(int node, int can) {
  data &r = dp2[node][can];
  if (hh2[node][can]) return r;
  hh2[node][can] = 1;
  if (!h[node]) h[node] = 1;
  data tt;
  for (__typeof(v[node].begin()) it = v[node].begin(); it != v[node].end();
       it++) {
    if (h[*it] < 2 && root[*it] == node) {
      data temp = dfs(*it, 0) + g(node, *it) - dfs(*it, 1);
      if (tt < temp) tt = temp;
      r = r + dfs(*it, 1);
    }
  }
  data r2 = r + tt;
  if (r2 > dp2[node][can] && can == 1) r = r2;
  return r;
}
void prin(int node, int can) {
  data r = dp2[node][can];
  if (!h[node]) h[node] = 1;
  data tt;
  for (__typeof(v[node].begin()) it = v[node].begin(); it != v[node].end();
       it++) {
    if (h[*it] < 2 && root[*it] == node) tt = tt + dfs(*it, 1);
  }
  if (tt == r) {
    for (__typeof(v[node].begin()) it = v[node].begin(); it != v[node].end();
         it++)
      if (h[*it] < 2 && root[*it] == node) prin(*it, 1);
    return;
  }
  bool t = 0;
  for (__typeof(v[node].begin()) it = v[node].begin(); it != v[node].end();
       it++) {
    if (h[*it] < 2 && root[*it] == node) {
      if (t) {
        prin(*it, 1);
        continue;
      }
      data temp = dfs(*it, 0) + g(node, *it) - dfs(*it, 1);
      if (r == (tt + temp)) {
        ans.push_back(make_pair(node, *it));
        prin(*it, 0);
        t = 1;
      } else
        prin(*it, 1);
    }
  }
}
data solve(int x, bool t) {
  data &r = dp[x][t];
  if (hh[x][t]) return r;
  hh[x][t] = 1;
  r.ans = 0;
  r.cnt = 0;
  if (s == x) {
    if (root[root[x]] != s) {
      r = dfs(x, 0) + dfs(root[x], 0) + solve(root[root[x]], 1) + g(x, root[x]);
    } else
      r = dfs(x, 0) + dfs(root[x], 0) + g(x, root[x]);
    data tt = dfs(x, 1) + solve(root[x], 1);
    if (tt > r) r = tt;
    tt = dfs(x, 0) + solve(root[x], 0);
    if (tt > r) r = tt;
    return r;
  }
  if (root[x] == s) {
    if (t) return r = dfs(x, 1);
    r = dfs(x, 1);
    data temp = dfs(x, 0) + g(x, root[x]);
    if (!t && temp > r) r = temp;
    return r;
  }
  if (root[root[x]] != s)
    r = dfs(x, 0) + dfs(root[x], 0) + solve(root[root[x]], t) + g(x, root[x]);
  else {
    r = dfs(x, 0) + dfs(root[x], 0) + g(x, root[x]);
  }
  data temp = dfs(x, 1) + solve(root[x], t);
  if (temp > r) r = temp;
  return r;
}
void print(int x, bool t) {
  data &r = dp[x][t];
  if (s == x) {
    if (root[root[x]] != s) {
      if (r == dfs(x, 0) + dfs(root[x], 0) + solve(root[root[x]], 1) +
                   g(x, root[x])) {
        print(root[root[x]], 1);
        prin(x, 0);
        prin(root[x], 0);
        ans.push_back(make_pair(x, root[x]));
        return;
      }
    } else {
      if (r == dfs(x, 0) + dfs(root[x], 0) + g(x, root[x])) {
        ans.push_back(make_pair(x, root[x]));
        prin(x, 0);
        prin(root[x], 0);
        return;
      }
    }
    data tt = dfs(x, 1) + solve(root[x], 1);
    if (tt == r) {
      prin(x, 1);
      print(root[x], 1);
      return;
    }
    prin(x, 0);
    print(root[x], 0);
    return;
  }
  if (root[x] == s) {
    if (t) {
      prin(x, 1);
      return;
    }
    if (r == dfs(x, 1)) {
      prin(x, 0);
      ans.push_back(make_pair(x, root[x]));
      return;
    }
    prin(x, 0);
    ans.push_back(make_pair(x, root[x]));
    return;
  }
  if (root[root[x]] != s) {
    if (r ==
        dfs(x, 0) + dfs(root[x], 0) + solve(root[root[x]], t) + g(x, root[x])) {
      prin(x, 0);
      prin(root[x], 0);
      print(root[root[x]], t);
      ans.push_back(make_pair(x, root[x]));
      return;
    }
  } else {
    if (r == dfs(x, 0) + dfs(root[x], 0) + g(x, root[x])) {
      prin(x, 0);
      prin(root[x], 0);
      ans.push_back(make_pair(x, root[x]));
      return;
    }
  }
  prin(x, 1);
  print(root[x], t);
}
data get(int x) {
  while (3 != h[x]) {
    x = root[x];
    h[x]++;
  }
  s = x;
  solve(x, 0);
  print(x, 0);
  return solve(x, 0);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &x, &a[i]);
    root[i] = x;
    v[x].push_back(i);
  }
  data ans;
  for (int i = 1; i <= n; i++) {
    if (h[i]) continue;
    ans = ans + get(i);
  }
  cout << ans.ans << ' ' << ans.cnt << '\n';
  assert((int)(::ans.size()) == ans.ans);
  int tt = 0;
  for (__typeof(::ans.begin()) it = ::ans.begin(); it != ::ans.end(); it++) {
    tt += a[it->first] != a[it->second];
    printf("%d %d\n", it->first, it->second);
  }
  assert(tt == ans.cnt);
  return 0;
}