#include <bits/stdc++.h>
using namespace std;
int f, m, g, ans;
vector<vector<long long> > al(1000005);
vector<long long> bv(1000005), visited(1000005, 0), sv(1000005);
void dfs(long long node) {
  long long i, j;
  if (f) return;
  visited[node] = 1;
  sv[node] = bv[node];
  for (j = 0; j < al[node].size(); j++) {
    if (visited[al[node][j]] == 0 && al[node][j] != m) {
      dfs(al[node][j]);
      sv[node] += sv[al[node][j]];
    }
  }
  if (f == 0 && sv[node] == g) {
    ans = node;
    f = 1;
  }
}
int main() {
  long long i, j, k, n, sp;
  g = 0;
  scanf("%lld", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &j);
    scanf("%lld", &bv[i]);
    g += bv[i];
    if (j == 0)
      sp = i;
    else {
      al[i].push_back(j);
      al[j].push_back(i);
    }
  }
  if (g % 3)
    cout << -1;
  else {
    g /= 3;
    f = 0;
    m = -1;
    dfs(sp);
    if (f == 0 || ans == sp)
      cout << -1;
    else {
      for (i = 1; i <= n; i++) visited[i] = 0;
      j = ans;
      f = 0;
      m = j;
      dfs(sp);
      if (f == 0 || ans == sp)
        cout << -1;
      else {
        cout << j << " " << ans;
      }
    }
  }
}
