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

const int di[] = {1, -1, 0, 0};
const int dj[] = {0, 0, 1, -1};

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	int h, w;
	cin >> h >> w;
	
	vector <string> g(h);
	vector < vector < pair <int, int> > > go(26);
	
	int si, sj, gi, gj;
	for(int i = 0; i < h; i++){
		cin >> g[i];
		for(int j = 0; j < w; j++){
			if(g[i][j] == 'S')si = i, sj = j;
			if(g[i][j] == 'G')gi = i, gj = j;
			if('a' <= g[i][j] && g[i][j] <= 'z'){
				go[g[i][j] - 'a'].push_back({i, j});
			}
		}
	}
		
	vector <int> d(h * w + 26, -1);
	vector <bool> vis(h * w + 26, false);
	deque <int> q;
	
	d[si * w + sj] = 0;
	q.push_back(si * w + sj);
	
	while(!q.empty()){
		int u = q.front();
		q.pop_front();
		
		if(vis[u])continue;
		vis[u] = true;
		
		if(u >= h * w){
			int c = u - h * w;
			for(auto [ni, nj] : go[c]){
				if(d[ni * w + nj] == -1 || d[ni * w + nj] > d[u] + 1){
					d[ni * w + nj] = d[u] + 1;
					q.push_back(ni * w + nj);
				}
			}
		}else{
			int i = u / w, j = u % w;
			
			if('a' <= g[i][j] && g[i][j] <= 'z'){
				if(d[h * w + g[i][j] - 'a'] == -1 || d[h * w + g[i][j] - 'a'] > d[u]){
					d[h * w + g[i][j] - 'a'] = d[u];
					q.push_front(h * w + g[i][j] - 'a');
				}
			}
			
			for(int dir = 0; dir < 4; dir++){
				int ni = i + di[dir], nj = j + dj[dir];
				if(ni < 0 || ni >= h || nj < 0 || nj >= w)continue;
				if(g[ni][nj] == '#')continue;
				
				if(d[ni * w + nj] == -1 || d[ni * w + nj] > d[u] + 1){
					d[ni * w + nj] = d[u] + 1;
					q.push_back(ni * w + nj);
				}
			}
		}
	}
	
	printf("%d\n", d[gi * w + gj]);
	
	
	return 0;
}

#include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define rrep(i, n) for (int i = (int)n-1; i >= 0; --i)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9+7;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

__attribute__ ((constructor))
void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);
}

int main(){
    int N, M;
    cin >> N >> M;

    vector<vector<int>> to(N);

    rep(i,M){
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        to[a].push_back(b);
        to[b].push_back(a);
    }



    int k;
    cin >> k;
    vector<int> c(k);
    rep(i, k) cin >> c[i];
    rep(i, k) c[i]--;

    // k個の頂点間の最短距離をBFSして求める．
    auto bfs = [&](int now_c) {
        vector<int> dist(N,inf);
        dist[now_c] = 0;
        queue<int> que;
        que.push(now_c);
        while(!que.empty()){
            int c_front = que.front();
            que.pop();
            for(auto u : to[c_front]){
                if(dist[u] != inf)
                    continue;
                chmin(dist[u], dist[c_front] + 1);
                que.push(u);
            }
        }
        return dist;
    };

    vector<vector<int>> dist_k(k, vector<int>(k, inf));
    rep(i,k){
        auto dist = bfs(c[i]);
        rep(j, k){
            if(dist[c[j]] != inf){
                chmin(dist_k[i][j],dist[c[j]]);
            }
        }
    }


    // bit DP

    int k2 = 1 << k;

    vector<vector<int>> dp(k2, vector<int>(k, inf));


    // 初期化
    rep(i,k){
        dp[1<<i][i] = 1; //1個の石のみ
    }

    rep(i,k2){
        rep(j, k){
            if(!(i>>j)&1)
                continue;
            rep(l, k){
                if(i>>k&1)
                    continue;
                chmin(dp[i | 1 << l][l], dp[i][j] + dist_k[j][l]);
            }
        }
    }

    int ans = inf;

    rep(i,k){
        chmin(ans,dp[k2-1][i]);
    }

    if(ans != inf)
        cout << ans << endl;
    else
        cout << -1 << endl;

    return 0;
}