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>
#pragma GCC optimize(2)
using namespace std;
long long n, sex[100010], ced, hs;
pair<long long, long long> dp[100010][2], ans;
bool vis[100010];
vector<pair<long long, long long> > vt[100010], ans_path, pat;
vector<long long> cyc, pas;
stack<long long> path, ped;
void dfs(long long x, long long lst) {
  vis[x] = true;
  pas.push_back(x);
  path.push(x);
  ped.push(lst);
  long long i, j;
  for (i = 0; i < vt[x].size(); i++) {
    if (vt[x][i].second != lst && vt[x][i].second != ced) {
      if (vis[vt[x][i].first]) {
        ced = vt[x][i].second;
        cyc.push_back(ced);
        hs = vt[x][i].first;
        while (!path.empty() && (path.top() != vt[x][i].first)) {
          path.pop();
          cyc.push_back(ped.top());
          ped.pop();
        }
      } else {
        dfs(vt[x][i].first, vt[x][i].second);
        if (x == hs) {
          hs = -1;
        }
      }
    }
  }
  if (hs == -1) {
    path.pop();
    ped.pop();
  }
  return;
}
void dfs2(long long x, long long lst) {
  long long i;
  pair<long long, long long> sum = make_pair(0, 0), upd;
  for (i = 0; i < vt[x].size(); i++) {
    if (vt[x][i].second == lst || vt[x][i].second == ced) {
      continue;
    }
    dfs2(vt[x][i].first, vt[x][i].second);
    upd = max(dp[vt[x][i].first][0], dp[vt[x][i].first][1]);
    sum.first += upd.first;
    sum.second += upd.second;
  }
  dp[x][0] = sum;
  for (i = 0; i < vt[x].size(); i++) {
    if (vt[x][i].second == lst || vt[x][i].second == ced) {
      continue;
    }
    upd = max(dp[vt[x][i].first][0], dp[vt[x][i].first][1]);
    sum.first -= upd.first;
    sum.second -= upd.second;
    sum.first += dp[vt[x][i].first][0].first;
    sum.second += dp[vt[x][i].first][0].second;
    sum.first++;
    if (sex[x] != sex[vt[x][i].first]) {
      sum.second++;
    }
    dp[x][1] = max(dp[x][1], sum);
    sum.first += upd.first;
    sum.second += upd.second;
    sum.first -= dp[vt[x][i].first][0].first;
    sum.second -= dp[vt[x][i].first][0].second;
    sum.first--;
    if (sex[x] != sex[vt[x][i].first]) {
      sum.second--;
    }
  }
  return;
}
void getdp(long long rt) {
  long long i;
  for (i = 0; i < pas.size(); i++) {
    dp[pas[i]][0] = dp[pas[i]][1] = make_pair(0, 0);
  }
  dfs2(rt, -1);
  return;
}
void get_path(long long x, long long tp, long long lst) {
  long long i, j;
  pair<long long, long long> sum = make_pair(0, 0), upd;
  if (tp == 0) {
    for (i = 0; i < vt[x].size(); i++) {
      if (vt[x][i].second == lst || vt[x][i].second == ced) {
        continue;
      }
      if (dp[vt[x][i].first][0] > dp[vt[x][i].first][1]) {
        get_path(vt[x][i].first, 0, vt[x][i].second);
      } else {
        get_path(vt[x][i].first, 1, vt[x][i].second);
      }
    }
    return;
  }
  for (i = 0; i < vt[x].size(); i++) {
    if (vt[x][i].second == lst || vt[x][i].second == ced) {
      continue;
    }
    upd = max(dp[vt[x][i].first][0], dp[vt[x][i].first][1]);
    sum.first += upd.first;
    sum.second += upd.second;
  }
  for (i = 0; i < vt[x].size(); i++) {
    if (vt[x][i].second == lst || vt[x][i].second == ced) {
      continue;
    }
    upd = max(dp[vt[x][i].first][0], dp[vt[x][i].first][1]);
    sum.first -= upd.first;
    sum.second -= upd.second;
    sum.first += dp[vt[x][i].first][0].first;
    sum.second += dp[vt[x][i].first][0].second;
    sum.first++;
    if (sex[x] != sex[vt[x][i].first]) {
      sum.second++;
    }
    if (dp[x][1] == sum) {
      pat.push_back(make_pair(x, vt[x][i].first));
      get_path(vt[x][i].first, 0, vt[x][i].second);
      for (j = 0; j < vt[x].size(); j++) {
        if (vt[x][j].second == lst || vt[x][j].second == ced || j == i) {
          continue;
        }
        if (dp[vt[x][j].first][0] > dp[vt[x][j].first][1]) {
          get_path(vt[x][j].first, 0, vt[x][j].second);
        } else {
          get_path(vt[x][j].first, 1, vt[x][j].second);
        }
      }
      break;
    }
    dp[x][1] = max(dp[x][1], sum);
    sum.first += upd.first;
    sum.second += upd.second;
    sum.first -= dp[vt[x][i].first][0].first;
    sum.second -= dp[vt[x][i].first][0].second;
    sum.first--;
    if (sex[x] != sex[vt[x][i].first]) {
      sum.second--;
    }
  }
  return;
}
void solve(long long rt) {
  long long i;
  hs = -1;
  pair<long long, long long> ret = make_pair(0, 0);
  cyc.clear();
  pas.clear();
  ced = -1;
  dfs(rt, -1);
  getdp(rt);
  if (dp[rt][0] > dp[rt][1] && ret < dp[rt][0]) {
    ret = dp[rt][0];
    pat.clear();
    get_path(rt, 0, -1);
  } else if (ret < dp[rt][1]) {
    ret = dp[rt][1];
    pat.clear();
    get_path(rt, 1, -1);
  }
  ced = cyc[1];
  getdp(rt);
  if (dp[rt][0] > dp[rt][1] && ret < dp[rt][0]) {
    ret = dp[rt][0];
    pat.clear();
    get_path(rt, 0, -1);
  } else if (ret < dp[rt][1]) {
    ret = dp[rt][1];
    pat.clear();
    get_path(rt, 1, -1);
  }
  ans.first += ret.first;
  ans.second += ret.second;
  for (i = 0; i < pat.size(); i++) {
    ans_path.push_back(pat[i]);
  }
  return;
}
int main() {
  long long i, x;
  scanf("%lld", &n);
  for (i = 0; i < n; i++) {
    scanf("%lld%lld", &x, &sex[i]);
    x--;
    sex[i]--;
    vt[i].push_back(make_pair(x, i));
    vt[x].push_back(make_pair(i, i));
  }
  ans = make_pair(0, 0);
  for (i = 0; i < n; i++) {
    if (!vis[i]) {
      solve(i);
    }
  }
  printf("%lld %lld\n", ans.first, ans.second);
  for (i = 0; i < ans_path.size(); i++) {
    printf("%lld %lld\n", ans_path[i].first + 1, ans_path[i].second + 1);
  }
  return 0;
}