//#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
//using namespace atcoder;
using ll = long long;
#define all(A) A.begin(),A.end()
using vll = vector<ll>;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
using Graph = vector<vector<ll>>;
Graph G;
vll dist;
vector<bool> seen;

void dfs(ll v) {
    seen[v] = true;

    for (auto nv : G[v]) {

        if (seen[nv])continue;

        dist[nv] = dist[v] + 1;
        dfs(nv);
    }
}
int main() {
    ll N, M;
    cin >> N >> M;
    G.resize(N);
    seen.assign(N, false);
    dist.assign(N, -1);
    rep(i, M) {
        ll A, B;
        cin >> A >> B;
        A--; B--;
        G[A].push_back(B);
        G[B].push_back(A);

    }
    ll K;
    cin >> K;
    vll C(K);
    rep(i, K) {
        cin >> C[i];
        C[i]--;
    }
    vector<vll> D(K, vll(K));
    rep(i, K) {
        seen.assign(N, false);
        dist.assign(N, 1e15);
        dist[C[i]] = 0;
        queue<ll> Q;
        Q.push(C[i]);
        while(!Q.empty()){
            ll p=Q.front();
            Q.pop();
            seen[p]=1;
            for(auto nv:G[p]){
                if (seen[nv])continue;
                if(dist[nv]<=dist[p]+1)continue;
                dist[nv] = dist[p] + 1;
                Q.push(nv);
            }
        }
        //dfs(C[i]);
        rep(j, K) {
            if (dist[C[j]] >1e14) {
                cout << -1 << endl;
                return 0;
            }

            D[i][j] = dist[C[j]];
        }
    }


    vector<vll> DP((ll(1) << K), vll(K, 1e18));
    rep(i, K) {
        DP[0][i] = 0;
    }

    rep(n, (ll(1) << K)) {
        if (n == 0)continue;
        rep(i, K) {
            if (!(n & (1 << i)))continue;
            if (n == (1 << i)) {
                DP[n][i] = 0;
                continue;
            }
            ll m = 1e18;
            rep(j, K) {
                if (i == j)continue;
                if ((n-(1<<i)) & (1<< j)) {
                    m = min(m, DP[n - (1<< i)][j] + D[j][i]);
                }
            }
            DP[n][i] = m;
        }
    }
    ll an = 1e18;
    rep(i, K) {
        an = min(an, DP[(1 << K)-1][i]);
    }
    cout << an+1 << endl;








}

//#include <atcoder/all>
#include <bits/stdc++.h>

using namespace std;


#define rep2(x,fr,to) for(int x=(fr);x<(to);x++)
#define rep(x,to) for(int x=0;x<(to);x++)
#define repr(x,fr,to) for(int x=(fr);x>=(to);x--)
#define all(c) c.begin(),c.end()
#define sz(v) (int)v.size()

typedef long long  ll; typedef vector<int> VI; typedef pair<int,int> pii; typedef vector<ll> VL; const int MD = 1e9 +7; const ll INF = 1e18;
void dbg(){cerr<<"\n";} template <class F,class ...S> void dbg(const F& f, const S&...s){cerr <<f <<": "; dbg(s...);}

//using mint = atcoder::modint998244353;



int main()
{
	cin.tie(0); ios_base::sync_with_stdio(false);
	int n;
	cin >>n;
	VL a(n);
	rep(i, n) cin >>a[i];
	
	int ans = 1<<30 | 1<<29;
	
	rep(i, 1<<(n-1)){
		int sm = 0, w =0;
		rep(j, n){
			w |= a[j];
			if(i>>j &1){
				sm ^= w;
				w = 0;
			}
		}
		sm ^= w;
		ans = min(ans, sm);
	}
	
	cout <<ans <<"\n";
	return 0;	
}
