#include<bits/stdc++.h>
using namespace std;
#define CIO
#ifdef LOCAL
ifstream in("data.in"); ostream &out = cout; ofstream err("err.out");
#endif
#ifdef CIO
istream &in = cin; ostream &out = cout;
#endif
#ifdef FILE
ifstream in("data.in"); ofstream out("data.out");
#endif
#define newl '\n'
using LL = long long;
const int N = 1505, M = 6e5 + 5;
int n, m; LL ans;
priority_queue<pair<LL, int> > pq; bool v[N];
queue<int> q;
struct graph{
int head[N], to[M], nxt[M], tot; LL val[M];
graph(){ tot = 1; }
void add(int x, int y, LL z){
to[++tot] = y, val[tot] = z, nxt[tot] = head[x], head[x] = tot;
}
void Dijkstra(int st, LL d[N]){
memset(d, 0x3f, sizeof(LL) * N);
memset(v, 0, sizeof v);
d[st] = 0; pq.push(make_pair(0, st));
while(pq.size()){
int x = pq.top().second; pq.pop();
if(v[x]) continue; v[x] = true;
for(int e = head[x]; e; e = nxt[e]){
int y = to[e];
if(d[y] > d[x] + val[e]){
d[y] = d[x] + val[e];
pq.push(make_pair(-d[y], y));
}
}
}
}
void Toposort(int deg[N], LL d[N]){
memset(d, 0, sizeof(LL) * N);
for(int x = 1; x <= n; x++){
if(!deg[x]) q.push(x);
}
while(q.size()){
int x = q.front(); q.pop();
for(int e = head[x]; e; e = nxt[e]){
int y = to[e];
d[y] = max(d[y], d[x] + val[e]);
if(--deg[y] == 0) q.push(y);
}
}
for(int x = 1; x <= n; x++) ans = max(ans, d[x]);
}
} og, ng1, ng2;
int deg1[N], deg2[N];
LL ds1[N], ds2[N], dt1[N], dt2[N], tmp[N];
int main(){
ios::sync_with_stdio(0), in.tie(0), out.tie(0);
int s1, t1, s2, t2;
in >> n >> m >> s1 >> t1 >> s2 >> t2;
for(int i = 1; i <= m; i++){
int u, v, w; in >> u >> v >> w;
og.add(u, v, w), og.add(v, u, w);
}
og.Dijkstra(s1, ds1), og.Dijkstra(t1, dt1);
og.Dijkstra(s2, ds2), og.Dijkstra(t2, dt2);
for(int e = 2; e <= 2 * m; e++){
int x = og.to[e ^ 1], y = og.to[e];
if(ds1[x] + og.val[e] + dt1[y] == ds1[t1] && ds2[x] + og.val[e] + dt2[y] == ds2[t2]){
deg1[y]++; ng1.add(x, y, og.val[e]);
}
}
ng1.Toposort(deg1, tmp);
for(int e = 2; e <= 2 * m; e++){
int x = og.to[e ^ 1], y = og.to[e];
if(ds1[x] + og.val[e] + dt1[y] == ds1[t1] && dt2[x] + og.val[e] + ds2[y] == ds2[t2]){
deg2[y]++; ng2.add(x, y, og.val[e]);
}
}
ng2.Toposort(deg2, tmp);
out << ans << newl;
}