#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int,pii> ipii;
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define rdint(x,y) rnd()%(y-x+1)+x
#define debug(x) cerr<<#x<<" is "<<x<<endl;
#define debugp(x,y) cerr<<#x<<' '<<#y<<" is "<<x<<' '<<y<<endl;
#define debugl(x) cerr<<#x<<" is ";for(auto p:x)cerr<<p<<" ";cerr<<endl;
#define debugpl(x) cerr<<#x<<" is ";for(auto p:x)cerr<<p.first<<" "<<p.second<<" , ";cerr<<endl;
#include <vector>
const int maxn=1e5+5;
const int inf=1e9+5;
multiset<int> s;
int w[maxn];
void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
s.insert(inf);
w[0]=inf;
for(int i:W)s.insert(i);
for(int i=0;i<M;i++){
w[V[i]]=W[i];
}
}
int getMinimumFuelCapacity(int X, int Y) {
if(X==0){
s.erase(s.find(w[Y]));
int c=*s.begin();
s.insert(w[Y]);
if(c==inf)return -1;
return w[Y]+c;
}
s.erase(s.find(w[X]));
s.erase(s.find(w[Y]));
int c=*s.begin();
s.insert(w[X]);
s.insert(w[Y]);
if(c==inf)return -1;
return max(w[X],w[Y])+c;
}