1 #include <iostream>
2 #include <cstdio>
3 #include <algorithm>
4 using namespace std;
5 typedef long long LL;
6 const int maxn = 40001;
7 const int INF = 0x3f3f3f3f;
8 struct arc{
9 int u,v,w;
10 bool operator<(const arc &t) const{
11 return w < t.w;
12 }
13 }e[maxn];
14 int uf[maxn],n,m,S,T,ret;
15 bool flag;
16 int Find(int x){
17 int t = x;
18 while(uf[x] != x) x = uf[x];
19 while(uf[t] != t){
20 int tmp = uf[t];
21 uf[t] = x;
22 t = tmp;
23 }
24 return x;
25 }
26 int kruskal(int low){
27 for(int i = 0; i <= n; ++i) uf[i] = i;
28 for(int i = low; i < m; ++i){
29 int x = Find(e[i].u);
30 int y = Find(e[i].v);
31 if(x == y) continue;
32 uf[x] = y;
33 if(Find(S) == Find(T)) return e[i].w - e[low].w;
34 if((LL)e[low].w + ret <= e[i].w) return INF;
35 }
36 flag = false;
37 return INF;
38 }
39 int main(){
40 //freopen("Weirdo.in","r",stdin);
41 //freopen("oo.txt","w",stdout);
42 while(~scanf("%d %d %d %d",&n,&m,&S,&T)){
43 for(int i = 0; i < m; ++i)
44 scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].w);
45 sort(e,e+m);
46 ret = INF;
47 flag = true;
48 for(int i = 0; i < m && flag; ++i)
49 ret = min(kruskal(i),ret);
50 printf("%d\n",ret);
51 }
52 return 0;
53 }