#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 

using namespace __gnu_pbds; 
using namespace std;

#define int           long long
#define S             second
#define F             first
#define pb            push_back
#define all(c)        (c).begin(),(c).end()
#define rall(c)       (c).rbegin(),(c).rend() 
#define lb            lower_bound
#define ub            upper_bound
#define si(c)         (int)((c).size())
#define lcm(a, b)      (a * (b / __gcd(a,b)))  
#define inf           (int)(4e18)
#define endl          '\n'
#define mp            make_pair
#define time(s)       (double(clock()-s)/double(CLOCKS_PER_SEC))
#define debug(args...)    _F(#args, args)
#define vi            std::vector<int>
#define pii           pair<int, int> 
#define vpi           vector<pii>
#define ordered_set   tree<pii, null_type,less<pii>, rb_tree_tag,tree_order_statistics_node_update> 

clock_t start;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

template<typename T> void _F(const char *name, T arg1){ cerr << name << " = " << arg1 << endl;}
template<typename T, typename... Args> void _F(const char *names, T arg1, Args... args)
{ const char *name = strchr(names, ',');cerr.write(names, name-names) << " = " << arg1 << endl;_F(name+2, args...);}

template< typename T1, typename T2 > istream& operator>>(istream& in, pair<T1, T2> &q){ in >> q.F >> q.S; return in;}
template< typename T1, typename T2 > ostream& operator<<(ostream& out, pair<T1, T2> &q){ out << q.F << " " << q.S; return out;}
template< typename T1, typename T2 > pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2){ return {p1.F+p2.F, p1.S+p2.S};}
template< typename T1, typename T2 > pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2){ return {p1.F-p2.F, p1.S-p2.S};}
template< typename T1, typename T2 > bool operator<(pair<T1, T2> p1, pair<T1, T2> p2){ return p1 < p2 ;}

template<typename T> void Unique(vector<T> &v) {
    sort(all(v)), v.resize(distance(v.begin(), unique(all(v))));
}

void solve() {
	int n;
	cin >> n;
	int x[n], y[n], z[n];
	for(int i = 0; i < n; i++) {
		cin >> x[i] >> y[i] >> z[i];
	}

	auto cal = [&](int i, int j) {
		return abs(x[i]-x[j]) + abs(y[i]-y[j]) + max(0LL, z[j]-z[i]);
	};

	int dp[n][(1<<n)];
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < (1<<n); j++)
			dp[i][j] = inf;
	}
	dp[0][1] = 0;
	for(int j = 3; j < (1<<n); j++) {
		if(!(j&1)) continue;
		for(int i = 0; i < n; i++) {
			if((j>>i)&1) {
				for(int k = 0; k < n; k++) {
					if(i != k)
					dp[i][j] = min(dp[i][j], dp[k][j^(1<<i)] + cal(k, i));
				}
			}
		}
	}
	// cout << dp[0][(1<<n)-1] << endl;
	// cout << dp[1][(1<<n)-1] << endl;
	int ans = inf;
	for(int j = 0; j < n; j++) {
		ans = min(ans, dp[j][(1<<n)-1] + cal(j, 0));
	}
	cout << ans << endl;
}

signed main(){

    ios_base::sync_with_stdio(0); 
    cin.tie(0); cout.tie(0);   
    start = clock(); 

   

    int test = 1;
    // cin >> test;
 
    cout << fixed << setprecision(20);
     
    for(int i = 1; i <= test; ++i){
    	solve();
    }

    cerr << time(start);
    return 0;
}


/**
 *    author:  longvu
**/
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define FASTIO std::ios::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL);

#define nax (int) (100001)

const int mod = 1e9 + 7;

int32_t main()
{
    FASTIO

    int n;

    cin >> n;

    string s1, s2;

    cin >> s2;

    s1 = s2.substr(0, n);
    s2 = s2.substr(n, n);

    int q;

    cin >> q;

    while (q--)
    {
        int type, a, b;

        cin >> type >> a >> b;

        if (type == 1)
        {
            a--; b--;
            char c;
            int flag = 0;
            
            if (a > s1.size() - 1)
            {
                flag = 1;
                a -= s1.size();
                c = s2[a];
            }

            if (b > s1.size() - 1)
            {
                b -= s1.size();
                if (flag)
                {
                    s2[a] = s2[b];
                    s2[b] = c;
                }
                else
                {
                    c = s2[b];
                    s2[b] = s1[a];
                    s1[a] = c;
                }
            }
            else
            { 
                c = s1[a];
                s1[a] = s1[b];
                s1[b] = c;
            }
        }
        else
        {
            swap(s1, s2);
        }
    }

    cout << s1 + s2;

    return 0;
}