using System;
using System.Collections.Generic;
using System.Linq;

//ABC-073-D ワーシャルフロイドを使う問題
class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("3 3 3");
            WillReturn.Add("1 2 3");
            WillReturn.Add("1 2 1");
            WillReturn.Add("2 3 1");
            WillReturn.Add("3 1 4");
            //2
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 3 2");
            WillReturn.Add("1 3");
            WillReturn.Add("2 3 2");
            WillReturn.Add("1 3 6");
            WillReturn.Add("1 2 2");
            //4
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("4 6 3");
            WillReturn.Add("2 3 4");
            WillReturn.Add("1 2 4");
            WillReturn.Add("2 3 3");
            WillReturn.Add("4 3 1");
            WillReturn.Add("1 4 1");
            WillReturn.Add("4 2 2");
            WillReturn.Add("3 1 6");
            //3
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct EdgeInfoDef
    {
        internal int A;
        internal int B;
        internal int C;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(X => int.Parse(X)).ToArray();

        SplitAct(InputList[0]);
        int N = wkArr[0];

        int[] RArr = InputList[1].Split(' ').Select(X => int.Parse(X)).ToArray();

        var EdgeList = new List<EdgeInfoDef>();
        foreach (string EachStr in InputList.Skip(2)) {
            SplitAct(EachStr);
            EdgeInfoDef WillAdd;
            WillAdd.A = wkArr[0];
            WillAdd.B = wkArr[1];
            WillAdd.C = wkArr[2];
            EdgeList.Add(WillAdd);
        }

        //ワーシャルフロイドで、ノードの組み合わせごとの最短距離を求める
        int[,] CostArr = new int[N + 1, N + 1];

        for (int I = 1; I <= N; I++) {
            for (int J = 1; J <= N; J++) {
                if (I == J) CostArr[I, J] = 0;
                else CostArr[I, J] = int.MaxValue / 2;
            }
        }

        foreach (EdgeInfoDef EachEdge in EdgeList) {
            CostArr[EachEdge.A, EachEdge.B] = EachEdge.C;
            CostArr[EachEdge.B, EachEdge.A] = EachEdge.C;
        }

        for (int K = 1; K <= N; K++) {
            for (int I = 1; I <= N; I++) {
                for (int J = 1; J <= N; J++) {
                    int Cost1 = CostArr[I, J];
                    int Cost2 = CostArr[I, K];
                    int Cost3 = CostArr[K, J];

                    if (Cost1 > Cost2 + Cost3)
                        CostArr[I, J] = Cost2 + Cost3;
                }
            }
        }

        List<int[]> RArrList = ExecDFS(RArr);
        int AnswerCost = int.MaxValue;

        foreach (int[] EachRArr in RArrList) {
            int CurrCost = 0;
            for (int I = 1; I <= EachRArr.GetUpperBound(0); I++) {
                int FromInd = EachRArr[I - 1];
                int ToInd = EachRArr[I];
                CurrCost += CostArr[FromInd, ToInd];
            }
            if (AnswerCost > CurrCost) {
                AnswerCost = CurrCost;
            }
        }
        Console.WriteLine(AnswerCost);
    }

    struct JyoutaiDef
    {
        internal List<int> CurrList;
    }

    //深さ優先探索でRArrの順列のListを返す
    static List<int[]> ExecDFS(int[] pRArr)
    {
        var WillReturn = new List<int[]>();

        int UB = pRArr.GetUpperBound(0);

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrList = new List<int>();
        Stk.Push(WillPush);

        while (Stk.Count > 0) {
            JyoutaiDef Popped = Stk.Pop();

            if (Popped.CurrList.Count - 1 == UB) {
                WillReturn.Add(Popped.CurrList.ToArray());
                continue;
            }

            for (int I = 0; I <= UB; I++) {
                if (Popped.CurrList.Contains(pRArr[I]))
                    continue;

                WillPush.CurrList = new List<int>();
                WillPush.CurrList.AddRange(Popped.CurrList);
                WillPush.CurrList.Add(pRArr[I]);
                Stk.Push(WillPush);
            }
        }
        return WillReturn;
    }
}


Measured memory_bytes: 23876