#include <bits/stdc++.h>
using namespace std;
const int N = 3e5+10,inf = 3e8;
struct node { int v,w; };
struct edge { int u,v,w,l; };
int n,m,s[N],fa[N][20],de[N],d[N],maxt,maxw,maxm;
vector<node> g[N]; edge e[N];
void add(int u,int v,int w) { g[u].push_back({v,w}); }
void dfs1(int u,int f) {
fa[u][0]=f,de[u]=de[f]+1;
for(int i=1;(1<<i)<=de[u];i++)
fa[u][i]=fa[fa[u][i-1]][i-1];
for(auto e : g[u])
if(e.v!=f) {
s[e.v]=s[u]+e.w;
dfs1(e.v,u);
}
}
int lca(int u,int v) {
if(de[u]<de[v]) swap(u,v);
for(int i=19;i>=0;i--)
if(de[fa[u][i]]>=de[v])
u=fa[u][i];
if(u==v) return u;
for(int i=19;i>=0;i--)
if(fa[u][i]!=fa[v][i])
u=fa[u][i],v=fa[v][i];
return fa[u][0];
}
void dfs2(int u,int f,int w) {
for(auto e : g[u]) {
int v=e.v;
if(v!=f) {
dfs2(v,u,e.w);
d[u]+=d[v];
}
}
if(u!=1 && d[u]>=maxt) {
maxt=d[u];
maxm=max(maxm,w);
}
}
bool check(int x) {
int tot=0; maxm=maxt=0;
for(int i=0;i<=n;i++) d[i]=0;
for(int i=1;i<=m;i++) {
auto [u,v,w,l]=e[i];
if(w>x) d[u]++,d[v]++,d[l]-=2,tot++;
}
dfs2(1,0,0);
if(maxt<tot) return 0;
return (maxw-maxm<=x);
}
int main()
{
ios::sync_with_stdio(false);
cout.tie(0);cin.tie(0);
cin>>n>>m;
for(int i=1,u,v,w;i<n;i++) {
cin>>u>>v>>w;
add(u,v,w); add(v,u,w);
}
dfs1(1,0);
for(int i=1,u,v,w,l;i<=m;i++) {
cin>>u>>v;
l=lca(u,v);
w=s[u]+s[v]-2*s[l];
e[i]={u,v,w,l};
maxw=max(maxw,w);
}
int l=0,r=inf,mid,ans=inf+10;
while(l<=r) {
mid=(l+r)>>1;
if(check(mid)) {
ans=min(ans,mid);
r=mid-1;
}
else l=mid+1;
}
cout<<ans;
return 0;
}
记录