#include <bits/stdc++.h>
#define ll long long
using namespace std;
void findp(ll curr, vector<ll> parent[], vector<ll>& path, vector<vector<ll>>& p) {
if (curr==-1) {
vector<ll> p1 = path;
reverse(p1.begin(), p1.end());
p.push_back(p1);
return;
}
for (auto pp : parent[curr]) {
path.push_back(curr);
findp(pp, parent, path, p);
path.pop_back();
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n,m;
cin >> n >> m;
ll s,t,u1,v1;
cin >> s >> t >> u1 >> v1;
s--; t--; u1--; v1--;
vector<vector<pair<ll,ll>>> adj(n);
for (int i=0; i<m; i++) {
ll u,v,c;
cin >> u >> v >> c;
u--; v--;
adj[u].push_back({v,c});
adj[v].push_back({u,c});
}
priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>> pq;
pq.push({0, s});
vector<ll> dist(n, 1e18);
dist[s] = 0;
vector<ll> parent[n];
parent[s].push_back(-1);
while(!pq.empty()) {
auto [d,u] = pq.top();
pq.pop();
if (d>dist[u]) continue;
for (auto [v,w] : adj[u]) {
if (dist[u]+w<dist[v]) {
dist[v] = dist[u]+w;
pq.push({dist[v], v});
parent[v].clear();
parent[v].push_back(u);
} else if (dist[u]+w==dist[v]) {
parent[v].push_back(u);
}
}
}
vector<vector<ll>> p;
vector<ll> path;
findp(t, parent, path, p);
ll res = 1e18;
for (auto p1 : p) {
vector<vector<pair<ll,ll>>> adj1 = adj;
for (int i=1; i<p1.size(); i++) {
adj1[p1[i]].push_back({p1[i-1], 0});
adj1[p1[i-1]].push_back({p1[i], 0});
}
for (int i=0; i<n; i++) dist[i] = 1e18;
dist[u1] = 0;
pq.push({0, u1});
while(!pq.empty()) {
auto [d,u] = pq.top();
pq.pop();
if (d>dist[u]) continue;
for (auto [v,w] : adj1[u]) {
if (dist[v]>dist[u]+w) {
dist[v] = dist[u]+w;
pq.push({dist[v], v});
}
}
}
res = min(res, dist[v1]);
}
cout << res;
}