#include <iostream>
#include <algorithm>
#include <cstring>
#include <unordered_map>
#include <queue>
using namespace std;
typedef pair<int, int> node;
const int N = 50010, M = 200010;
int n, m;
int h[N], e[M], ne[M], w[M], idx;
int stop[6];
unordered_map<int, int> mp;
int dist[6][N];
bool st[N];
int res = 0x3f3f3f3f;
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void dijkstra(int k, int start) {
memset(dist[k], 0x3f, sizeof dist[k]);
memset(st, 0, sizeof st);
dist[k][start] = 0;
priority_queue<node, vector<node>, greater<node>> heap;
heap.push({0, start});
while (!heap.empty()) {
auto head = heap.top();
heap.pop();
int distance = head.first, t = head.second;
if (st[t]) continue;
st[t] = true;
for (int i = h[t]; ~i; i = ne[i]) {
int j = e[i];
if (dist[k][j] > distance + w[i]) {
dist[k][j] = distance + w[i];
heap.push({dist[k][j], j});
}
}
}
}
void dfs(int u, int i, int cost) {
if (cost >= res) return;
if (u == 5) {
res = cost;
return;
}
for (int j = 1; j <= 5; j++) {
if (st[stop[j]]) continue;
st[stop[j]] = true;
dfs(u + 1, j, cost + dist[i][stop[j]]);
st[stop[j]] = false;
}
}
int main() {
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
stop[0] = 1;
for (int i = 1; i <= 5; i++) cin >> stop[i]; // 将亲戚车站映射到1,2,...5
while (m--) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c), add(b, a, c);
}
for (int i = 0; i < 6; i++) dijkstra(i, stop[i]);
memset(st, 0, sizeof st);
dfs(0, 0, 0);
cout << res << endl;
return 0;
}