#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define eb emplace_back
using namespace std;
template <class T = int>
using V = vector<T>;
template <class T = int>
using VV = V<V<T>>;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pdd = pair<ld,ld>;

const int INF = 1e9;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    VV<int> g(n);
    rep(i,m){
        int a, b;
        cin >> a >> b;
        a--, b--;
        g[a].eb(b);
        g[b].eb(a);
    }
    int k;
    cin >> k;
    V<int> c(k);
    rep(i,k) cin >> c[i], c[i]--;
    VV<int> dist(k, V<int>(n, -1));
    rep(i,k){
        queue<int> que;
        dist[i][c[i]] = 0;
        que.push(c[i]);
        while(!que.empty()){
            int v = que.front();
            que.pop();
            for(int nv : g[v]){
                if(dist[i][nv] != -1) continue;
                dist[i][nv] = dist[i][v] + 1;
                que.push(nv);
            }
        }
    }
    rep(i,k) if(dist[0][c[i]] == -1){
        cout << -1 << endl;
        return 0;
    }
    VV<int> dp(1<<k, V<int>(k, INF));
    rep(i,k) dp[0][i] = 0;
    rep(i,(1<<k)-1){
        rep(j,k){
            rep(l,k){
                if(!(i>>l&1)){
                    dp[i|(1<<l)][l] = min(dp[i|(1<<l)][l], dp[i][j] + dist[j][c[l]]);
                }
            }
        }
    }
    int ans = INF;
    rep(i,k) ans = min(ans, dp[(1<<k)-1][i]+1);
    cout << ans << endl;
}
        #include<bits/stdc++.h>
#define loop(i, n) for(int i = 0; i < n; i++)
#define pb          push_back
#define ll          long long int
#define vi          vector<int>
#define pi          pair<int, int>
#define W(t)        int t; cin >> t; while(t--)
#define FIO         ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff          first
#define ss          second
#define rep(i, a, b)for(ll i = a; i <= b; i++)
#define dep(i, a, b)for(ll i = a; i >= b; i--) 
#define all(x)      x.begin(), x.end()
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI                3.14159265
using namespace std;
const ll mod = (ll)(998244353);
ll test = 1;
vector<ll> read(int n){
    
    vector<ll> v(n);

    loop(i, n)
        cin >> v[i];
    return v;
}
std::vector<ll> v;
ll OR(ll i, ll j){
    ll left = v[i];


    if(i+1 <= j-1){
        rep(k, i+1, j-1){
        
            left = left|v[k];
        }
    }
   

    return left;
}
ll calculate(std::vector<ll> cnt){

    std::vector<ll> k;

    rep(i, 0, cnt.size()-1){
        if(cnt[i] == 1)k.pb(i);
    }
    ll ans = OR(k[0], k[1]);
    rep(i, 1, k.size()-2){

        

        ans ^= OR(k[i], k[i+1]);
    }
    
    return ans;
}
void solve(){
    ll i, j, k, m,n, cnt = 0, ans = 0, sum = 0;

    cin >> n;

    v.resize(n);    

    for(auto &it : v)cin >> it;


    ans = INT_MAX;
    for(ll i = 0; i < 1LL<<(n-1); i++){


        ll t = i;

        std::vector<ll> cnt(n+1, 0);

        cnt[n] = 1;

        cnt[0] = 1;

        rep(j, 0, n-2){
            if(t&1LL){
                cnt[j+1] = 1;
            }
            else{
                cnt[j+1] = 0;
            }
            t/=2;
        }
        ans = min(ans, calculate(cnt));
    }

    cout <<ans << "\n";


    test++;
}

int main(){
    FIO
    
    #ifndef ONLINE_JUDGE
    freopen("C:/Codeforces/input.txt", "r", stdin);
    freopen("C:/Codeforces/output.txt", "w", stdout);
    #endif
    ll i = 1;
    //W(t)
    solve();
    
    return 0;
    
}