#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;

void Main() {
  ll n;
  R n;
  n*=2;
  vector<ll> a[3];
  rep(i,n) {
    ll x;
    char c;
    cin >> x >> c;
    if(c=='R') a[0].pb(x);
    else if(c=='B') a[1].pb(x);
    else a[2].pb(x);
  }
  rep(i,3) sort(all(a[i]));
  P c[3];
  rep(i,3) c[i]=P(a[i].size()%2,i);
  sort(c,c+3,greater<P>());
  ll ans=MAXL;
  if(c[0].F) {
    ll x=c[0].S,y=c[1].S,z=c[2].S;
    rep(i,a[x].size()) {
      ll k=lower_bound(all(a[y]),a[x][i])-a[y].begin();
      if(k<a[y].size()) ans=min(ans,abs(a[x][i]-a[y][k]));
      if(k) ans=min(ans,abs(a[x][i]-a[y][k-1]));
    }
    vector<P> v[2];
    rep(i,a[z].size()) {
      ll k=lower_bound(all(a[x]),a[z][i])-a[x].begin();
      if(k<a[x].size()) {
        ll d=abs(a[z][i]-a[x][k]);
        v[0].pb(P(d,i));
      }
      if(k) {
        ll d=abs(a[z][i]-a[x][k-1]);
        v[0].pb(P(d,i));
      }
    }
    rep(i,a[z].size()) {
      ll k=lower_bound(all(a[y]),a[z][i])-a[y].begin();
      if(k<a[y].size()) {
        ll d=abs(a[z][i]-a[y][k]);
        v[1].pb(P(d,i));
      }
      if(k) {
        ll d=abs(a[z][i]-a[y][k-1]);
        v[1].pb(P(d,i));
      }
    }
    rep(i,2) sort(all(v[i]));
    rep(i,min(3,(int)v[0].size())) {
      rep(j,min(3,(int)v[1].size())) {
        if(v[0][i].S!=v[1][j].S) ans=min(ans,v[0][i].F+v[1][i].F);
      }
    }
  } else ans=0;
  pr(ans);
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();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;
    ll k;

    cin >> n >> k;

    vector<vector<ll>> t(n, vector<ll>(n, 0));

    rep(i, n) rep(j, n) cin >> t[i][j];

    vector<int> v(n -1);

    rep(i, n-1) v[i] = i + 1;

    ll cnt = 0;


    //N!の順列の全探索
    do{
        ll now = 0;
        FOR(i, 0, n-1){
            if(i == 0){
                now += t[0][v[i]];
            }
            if(i == n - 2){
                now += t[v[i]][0];
            }
            if(i != 0){
                now += t[v[i - 1]][v[i]];
            }
        }
        if(now == k)
            cnt++;
    }while(next_permutation(v.begin(),v.end()));

    cout << cnt << endl;

    return 0;
}