#include <cstdio>

#define rep( i, a, b ) for( int (i) = (a) ; (i) <= (b) ; (i) ++ )
#define per( i, a, b ) for( int (i) = (a) ; (i) >= (b) ; (i) -- )

const int MAXN = 105;

template<typename _T>
void read( _T &x )
{
	x = 0;char s = getchar();int f = 1;
	while( s > '9' || s < '0' ){if( s == '-' ) f = -1; s = getchar();}
	while( s >= '0' && s <= '9' ){x = ( x << 3 ) + ( x << 1 ) + ( s - '0' ), s = getchar();}
	x *= f;
}

template<typename _T>
void write( _T x )
{
	if( x < 0 ){ putchar( '-' ); x = ( ~ x ) + 1; }
	if( 9 < x ){ write( x / 10 ); }
	putchar( x % 10 + '0' );
}

char typ[MAXN][MAXN];
char in[MAXN];
int N, K;

char Compete( const char a, const char b )
{
	if( a == b ) return a;
	if( a == 'P' ) return b == 'R' ? a : b;
	if( a == 'S' ) return b == 'P' ? a : b;
	if( a == 'R' ) return b == 'S' ? a : b;
}

int main()
{
	read( N ), read( K );
	scanf( "%s", in + 1 );
	for( int i = 1 ; i <= N ; i ++ ) typ[i][0] = in[i];
	int stp = 1;
	for( int j = 1 ; j <= K ; j ++, stp = stp * 2 % N )
		for( int i = 1 ; i <= N ; i ++ )
			typ[i][j] = Compete( typ[i][j - 1], typ[( i + stp - 1 ) % N + 1][j - 1] );
	putchar( typ[1][K] ), putchar( '\n' );
	return 0;
}
/*
	JAI JAGANNATH!
*/
//@Author : zanj0

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

#define int 			long long int

#define ff              first
#define ss              second
#define pb              push_back
#define MOD             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define w(x)            int x; cin>>x; while(x--)
#define endl       		"\n"
#define timetaken cerr<<"Time : "<<1000*(long double)clock()/(long double)CLOCKS_PER_SEC<<"ms\n"

void zanj0()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
const int N = 1e5 + 5;
vector <vector <int> > graph[N];
int n;
map <int, set <int> > divs;

int dijkstra(int src) {
	priority_queue < pair <int, int>, vector < pair <int, int> >, greater < pair <int, int> > > pq;
	pq.push({0, src});
	vector <int> dist(n + 1, inf);
	while (!pq.empty()) {
		pair <int, int> node = pq.top();
		pq.pop();
		if (node.ff >= dist[node.ss]) continue;
		dist[node.ss] = node.ff;
		for (auto& it : graph[node.ss]) {
			int nxt = it[0];
			for (auto &it2 : divs[it.back()]) {
				int val = it2 + 1;
				int extra = it2 - dist[node.ss];
				int ndist = it[1] + (it.back() / val) + extra + dist[node.ss];
				if (extra < 0) {
					ndist = it[1] + (it.back() / (dist[node.ss] + 1)) + dist[node.ss];
				}
				if (ndist < dist[nxt]) {
					pq.push({ndist, nxt});
				}
			}
		}
	}
	return dist.back();
}
set <int> getDivs(int x) {
	set <int> ret;
	ret.insert(sqrt(x));
	ret.insert(sqrt(x) + 1);
	if (sqrt(x) > 0) ret.insert((sqrt(x) - 1));
	return ret;
}
void solve() {
	cin >> n;
	int m;
	cin >> m;
	for (int i = 0; i < m; i++) {
		int u, v, x, y;
		cin >> u  >> v >> x >> y;
		graph[u].pb({v, x, y});
		graph[v].pb({u, x, y});
		if (!divs.count(y)) divs[y] = getDivs(y);
	}
	int ret = dijkstra(1);
	if (ret == inf) ret = -1;
	cout << ret << endl;
}
int32_t main()
{
	zanj0();
	solve();
	timetaken;
	return 0;
}