#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

static const LL INF = 0x1fffffffffffffffLL;

template<typename T>
struct DP3
{
	int N1, N2, N3;
	vector<T> data;
	DP3(int N1, int N2, int N3, const T& t = T())
		: N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size()*sizeof(T)<(1<<26)); }
	T& operator()(int i1, int i2, int i3)
		{ return data[ ((i1*N2)+i2)*N3+i3 ]; }
	void swap(DP3& rhs)
		{ data.swap(rhs.data); }
};

class TimeTravellingTour { public:
	long long determineCost(int N, vector <int> cities, vector <string> roads) 
	{
		vector< vector<LL> > G(N, vector<LL>(N,INF));
		{
			string str = accumulate(roads.begin(), roads.end(), string(""));
			for(int i=0; i<str.size(); ++i)
				if( str[i] == ',' )
					str[i] = ' ';
			stringstream sin(str);
			for(int a,b,cost; sin>>a>>b>>cost; )
				G[a][b] = G[b][a] = cost;
		}
		for(int i=0; i<N; ++i)
			G[i][i] = 0;
		for(int k=0; k<N; ++k)
			for(int i=0; i<N; ++i)
				for(int j=0; j<N; ++j)
					G[i][j] = min(G[i][j], G[i][k]+G[k][j]);

		DP3<LL> memo(N, cities.size()+1, cities.size()+1, -1);
		LL a = rec( 0, 0, cities.size(), cities, G, memo );
		return a>=INF ? -1 : a;
	}

	LL rec(int cur, int s, int e, const vector<int>& C, const vector< vector<LL> >& G, DP3<LL>& memo )
	{
		if( s+1 == e )
			return G[cur][C[s]];
		if( memo(cur,s,e) >= 0 )
			return memo(cur,s,e);

		LL best = INF;
		for(int v=0; v<G[cur].size(); ++v)
			if( G[cur][v] < INF )
				for(int m=s+1; m<e; ++m)
					best = min(best, G[cur][v]+rec(v,s,m,C,G,memo)+rec(v,m,e,C,G,memo));
		return memo(cur,s,e) = best;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const long long& Expected, const long long& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, TimeTravellingTour().determineCost(N, cities, roads));}
int main(){

CASE(0)
	int N = 5; 
	int cities_[] = {2,3,2,4};
	  vector <int> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities_)); 
	string roads_[] = {"0,2,4 0,1,2 2,1,2 1,3,3 4,0,4"};
	  vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 
	long long _ = 13LL; 
END
CASE(1)
	int N = 3; 
	int cities_[] = {1,0,1};
	  vector <int> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities_)); 
	string roads_[] = {"0,2,1"," 2",",1,5"};
	  vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 
	long long _ = 12LL; 
END
CASE(2)
	int N = 3; 
	int cities_[] = {2};
	  vector <int> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities_)); 
	string roads_[] = {"0,1,2"};
	  vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 
	long long _ = -1LL; 
END
CASE(3)
	int N = 6; 
	int cities_[] = {4, 1, 3, 2};
	  vector <int> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities_)); 
	string roads_[] = {"0,1,5 0,2,5 0,5,2 2,3,5 2,4,2 3,4,4 3,5,1"};
	  vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 
	long long _ = 19LL; 
END
/*
CASE(4)
	int N = ; 
	int cities_[] = ;
	  vector <int> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities_)); 
	string roads_[] = ;
	  vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 
	long long _ = LL; 
END
CASE(5)
	int N = ; 
	int cities_[] = ;
	  vector <int> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities_)); 
	string roads_[] = ;
	  vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 
	long long _ = LL; 
END
*/
}
// END CUT HERE
