//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
#include<iostream>
#include<queue>
#include<algorithm>
#include<math.h>
#include<cstring>
#include<stack>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
const int N = 50010, M = 200010, INF = 0x3f3f3f3f;
int h[N], e[M], ne[M], w[M], idx;
int source[7], dist[7][N];
int us[N],st[N];
int n, m;
typedef pair<int, int>PII;
void add(int a, int b, int c) {
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx++;
}
void diji(int start,int id) {
memset(us, 0, sizeof us);
dist[id][start] = 0;
priority_queue<PII, vector<PII>, greater<PII>>heap;
heap.push({ 0,start });
while (heap.size()) {
auto t = heap.top();
heap.pop();
int ver = t.second, dis = t.first;
if (us[ver] == 1)continue;
us[ver] = 1;
for (int i = h[ver]; i != -1; i = ne[i]) {
int j = e[i];
if (us[j] == 0 && dist[id][j] > dis + w[i]) {
dist[id][j] = dis + w[i];
heap.push({ dist[id][j],j });
}
}
}
}
int dfs(int u, int start, int distance) {
if (u == 6)return distance;
int res = INF;
for (int i = 1; i <= 5; i++) {
if (st[i] == 0) {
int next = source[i];
st[i] = 1;
res = min(res, dfs(u + 1, i, distance + dist[start][next]));
st[i] = 0;
}
}
return res;
}
int main() {
cin >> n >> m;
source[0] = 1;
for (int i = 1; i <= 5; i++)cin >> source[i];
memset(h, -1, sizeof h);
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
memset(dist, 0x3f, sizeof dist);
for (int i = 0; i < 6; i++)diji(source[i],i);
cout << dfs(1, 0, 0);
return 0;
}