#include <bits/stdc++.h>
#define fi first
#define se second
#define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define IT iterator
#define V vector
#define TP template <class o>
#define TPP template <typename t1, typename t2>
#define SZ(a) ((int)a.size())
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define REP(i, a, b) for (int i = b; i >= a; i--)
#define FOR(i,n) rep(i,1,n)
#define debug(x) cerr << #x << " = " << x << endl
using namespace std;
typedef unsigned ui;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
const int N = 110, size = 1 << 20, mod = 998244353, inf = 2e9;
const ll INF = 1e15;

// char buf[size],*p1=buf,*p2=buf;
TP void qr(o& x) {
    char c = gc;
    x = 0;
    int f = 1;
    while (!isdigit(c)) {
        if (c == '-')
            f = -1;
        c = gc;
    }
    while (isdigit(c))
        x = x * 10 + c - '0', c = gc;
    x *= f;
}
TP void qw(o x) {
    if (x / 10)
        qw(x / 10);
    putchar(x % 10 + '0');
}
TP void pr1(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar(' ');
}
TP void pr2(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar('\n');
}

// math
ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll power(ll a, ll b = mod - 2, ll p = mod) {
    ll c = 1;
    while (b) {
        if (b & 1)
            c = c * a % p;
        b /= 2;
        a = a * a % p;
    }
    return c;
}

TP void cmax(o& x, o y) {
    if (x < y)
        x = y;
}
TP void cmin(o& x, o y) {
    if (x > y)
        x = y;
}
TPP void ad(t1& x, t2 y) {
    x += y;
    if (x >= mod)
        x -= mod;
}
TPP void dl(t1& x, t2 y) {
    x -= y;
    if (x < 0)
        x += mod;
}

template<typename T>
struct BIT {
	T* c; int n; // require you to define 0 as the initial value !!
	BIT(int _n):n(_n){c=new T[n];c--; FOR(i,n) c[i]=T(0); }
	void add(int x,T y) { for( ;x<=n;x += x&-x) c[x]=c[x]+y; }
	T sum(int x) {T y=T(0); for( ;x;x &= x-1) y=y+c[x]; return y;}
};

// dbinom
ll jc[N], inv[N];
void jc_init(int n) {
    jc[0] = 1;
    for (int i = 1; i <= n; i++)
        jc[i] = jc[i - 1] * i % mod;
    inv[n] = power(jc[n]);
    for (int i = n; i; i--)
        inv[i - 1] = inv[i] * i % mod;
}
ll C(int x, int y) {
    if (x < y || y < 0)
        return 0;
    return jc[x] * inv[y] % mod * inv[x - y] % mod;
}

int n,k,bin[N]; //左端点 
char s[N],win[N][N];

char f(char a,char b) {
	if(a == b) return a;
	if(a == 'R') {
		if(b == 'S') return a;
		return b;
	}
	if(a == 'S') {
		if(b == 'R') return b;
		return a;
	}
	if(a == 'P') {
		if(b == 'S') return b;
		return a;
	}
	return 0;
}

void solve() {
	qr(n);qr(k);
	scanf("%s",s+1); bin[0]=1;
	FOR(i,n) win[0][i]=s[i];
	FOR(j,k) {
		bin[j]=bin[j-1]*2%n;
		FOR(i,n) win[j][i]=f(win[j-1][i],win[j-1][(i+bin[j-1]-1)%n+1]);
	}
	putchar(win[k][1]);
}

int main() {
    int T = 1;
//    qr(T);
    while (T--)
        solve();
    return 0;
}

// atcoder/arc121/B/main.cpp
// author: @___Johniel
// github: https://github.com/johniel/

#include <bits/stdc++.h>

#define each(i, c) for (auto& i : c)
#define unless(cond) if (!(cond))

using namespace std;

template<typename P, typename Q> ostream& operator << (ostream& os, pair<P, Q> p) { os << "(" << p.first << "," << p.second << ")"; return os; }
template<typename P, typename Q> istream& operator >> (istream& is, pair<P, Q>& p) { is >> p.first >> p.second; return is; }
template<typename T> ostream& operator << (ostream& os, vector<T> v) { os << "("; for (auto& i: v) os << i << ","; os << ")"; return os; }
template<typename T> istream& operator >> (istream& is, vector<T>& v) { for (auto& i: v) is >> i; return is; }
template<typename T> ostream& operator << (ostream& os, set<T> s) { os << "#{"; for (auto& i: s) os << i << ","; os << "}"; return os; }
template<typename K, typename V> ostream& operator << (ostream& os, map<K, V> m) { os << "{"; for (auto& i: m) os << i << ","; os << "}"; return os; }

template<typename T> inline T setmax(T& a, T b) { return a = std::max(a, b); }
template<typename T> inline T setmin(T& a, T b) { return a = std::min(a, b); }

using lli = long long int;
using ull = unsigned long long;
using point = complex<double>;
using str = string;
template<typename T> using vec = vector<T>;

constexpr array<int, 8> di({0, 1, -1, 0, 1, -1, 1, -1});
constexpr array<int, 8> dj({1, 0, 0, -1, 1, -1, -1, 1});
constexpr lli mod = 1e9 + 7;

int main(int argc, char *argv[])
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.setf(ios_base::fixed);
  cout.precision(15);

  int n;
  while (cin >> n) {
    n *= 2;
    vec<pair<lli, char>> v(n);
    cin >> v;

    vec<lli> r, g, b;
    each (i, v) {
      if (i.second == 'R') r.push_back(i.first);
      if (i.second == 'G') g.push_back(i.first);
      if (i.second == 'B') b.push_back(i.first);
    }
    sort(r.begin(), r.end());
    sort(g.begin(), g.end());
    sort(b.begin(), b.end());

    lli mn = 1LL << 60;

    if (r.size() % 2 == 0 && g.size() % 2 == 0 && b.size() % 2 == 0) {
      mn = 0;
    }
    if (0) ;
    else if (r.size() % 2 == 0) ;
    else if (g.size() % 2 == 0) swap(r, g);
    else if (b.size() % 2 == 0) swap(r, b);

    each (i, g) {
      auto itr = lower_bound(b.begin(), b.end(), i);
      if (itr != b.end()) setmin(mn, abs(i - *itr));
      if (itr != b.begin()) {
        --itr;
        setmin(mn, abs(i - *itr));
      }
    }

    lli x = 1LL << 60;
    lli y = 1LL << 60;
    each (i, r) {
      auto itr = lower_bound(b.begin(), b.end(), i);
      if (itr != b.end()) setmin(x, abs(i - *itr));
      if (itr != b.begin()) {
        --itr;
        setmin(x, abs(i - *itr));
      }
    }
    each (i, r) {
      auto itr = lower_bound(g.begin(), g.end(), i);
      if (itr != g.end()) setmin(y, abs(i - *itr));
      if (itr != g.begin()) {
        --itr;
        setmin(y, abs(i - *itr));
      }
    }
    setmin(mn, x + y);
    cout << mn << endl;
  }

  return 0;
}
