#include "swap.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <math.h>
#include <iomanip>
#define rep(i, s, e) for (ll i = s; i < e; i++)
#define upmax(a, b) a = max(a, b)
#define upmin(a, b) a = min(a, b)
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
const ll INF = 2e18;
const ll MOD = 1e9 + 7;
ll n, m;
multiset<pll> min_edge;
vvpll g;
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
n = N, m = M;
g.clear(), g.resize(n);
rep(i, 0, m) {
g[U[i]].push_back({ V[i], W[i] });
g[V[i]].push_back({ U[i], W[i] });
}
rep(i, 0, m) {
min_edge.insert({ W[i], V[i] });
if (min_edge.size() > 3) {
min_edge.erase(prev(min_edge.end()));
}
}
}
int getMinimumFuelCapacity(int X, int Y) {
if (n <= 3) return -1;
ll x = X, y = Y;
ll res = 0;
if (x != 0) {
upmax(res, g[x][0].second);
}
if (y != 0) {
upmax(res, g[y][0].second);
}
for (auto& it : min_edge) {
if (it.second != x && it.second != y) {
upmax(res, it.first);
break;
}
}
return res;
}