#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#define miu 100007
using namespace std;
template<class T>inline T mmin(T x, T y) {
    return x > y ? y : x;
}
int n;
vector<long long> G[3];
char buf[7];
int flag[3], tp = 0;
const long long inf = 0x3f3f3f3f3f3f3f3f;
long long ans = inf, lm = inf, rm = inf;
signed main() {
    scanf("%d", &n);
    for(int i = 1; i <= 2 * n; ++i) {
        long long tmp;
        scanf("%lld", &tmp);
        scanf("%s", buf + 1);
        if(buf[1] == 'R')G[0].push_back(tmp);
        if(buf[1] == 'G')G[1].push_back(tmp);
        if(buf[1] == 'B')G[2].push_back(tmp);
    }
    for(int i = 0; i < 3; ++i) {
        if(G[i].size() % 2 == 1) {flag[tp] = i; ++tp;}
    }
    if(tp == 0)return printf("0\n"), 0;
    for(int i = 0; i < 3; ++i) if(G[i].size() % 2 == 0) {flag[tp] = i; ++tp;}
    sort(G[flag[0]].begin(), G[flag[0]].end());
    sort(G[flag[2]].begin(), G[flag[2]].end());
    //printf("%d\n", G[flag[0]].size());
    //for(vector<long long> :: iterator it = G[flag[0]].begin(); it != G[flag[0]].end(); ++it);
    for(vector<long long> :: iterator it = G[flag[1]].begin(); it != G[flag[1]].end(); ++it) {
        vector<long long> :: iterator r = lower_bound(G[flag[0]].begin(), G[flag[0]].end(), *it);
        //printf("%lld %lld %d %d\n", *r, *it, r - G[flag[0]].begin(), it - G[flag[1]].begin());
        if(r != G[flag[0]].end())ans = mmin(ans, (*r) - (*it));
        if(r != G[flag[0]].begin())ans = mmin(ans, (*it) - (*(r - 1)));
    }
    for(vector<long long> :: iterator it = G[flag[1]].begin(); it != G[flag[1]].end(); ++it) {
        vector<long long> :: iterator r = lower_bound(G[flag[2]].begin(), G[flag[2]].end(), *it);
        //printf("%lld %lld %d %d\n", *r, *it, r - G[flag[0]].begin(), it - G[flag[1]].begin());
        if(r != G[flag[2]].end())rm = mmin(rm, (*r) - (*it));
        if(r != G[flag[2]].begin())rm = mmin(rm, (*it) - (*(r - 1)));
    }
    for(vector<long long> :: iterator it = G[flag[0]].begin(); it != G[flag[0]].end(); ++it) {
        vector<long long> :: iterator r = lower_bound(G[flag[2]].begin(), G[flag[2]].end(), *it);
        //printf("%lld %lld %d %d\n", *r, *it, r - G[flag[0]].begin(), it - G[flag[1]].begin());
        if(r != G[flag[2]].end())lm = mmin(lm, (*r) - (*it));
        if(r != G[flag[2]].begin())lm = mmin(lm, (*it) - (*(r - 1)));
    }
    printf("%lld\n", mmin(ans, lm + rm));
    return 0;
}

//{{{
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using VLL = vector<LL>;
using vi = vector<int>;
using pii = pair<int, int>;
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset(a, b, sizeof(a))
#ifdef LOCAL
#include "prettyprint.hpp"
#endif
// clang-format off
void _print() { cerr << "]\033[0m\n"; }
template <typename T> void __print(T x) { cerr << x; }
template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); }
#ifdef LOCAL
#define debug(x...) cerr << "\033[1;34m[" << #x << "] = \033[1;32m["; _print(x)
#define debug_arr(x...) cerr << "\033[1;34m[" << #x << "] = \033[1;32m" << (x) << "\033[0m\n"
#else
#define debug(x...)
#define debug_arr(x...)
#endif
// clang-format on
//}}}

const int N = 2e5 + 10;
int head[N], e;
struct E
{
    int v, next;
} edge[N << 1];

void addedge(int u, int v)
{
    edge[e].v = v, edge[e].next = head[u];
    head[u] = e++;
}

// clang-format off
const int NV = 2e5 + 10;
const LL inf = 0x3f3f3f3f3f3f3f3fll;
LL dis[NV];
bool vis[NV];
queue<int> q;

void bfs(int st) {
    clr(dis, 0x3f), clr(vis, false);
    dis[st] = 0;
    while (!q.empty()) q.pop();
    q.push(st);

    while (!q.empty()) {
        int u = q.front();
        q.pop();

        if (vis[u]) continue;
        vis[u] = true;
        for (int i = head[u]; ~i; i = edge[i].next) {
            int v = edge[i].v;
            if(dis[v] > dis[u] + 1)
            {
                dis[v] = dis[u] + 1;
                q.push(v);
            }
        }
    }
}  // clang-format on

int n, m;
int k;
int c[22];

LL g[22][22];
LL dp[1 << 17][18];

LL dfs(int st, int cur)
{
    assert((1 << cur) & st);
    if (dp[st][cur] != -1) return dp[st][cur];

    LL ret = inf;
    for (int p = 0; p < k; p++)
    {
        if (p == cur) continue;
        if (((1 << p) & st) == 0) continue;
        if (g[p][cur] == inf) continue;

        assert((1 << p) & st);
        int pre_st = st ^ (1 << cur);

        LL predis = dfs(pre_st, p);
        if (predis == inf) continue;
        LL val = predis + g[p][cur];
        ret = min(ret, val);
    }
    return dp[st][cur] = ret;
}

int main()
{
#ifdef LOCAL
    freopen("in", "r", stdin);
    // freopen("out", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);

    while (cin >> n >> m)
    {
        clr(head, -1), e = 0;
        for (int i = 1; i <= m; ++i)
        {
            int u, v;
            cin >> u >> v;
            addedge(u, v);
            addedge(v, u);
        }

        cin >> k;
        assert(k >= 1);
        assert(k <= 17);
        for (int i = 0; i < k; i++) cin >> c[i];

        clr(g, 0x3f);
        for (int i = 0; i < k; i++)
        {
            int st = c[i];
            bfs(st);

            for (int j = 0; j < k; j++)
            {
                int ed = c[j];
                g[i][j] = dis[ed];
            }
        }

        // for (int i = 0; i < k; i++)
        //{
        // for (int j = 0; j < k; j++)
        //{
        // debug(i, j, g[i][j]);
        //}
        //}

        clr(dp, -1);
        for (int i = 0; i < k; i++) dp[1 << i][i] = 1;

        LL ans = inf;
        int lim = 1 << k;

        for (int i = 0; i < k; i++)
        {
            LL v = dfs(lim - 1, i);
            ans = min(ans, v);
        }

        if (ans >= inf) ans = -1;
        cout << ans << endl;
    }

    return 0;
}