#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;

const int inf = 1e9;

int n;
int x[20], y[20], z[20];
int dist[20][20];
int dp[1 << 20][20];

int rec(int bit, int v) {
	if (dp[bit][v] != -1) return dp[bit][v];
	if (bit == (1 << v)) {
		return dp[bit][v] = dist[0][v];
	}
	int prev = bit & ~(1 << v);
	int ret = inf;
	rep(pv, n) {
		if (!(prev & (1 << pv))) continue;
		ret = min(ret, rec(prev, pv) + dist[pv][v]);
	}
	return dp[bit][v] = ret;
}

int main() {
	cin >> n;
	rep(i, n) cin >> x[i] >> y[i] >> z[i];
	for (int bit = 0; bit < (1 << n); bit++) rep(v, n) dp[bit][v] = -1;
	rep(i, n) rep(j, n) {
		dist[i][j] = abs(x[j] - x[i]) + abs(y[j] - y[i]) + max(0, z[j] - z[i]);
	}
	cout << rec((1 << n) - 1, 0) << endl;
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(ll i=0;i<(n);i++)
const ll INF = 1LL<<60;



int main(){
  ll n,k;
  cin>>n>>k;
  string s;
  cin>>s;
  
  map<char,ll> rcp;
  rcp['R']=1;
  rcp['S']=2;
  rcp['P']=3;
  
  string ns=s;
  ll knum=300;
  k=k+1;
while(k!=1){
  k--;
  s.clear();
  s=ns;
  ns.clear();
  s=s+s;
  if(k<10)knum=pow(2,k);
  rep(i,min((ll)s.size(),knum)/2){
     if(rcp[s[2*i]]-rcp[s[2*i+1]]==0)ns+=s[2*i];
     if((rcp[s[2*i]]-rcp[s[2*i+1]]+3)%3==2)ns+=s[2*i];
     if((rcp[s[2*i]]-rcp[s[2*i+1]]+3)%3==1)ns+=s[2*i+1];
  }
  if((ll)ns.size()==1)break;
}
  

  cout<<ns[0]<<endl;
}
