#include <bits/stdc++.h>
using namespace std;
const int N=100005;
const int MX=INT_MAX;
int n,m;
int a[5];
int d[6][N];
typedef pair<int,int> PII;
vector<PII> g[N];
void dij(int idx, int s) {
priority_queue<PII, vector<PII>, greater<PII> > q;
vector<bool> vis(n + 1, false);
for (int i = 1; i <= n; ++i) d[idx][i] = INT_MAX;
d[idx][s] = 0;
q.push(make_pair(0, s));
while (!q.empty()) {
PII top = q.top(); q.pop();
int u = top.second;
if (vis[u]) continue;
vis[u] = true;
for (size_t i = 0; i < g[u].size(); ++i) {
int v = g[u][i].first;
int w = g[u][i].second;
if (d[idx][v] > d[idx][u] + w) {
d[idx][v] = d[idx][u] + w;
q.push(make_pair(d[idx][v], v));
}
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=5;i++){
cin>>a[i];
}
while(m--){
int x,y,t;
cin>>x>>y>>t;
g[x].push_back({y, t});
g[y].push_back({x, t});
}
for(int i=1;i<=5;i++){
dij(i,a[i]);
}
int ans=0x3f3f3f3f;
for (int city = 1; city <= n; ++city) {
int sum = 0;
for (int i = 1; i <= 5; ++i) {
if (d[i][city] == MX) {
sum = MX;
break;
}
sum += d[i][city];
}
if (sum < ans) ans = sum;
}
cout << ans << "\n";
return 0;
}