#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> node;
const int N = 50010, M = 200010;
int dist[6][N];
int h[N], w[M], e[M], ne[M], idx;
bool st[N];
int stop[6];
int n, m;
int res = 0x3f3f3f3f;
bool book[6];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void dfs(int u, int i, int sum)
{
if (sum >= res) return;
if (u == 5)
{
res = min(res, sum);
return;
}
for (int j = 1; j <= 5; j++)
{
if (book[j]) continue;
book[j] = true;
dfs(u + 1, j, sum + dist[i][stop[j]]);
book[j] = false;
}
}
void dijkstra(int k)
{
memset(dist[k], 0x3f, sizeof dist[k]);
memset(st, 0, sizeof st);
dist[k][stop[k]] = 0;
priority_queue<node, vector<node>, greater<node>> heap;
heap.push({0, stop[k]});
while (!heap.empty())
{
auto top = heap.top();
heap.pop();
int distance = top.first, t = top.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] > dist[k][t] + w[i])
{
dist[k][j] = dist[k][t] + w[i];
heap.push({dist[k][j], j});
}
}
}
}
int main()
{
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
stop[0] = 1;
for (int i = 1; i <= 5; i++) scanf("%d", &stop[i]);
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 <= 5; i++) dijkstra(i);
dfs(0, 0, 0);
printf("%d\n", res);
return 0;
}