#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define lsb(x) (x&(-x))
#define pii pair<int,int>
#define ss second
#define ff first
#define piii pair<int,pii>
#define debu(x) (cerr << #x << " = "<< x << "\n")
#define debu2(x,y) (cerr << #x << " = "<< x << " " << #y << " = " << y << "\n")
#define debu3(x,y,z) (cerr << #x << " = "<< x << " " << #y << " = " << y << " " << #z << " = " << z<< "\n")
#define bitout(x,y) {\
cerr << #x << " : ";\
for (int justforbits = y; justforbits >=0; justforbits--)cout << (((1 << justforbits) & x)>=1);\
cout << "\n";\
}
#define rangeout(j,rangestart,rangeend) {\
cerr << "outputting " << #j<< ":\n";\
for (int forrang = rangestart; forrang <= rangeend; forrang++)cerr << j[forrang] << " ";\
cerr<<"\n";\
}
#define c1 {cerr << "Checkpoint 1! \n\n";cerr.flush();}
#define c2 {cerr << "Checkpoint 2! \n\n";cerr.flush();}
#define c3 {cerr << "Checkpoint 3! \n\n";cerr.flush();}
#define c4 {cerr << "Checkpoint 4! \n\n";cerr.flush();}
int n,m;
vector<pii>stuffs;
vector<int>foreach;
bool isring=true;
int maxxy=0;
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W)
{
int n=N,m=M;
foreach.resize(n);
for(int i=0;i<n;i++)
{
foreach[V[i]]=W[i];
stuffs.pb(mp(W[i],V[i]));
}
foreach[0]=0;
sort(stuffs.begin(),stuffs.end());
}
int getMinimumFuelCapacity(int X, int Y)
{
if(n==2)return -1;
if(n==3&&(X!=0&&Y!=0))return -1;
int ans=max(foreach[X],foreach[Y]);
int curr=0;
while(stuffs[curr].ss==X||stuffs[curr].ss==Y)
{
curr++;
}
ans=max(ans,stuffs[curr].ff);
return ans;
}