#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
const int maxn=300010;
int n,m,cnt,maxx,minn;
int head[maxn],to[maxn<<1],next[maxn<<1],val[maxn<<1];
int deep[maxn],son[maxn],top[maxn],fa[maxn],size[maxn],dis[maxn],v[maxn];
int s[maxn],q[maxn],p1[maxn],p2[maxn],pa[maxn],len[maxn];
int readin() //读入优化
{
int ret=0; char gc;
while(gc<‘0‘||gc>‘9‘) gc=getchar();
while(gc>=‘0‘&&gc<=‘9‘) ret=ret*10+gc-‘0‘,gc=getchar();
return ret;
}
void add(int a,int b,int c)
{
to[cnt]=b,val[cnt]=c,next[cnt]=head[a],head[a]=cnt++;
}
void dfs1(int x)
{
size[x]=1,q[++q[0]]=x;
for(int i=head[x];i!=-1;i=next[i])
{
if(to[i]!=fa[x])
{
v[to[i]]=val[i],deep[to[i]]=deep[x]+1,dis[to[i]]=dis[x]+val[i],fa[to[i]]=x;
dfs1(to[i]);
size[x]+=size[to[i]];
if(size[to[i]]>size[son[x]]) son[x]=to[i];
}
}
}
int getlca(int x,int y) //树剖
{
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
x=fa[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
return x;
}
int check(int sta)
{
int i,sum=0;
memset(s,0,sizeof(s));
for(i=1;i<=m;i++)
if(len[i]>sta)
s[p1[i]]++,s[p2[i]]++,s[pa[i]]-=2,sum++;
for(i=n;i>=1;i--)
{
s[fa[q[i]]]+=s[q[i]];
if(v[q[i]]>=maxx-sta&&s[q[i]]==sum) return 1;
}
return 0;
}
int main()
{
n=readin(),m=readin();
memset(head,-1,sizeof(head));
int i,j,a,b,c;
for(i=1;i<n;i++)
{
a=readin(),b=readin(),c=readin();
add(a,b,c),add(b,a,c),minn=max(minn,c);
}
deep[1]=top[1]=1;
dfs1(1);
for(i=1;i<=n;i++) //用DFS序
{
if(q[i]==son[fa[q[i]]]) top[q[i]]=top[fa[q[i]]];
else top[q[i]]=q[i];
}
for(i=1;i<=m;i++) //预处理
{
p1[i]=readin(),p2[i]=readin();
pa[i]=getlca(p1[i],p2[i]);
len[i]=dis[p1[i]]+dis[p2[i]]-2*dis[pa[i]];
maxx=max(maxx,len[i]);
}
int l=maxx-minn,r=maxx+1,mid; //缩小范围
while(l<r)
{
mid=l+r>>1;
if(check(mid)) r=mid;
else l=mid+1;
}
printf("%d",r);
return 0;
}