#include <bits/stdc++.h>
using namespace std;
int harmad;
int start;
vector<int> g[1000001];
int szulo[1000001];
int n;
int temp[1000001];
vector<int> v;
int cnt = 1;
int sum[1000001];
void beolvas() {
  cin >> n;
  for (int i = 1, szulo1, ho; i <= n; i++) {
    cin >> szulo1 >> ho;
    temp[i] = ho;
    szulo[i] = szulo1;
    g[szulo1].push_back(i);
    harmad += ho;
  }
  if (harmad % 3 != 0) {
    cout << -1 << endl;
    exit(0);
  }
  harmad /= 3;
}
void mbejar(int akt) {
  for (int i : g[akt]) {
    mbejar(i);
  }
  v.push_back(akt);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  beolvas();
  for (int i = 1; i <= n; i++) {
    if (!szulo[i]) {
      start = i;
      break;
    }
  }
  mbejar(start);
  for (int i : v) {
    sum[i] = temp[i];
    for (int j : g[i]) {
      sum[i] = sum[i] + sum[j];
    }
  }
  vector<int> mo;
  for (int i : v) {
    if (sum[i] == harmad && i != start) {
      int sz = i;
      int a = sum[i];
      while (sz != start) {
        sum[sz] -= a;
        sz = szulo[sz];
      }
      mo.push_back(i);
    }
    if (mo.size() == 2) {
      break;
    }
  }
  if (mo.size() == 2) {
    cout << mo.front() << " " << mo.back() << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}
