#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ll long long
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define fr first
#define sc second
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for(int i = a; i < b; i++)
#define int long long

const int N = 1e5 + 20;
const int K = 18;
const int INF = 1e9;
const int MOD = 1e9 + 7;

vector <int> adj[N];
int dist[N][K], n, k, dp[1 << K][K], m, arr[K];

void bfs(int idx) {
	rep(node, 1, n + 1) dist[node][idx] = INF;
	queue <int> q;
	q.push(arr[idx]);
	dist[arr[idx]][idx] = 0;
	while(!q.empty()) {
		int node = q.front();
		q.pop();
		for(auto &child: adj[node]) {
			if(dist[child][idx] == INF) {
				dist[child][idx] = dist[node][idx] + 1;
				q.push(child);
			}
		}
	}
}

int calc(int last, int mask) {
	if(mask == ((1 << k) - 1))
		return 0;
	if(dp[mask][last] != -1)
		return dp[mask][last];
	int ans = INF;
	for(int i = 0; i < k; i++) {
		if(mask & (1 << i)) continue;
		ans = min(ans, calc(i, mask | (1 << i)) + dist[arr[last]][i]);
	}
	return dp[mask][last] = ans;
}

void solve() {
	cin >> n >> m;
	rep(i, 0, m) {
		int x, y;
		cin >> x >> y;
		adj[x].pb(y);
		adj[y].pb(x);
	}
	cin >> k;
	rep(i, 0, k) cin >> arr[i];
	rep(i, 0, k) bfs(i);
	rep(i, 0, k) {
		if(dist[arr[0]][i] == INF) {
			cout << "-1\n";
			return;
		}
	}
	memset(dp, -1, sizeof dp);
	int ans = INF;
	rep(i, 0, k) ans = min(ans, 1 + calc(i, (1 << i)));
	cout << ans << "\n";
}	

signed main() {
  	ios_base::sync_with_stdio(false);
  	cin.tie(NULL); cout.tie(NULL);
  	int t = 1;
  	// cin >> t;
  	while(t--) 
    	solve();
  	return 0;
}
 
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define SORT(a) sort(a.begin(),a.end())
#define rSORT(a) sort(a.rbegin(),a.rend())
#define fSORT(a,f) sort(a.begin(),a.end(),f)
#define all(a) a.begin(),a.end()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#define tp(a,i) get<i>(a)
#ifdef _DEBUG
#define line cout << "-----------------------------\n"
#define stop system("pause")
#endif
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); }
inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; }
template<class T>inline T sum(const vector<T>& a, int l, int r) { return a[r] - (l == 0 ? 0 : a[l - 1]); }
template<class T>inline void END(T s) { print(s); exit(0); }
void END() { exit(0); }

int main() {
    init();

    int n, m; cin >> n >> m;
    vector<vector<pair<int, int>>> g(n);
    VI c(m);
    FOR(i, 0, m) {
        int a, b; cin >> a >> b >> c[i];
        --a, --b, --c[i];
        g[a].emplace_back(b, i);
        g[b].emplace_back(a, i);
    }
    
    VI ans(n, -1);
    ans[0] = 0;
    vector<bool> dp(n);
    dp[0] = true;
    queue<int> bfs;
    bfs.push(0);
    while (!bfs.empty()) {
        int cur = bfs.front();
        bfs.pop();
        for (auto [to, i] : g[cur]) {
            if (dp[to])continue;
            dp[to] = true;
            if (ans[cur] == c[i]) {
                if (c[i] == 0)ans[to] = 1;
                else ans[to] = 0;
            }
            else ans[to] = c[i];
            bfs.push(to);
        }
    }

    FOR(i, 0, n)print(++ans[i]);

    return 0;
}