import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

int readint() {
    return readln.chomp.to!int;
}

int[] readints() {
    return readln.split.map!(to!int).array;
}

int[][] warshallFloyd(int n, int[][] adj) {
    auto d = new int[][](n + 1, n + 1);
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= n; j++) {
            d[i][j] = adj[i][j] == 0 ? int.max / 10 : adj[i][j];
        }
    }

    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (d[i][j] > d[i][k] + d[k][j]) {
                    d[i][j] = d[i][k] + d[k][j];
                }
            }
        }
    }
    return d;
}

int calc(int[] rs, int n, int[][] adj) {
    rs.sort;

    auto costs = warshallFloyd(n, adj);

    int ans = int.max;
    foreach (xs; rs.permutations) {
        int cost = 0;
        for (int i = 0; i < xs.length - 1; i++) {
            cost += costs[xs[i]][xs[i + 1]];
        }

        ans = min(ans, cost);
    }
    return ans;
}

void main() {
    auto nmr = readints;
    int n = nmr[0], m = nmr[1];
    auto rs = readints;

    auto adj = new int[][](n + 1, n + 1);
    for (int i = 0; i < m; i++) {
        auto abc = readints;
        int a = abc[0], b = abc[1], c = abc[2];
        adj[a][b] = adj[b][a] = c;
    }

    auto ans = calc(rs, n, adj);
    writeln(ans);
}
