//高知能系Vtuberの高井茅乃です。
//Twitter: https://twitter.com/takaichino
//YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw

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

#define INF INT_MAX
#define LLINF LLONG_MAX
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define MODA 1000000007
#define MODB  998244353 

template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& vec) {
    for (T& x: vec) { is >> x; }
    return is;
}

ll dp[18][1 << 18] = {};

int main() {
	ll ans = 0;
	ll tmp = 0;
	int n; cin >> n;
	ll x[n+1], y[n+1], z[n+1];
	REP(i, n) cin >> x[i] >> y[i] >> z[i];
	x[n] = x[0]; y[n] = y[0]; z[n] = z[0];
	int full = 0; REP(i, n) full += 1 << i;
	REP(bit, 1 << (n+1)){ 
		REP(j, n+1){
			dp[j][bit] = INF;
		}
	}
	dp[0][0] = 0;
	REP1(i, n-1){
		dp[i][1 << (i-1)] = 
			dp[0][0] + abs(x[0] - x[i]) + abs(y[0] - y[i]) + max((ll)0, z[i] - z[0])
		;
		//cout << dp[0][0] + abs(x[0] - x[i]) + abs(y[0] - y[i]) + max((ll)0, z[i] - z[0]) << endl;
	}
	//cout << dp[1][1] << endl;
	REP(bit, 1 << (n+1)){
		REP(j, n+1){
			if(!(bit & (1<<(j-1))) ) continue;
			ll cost = dp[j][bit], here = j;
			//cout << cost << " " << here << " " << bitset<18>(bit) << endl; 
			REP1(i, n){
				dp[i][bit | (1 << (i-1) )] = min( dp[i][bit | (1 << (i-1))], 
					cost + abs(x[here] - x[i]) + abs(y[here] - y[i]) + max((ll)0, z[i] - z[here])
				);
			}
		}
	}
	cout << dp[n][full] << endl;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define vt vector
#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))

// 🚩
// 20210424 21:52:52~
//
void flip(string& S, ll a, ll b)
{
    char temp = S.at(a);
    S.at(a) = S.at(b);
    S.at(b) = temp;
}
string cur(string& S, ll N, bool flipped)
{
    if (!flipped)
        return S.substr(0, N) + S.substr(N, N);
    else
        return S.substr(N, N) + S.substr(0, N);
}
int main()
{
    string S;
    ll N;
    ll Q;

    bool flipped = false;
    {
        cin >> N;
        cin >> S;
        cin >> Q;
    }
    rep(q, Q)
    {
        ll t, a, b;
        cin >> t >> a >> b;
        a--;
        b--;
        if (t == 1)
        {
            if (flipped)
            {
                a += N;
                b += N;
                a %= N * 2;
                b %= N * 2;
                //cout << a << "," << b << endl;
            }

            flip(S, a, b);
        }
        else if (t == 2)
        {
            flipped = !flipped;
        }
        //cout << "  " << cur(S, N, flipped) << endl;
    }
    cout << cur(S, N, flipped) << endl;
}
