#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))
#define SZ(x) int(x.size())
#define ok cerr<<"ok"<<endl
#define db(x) cerr<<(#x)<<'='<<(x)<<endl
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
//1.integer overflow (1e5 * 1e5) (2e9 + 2e9)
//2.runtime error
//3.boundary condition
const int N=(int)1e5+100;
int n,m,col[N];
vector< pair<int,int> > G[N];
void dfs(int now){
    for(pair<int,int> pnow:G[now]){
        if(col[pnow.fi]>0) continue;
        if(col[now]==pnow.se){
            col[pnow.fi]=pnow.se%n+1;
        } else col[pnow.fi]=pnow.se;
        dfs(pnow.fi);
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("input.txt", "r", stdin);
#endif
    ms(col,0);
    cin>>n>>m;
    forn(i, m){
        int u,v,c;
        cin>>u>>v>>c;
        G[u].eb(mp(v,c));
        G[v].eb(mp(u,c));
    }
    col[1]=1;
    dfs(1);
    for1(i, n) cout<<col[i]<<'\n';
#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}

#include <bits/stdc++.h>
#define s second
#define f first
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define read(x) for(auto& qw : (x)) cin >> qw;
#define print(x) for(auto& qw : (x)) cout << qw << " "; cout << endl;
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MAX = 1e5 + 10;

vector<int> g[MAX];
int N, M, K;
vector<int> A;
vector<vector<int>> D(18,vector<int>(MAX,0));

void bfs(int s, vector<int> &d){
	fill(all(d), INF);
	queue<int> Q;
	Q.push(s);
	d[s] = 0;

	while(Q.size()){
		int v = Q.front();
		Q.pop();

		for(int u : g[v]){
			if(d[u] != INF) continue;
			d[u] = d[v] + 1;
			Q.push(u);
		}
	}
}

ll DP[(1<<18)][20];

ll solve(int mask, int last){
	if(DP[mask][last] + 1)
		return DP[mask][last];

	if(mask == (1<<K) - 1)
		return 0;

	ll ans = LINF;

	for(int i = 0; i < K; i++){
		if(mask >> i & 1) continue;

		int n_mask = mask | (1 << i);

		ans = min(ans, (ll)D[last][A[i]] + solve(n_mask, i));
	}
	

	return DP[mask][last] = ans;
}

void test_case(){
	cin >> N >> M;

	while(M--){
		int a,b;
		cin >> a >> b;
		a--;b--;

		g[a].pb(b);
		g[b].pb(a);
	}

	cin >> K;
	A.resize(K);
	for(int& x : A){
		cin >> x;
		x--;
	}

	for(int i = 0; i < K; i++)
		bfs(A[i],D[i]);

	memset(DP, -1, sizeof DP);

	ll ans = LINF;
	for(int i = 0; i < K; i++)
		ans = min(ans, 1 + solve(1<<i, i));

	cout << (ans >= INF ? -1 : ans) << endl;
}



int main(){ _

	int tt = 1;
//	cin >> tt;

	while(tt--){
		test_case();
	}

	return 0;
}
