#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000010;
namespace Tree {
int head[MAXN], nxt[MAXN << 1], to[MAXN << 1], eidx;
void init() {
  eidx = 0;
  memset(head, -1, sizeof(head));
}
void adde(int u, int v) {
  to[eidx] = v;
  nxt[eidx] = head[u];
  head[u] = eidx++;
  to[eidx] = u;
  nxt[eidx] = head[v];
  head[v] = eidx++;
}
}  // namespace Tree
int n, root;
int sum = 0, part;
int w[MAXN], sumw[MAXN], flag[MAXN];
int f1cnt[MAXN];
queue<int> bfsq;
bool vis[MAXN] = {0};
void solve1(int u, int fa) {
  using namespace Tree;
  vis[u] = true;
  for (int i = head[u]; ~i; i = nxt[i]) {
    int v = to[i];
    if (v == fa) continue;
    if (f1cnt[v]) bfsq.push(v), vis[v] = true;
  }
  int outcnt = 0;
  while (!bfsq.empty()) {
    int u = bfsq.front();
    bfsq.pop();
    if (flag[u] == 1) {
      printf("%d ", u);
      if (++outcnt == 2) exit(0);
      continue;
    }
    for (int i = head[u]; ~i; i = nxt[i]) {
      int v = to[i];
      if (vis[v]) continue;
      if (f1cnt[v]) bfsq.push(v), vis[v] = true;
    }
  }
}
void solve2(int u, int fa) {
  using namespace Tree;
  printf("%d ", u);
  vis[u] = true;
  for (int i = head[u]; ~i; i = nxt[i]) {
    int v = to[i];
    if (v == fa) continue;
    if (f1cnt[v]) bfsq.push(v), vis[v] = true;
  }
  while (!bfsq.empty()) {
    int u = bfsq.front();
    bfsq.pop();
    if (flag[u] == 1) {
      printf("%d ", u);
      exit(0);
    }
    for (int i = head[u]; ~i; i = nxt[i]) {
      int v = to[i];
      if (vis[v]) continue;
      if (f1cnt[v]) bfsq.push(v), vis[v] = true;
    }
  }
}
void sumw_dfs(int u, int fa) {
  using namespace Tree;
  sumw[u] = w[u];
  f1cnt[u] = 0;
  for (int i = head[u]; ~i; i = nxt[i]) {
    int v = to[i];
    if (v == fa) continue;
    sumw_dfs(v, u);
    sumw[u] += sumw[v];
    if (f1cnt[v] && f1cnt[u]) solve1(u, fa);
    f1cnt[u] += f1cnt[v];
  }
  if (sumw[u] == part)
    flag[u] = 1;
  else if (sumw[u] == part * 2)
    flag[u] = 2;
  else
    flag[u] = 0;
  if (u != root && flag[u] == 2 && f1cnt[u]) solve2(u, fa);
  if (flag[u] == 1) ++f1cnt[u];
}
void calc_sumw() { sumw_dfs(root, -1); }
int main() {
  scanf("%d", &n);
  Tree::init();
  for (int i = 1; i <= n; ++i) {
    int pa;
    scanf("%d%d", &pa, w + i);
    sum += w[i];
    if (pa == 0)
      root = i;
    else
      Tree::adde(i, pa);
  }
  if (sum % 3 != 0) {
    printf("-1");
    return 0;
  }
  part = sum / 3;
  calc_sumw();
  printf("-1\n");
  return 0;
}
