#include <iostream>
#include <cstring>
#include <bitset>
#include <unordered_map>
#include <queue>
#define x first
#define y second
using namespace std;
using PII = pair <int,int>;
using LL = long long;
const int N = 1e6;
int p[5], n, m, h[N], e[N], w[N], ne[N], idx;
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
LL getIdx(int i, int j) {
return i * N + j;
}
int dijkstra(int s, int en) {
int dist[N];
memset(dist, 0x3f, sizeof dist);
dist[s] = 0;
bitset <N> st;
priority_queue <PII, vector <PII>, greater <PII>> heap;
heap.push({0,s});
while(heap.size()) {
auto t = heap.top(); heap.pop();
if (st[t.y]) continue;
st[t.y] = 1;
for (int i = h[t.y]; ~i; i = ne[i]) {
int j = e[i];
if (dist[j] > dist[t.y] + w[i]) {
dist[j] = dist[t.y] + w[i];
heap.push({dist[j],j});
}
}
}
return dist[en];
}
int id = 0;
vector <vector <int>> Sequences;
bitset <5> st;
void dfs(vector <int> Sequence, int id) {
if (id == 5) {
Sequences.push_back(Sequence);
return ;
}
for (int i = 0; i < 5; ++i) {
if (!st[i]) {
st[i] = 1;
Sequence[id] = p[i];
dfs(Sequence, id + 1);
st[i] = 0;
}
}
}
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
memset(h, -1, sizeof h);
for (int i = 0; i < 5; ++i) cin >> p[i];
while(m--) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c), add(b, a, c);
}
unordered_map <LL,int> hashtable;
for (int i = 0; i < 5; ++i) hashtable[getIdx(1,p[i])] = hashtable[getIdx(p[i],1)] = dijkstra(1,p[i]);
for (int i = 0; i < 5; ++i)
for (int j = i + 1; j < 5; ++j)
hashtable[getIdx(p[i],p[j])] = hashtable[getIdx(p[j],p[i])] = dijkstra(p[i],p[j]);
vector <int> Sequence(5);
dfs(Sequence,0);
int res = 0x7fffffff;
for (auto& Sequence : Sequences) {
int ans = 0;
for (int i = 0; i < 5; ++i) {
if (!i) ans += hashtable[getIdx(1,Sequence[i])];
else ans += hashtable[getIdx(Sequence[i - 1],Sequence[i])];
}
res = min(res, ans);
}
cout << res;
return 0;
}