#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000100;
int t[MAXN];
vector<int> graf[MAXN];
int n;
int uk[MAXN];
int rt, sve;
int tr[MAXN];
void load() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    graf[a - 1].push_back(i);
    if (a == 0) rt = i;
    t[i] = b;
  }
}
void dfs(int x, int p) {
  for (int i = 0; i < graf[x].size(); i++) {
    int sus = graf[x][i];
    if (sus == p) continue;
    dfs(sus, x);
    uk[x] += uk[sus];
  }
  uk[x] += t[x];
}
void dfs2(int x, int p) {
  if (uk[x] == sve) tr[x] = x;
  for (int sus : graf[x]) {
    if (sus == p) continue;
    dfs2(sus, x);
    if (tr[x] == -1) tr[x] = tr[sus];
  }
}
void dfs3(int x, int p) {
  if (uk[x] == 2 * sve && tr[x] != -1 && tr[x] != x) {
    printf("%d %d\n", x + 1, tr[x] + 1);
    exit(0);
  }
  int pr = -1, dr = -1;
  for (int sus : graf[x]) {
    if (sus == p) continue;
    if (tr[sus] != -1) {
      if (pr == -1)
        pr = tr[sus];
      else if (dr == -1)
        dr = tr[sus];
    }
    dfs3(sus, x);
  }
  if (dr != -1 && pr != dr) {
    printf("%d %d\n", pr + 1, dr + 1);
    exit(0);
  }
}
void solve() {
  dfs(rt, -1);
  sve = uk[rt];
  if (sve % 3) {
    printf("-1\n");
    return;
  }
  sve /= 3;
  for (int i = 0; i < n; i++) tr[i] = -1;
  dfs2(rt, -1);
  dfs3(rt, -1);
  printf("-1\n");
}
int main() {
  load();
  solve();
  return 0;
}
