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

#ifdef LOCAL
#include "../debug.h"
#else
#define debug(x...) 141
#endif
#define forn(i,x,n) for(int i = x; i < n; ++i)
#define forit(it, m) for(auto it = m.begin(); it!=m.end(); ++it)
typedef long long ll;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll n,aa,ans=1e18;
    char cc;

    cin>>n;
    map<char,vector<ll>> m;
    vector<ll> a,b,c;
    forn(i,0,2*n)
        cin>>aa>>cc, m[cc].push_back(aa);
    if(m['R'].size()%2==0&&m['G'].size()%2==0&&m['B'].size()%2==0){
        cout<<0;
        return 0;
    }
    debug(m['R'],m['G'],m['B']);
    for(auto [_,x]:m)
        if(x.size()%2&&a.empty())
            a=x;
        else if(x.size()%2)
            b=x;
        else
            c=x;
    sort(a.begin(),a.end());
    sort(b.begin(),b.end());
    for(auto x:a){
        auto it=lower_bound(b.begin(),b.end(),x);
        if(it!=b.end())
            ans=min(ans,abs(x-*it));
        if(it!=b.begin())
            ans=min(ans,abs(x-*(--it)));
    }
    vector<pair<ll,ll>> ca(c.size(),{1e18,0}),cb(c.size(),{1e18,0});
    forn(i,0,c.size()){
        auto it=lower_bound(a.begin(),a.end(),c[i]);
        if(it!=a.end())
            ca[i].first=min(ca[i].first,abs(c[i]-*it));
        if(it!=a.begin())
            ca[i].first=min(ca[i].first,abs(c[i]-*(--it)));

        it=lower_bound(b.begin(),b.end(),c[i]);
        if(it!=b.end())
            cb[i].first=min(cb[i].first,abs(c[i]-*it));
        if(it!=b.begin())
            cb[i].first=min(cb[i].first,abs(c[i]-*(--it)));
        ca[i].second=cb[i].second=i;
    }
    sort(ca.begin(),ca.end());
    sort(cb.begin(),cb.end());
    if(c.size()&&ca[0].second==cb[0].second)
        ans=min(ans,min(ca[0].first+cb[1].first,ca[1].first+cb[0].first));
    else if(c.size())
        ans=min(ans,ca[0].first+cb[0].first);
    cout<<ans<<'\n';
    
    return 0;
}
#include <bits/stdc++.h>
#define ll long long

using namespace std;

ll k, n;
const ll mod = 1e9 + 7;

ll dp[200100][17], cdp[17][17];

ll nCr(ll n, ll r) 
{
    if (r == 0 || r == n)
        return 1;
    if(cdp[n][r] != -1)
        return cdp[n][r];
    cdp[n][r] = (nCr(n-1, r-1) + nCr(n-1, r)) % mod;
    return cdp[n][r];
}

ll pwr(ll a, ll b)
{
    if(b == 0)
        return 1;
    if(b & 1)
    {
        return pwr(a, b - 1) * a % mod;
    }
    else
    {
        ll ret = pwr(a, b / 2);
        return ret * ret % mod;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string str;
    cin >> str;
    cin >> k;
    n = str.size();
    memset(dp, 0, sizeof(dp));
    memset(cdp, -1, sizeof(cdp));

    for(int i = 0; i < 17; i++)
    {
        for(int j = 0; j <= i; j++)
        {
            cdp[i][j] = nCr(i, j);
        }
    }

    dp[0][0] = 1;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= i && j <= k; j++)
        {
            dp[i][j] = pwr(j, i);
            for(int z = 0; z < j; z++)
            {
                dp[i][j] = (dp[i][j] - cdp[j][z] * dp[i][z] % mod + mod) % mod;
            }
        }
    }

    ll ret = 0;
    for(int i = 1; i < n; i++)
    {
        ret += 15 * dp[i - 1][k - 1] * cdp[15][k - 1] % mod;
        ret %= mod;
        ret += 15 * dp[i - 1][k] * cdp[15][k - 1] % mod;
        ret %= mod;
    }

    unordered_map<char, int> mp;
    for(int i = 0; i < 10; i++)
        mp[i + '0'] = i;
    for(int i = 10; i < 16; i++)
        mp[i - 10 + 'A'] = i;
    unordered_set<int> used;
    for(int i = 0; i < n && used.size() <= k; i++)
    {
        int sz = used.size();
        // need (k - sz) new number for 
        int num = mp[str[i]];
        int start = 0;
        if(i == 0)
            start = 1;
        for(int j = start; j < num; j++)
        {
            int nsz = sz;
            if(!used.count(j))
                nsz++;
            int nk = k - nsz;
            if(nk < 0)
                continue;
            int nn = n - i - 1;
            for(int z = nk; z <= k; z++)
            {
                ret += dp[nn][z] * cdp[16 - nsz][nk] % mod * cdp[nsz][z - nk] % mod;
                ret %= mod;
            }
        }
        used.insert(num);
    }

    unordered_set<char> st;
    for(int i = 0; i < n; i++)
    {
        st.insert(str[i]);
    }
    if(st.size() == k)
        ret++;

    cout << ret << endl;

    return 0;
}


