#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 50010, M = 200010;
const int inf = 0x3f3f3f3f;
int n, m;
int h[N], e[M], ne[M], w[M], idx;
int source[6];
int dist[6][N];
bool st[N];
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void dijkstra(int start, int dist[]) {
memset(dist, 0x3f, N * 4);
memset(st, false, sizeof st);
dist[start] = 0;
priority_queue<PII, vector<PII>, greater<PII>> q;
q.push({0, start});
while (!q.empty()) {
auto [distence, ver] = q.top();
q.pop();
if (st[ver]) continue;
st[ver] = true;
for (int i = h[ver]; i != -1; i = ne[i]) {
int j = e[i];
if (dist[j] > distence + w[i]) {
dist[j] = distence + w[i];
q.push({dist[j], j});
}
}
}
}
int dfs(int u, int start, int distence) {
if (u == 6) return distence;
int res = inf;
for (int i = 1; i <= 5; i++) {
if (!st[i]) {
st[i] = true;
int next = source[i];
res = min(res, dfs(u + 1, i, dist[start][next] + distence));
st[i] = false;
}
}
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
memset(h, -1, sizeof h);
cin >> n >> m;
for (int i = 1; i <= 5; i++) cin >> source[i];
source[0] = 1;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
for (int i = 0; i <= 5; i++) dijkstra(source[i], dist[i]);
memset(st, false, sizeof st);
cout << dfs(1, 0, 0) << endl;
return 0;
}