#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<vector>
#include<functional>
#include<algorithm>
#include<stdlib.h>
#include<string>
#include<string.h>
#define _USE_MATH_DEFINES
#include<math.h>
#include<deque>
#include<set>
#include<map>
#include<queue>
#include<list>
#include<iostream>
#include <bitset>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(auto i=a;i<b;i++)
#define rep2(i, a)for(auto i : a)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x, std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
ll mod = 998244353ll;
ll power(ll x, ll p) { ll a = 1; while (p > 0) { if (p % 2 == 0) { x *= x; p /= 2; } else { a *= x; p--; } }return a; }
ll mpower(ll x, ll p) { ll a = 1; while (p > 0) { if (p % 2 == 0) { x = x * x % mod; p /= 2; } else { a = a * x % mod; p--; } }return a; }
ll co(ll n, ll k) { ll a = 1; rep(i, 1ll, k) { a *= n - i + 1; a /= i; }return a; }
ll mc(ll n, ll m) { ll k = 1, l = 1; rep(i, n - m + 1, n + 1) k = k * i % mod; rep(i, 1, m + 1) l = l * i % mod; l = mpower(l, mod - 2); return k * l % mod; }

int main(void) {
	int n;
	cin >> n;
	vector<int> x(n), y(n), z(n), num;
	rep(i, 0, n) {
		cin >> x[i] >> y[i] >> z[i];
	}
	x.push_back(x.front());
	y.push_back(y.front());
	z.push_back(z.front());
	n++;
	vector<vector<ll>> dp(n, vector<ll>(1 << n, 1ll << 60));
	dp[0][1] = 0;
	rep(_i, 1, 1 << n) {
		rep(i, 0, n) {
			if (_i >> i & 1) {
				rep(i2, 0, n) {
					if (_i >> i2 & 1) {
						dp[i][_i] = min(dp[i][_i], dp[i2][_i ^ (1 << i)] + abs(x[i] - x[i2]) + abs(y[i] - y[i2]) + max(0, z[i] - z[i2]));
						//printf("_i:%d i:%d i2:%d %lld %lld\n", _i, i, i2, dp[i][_i], dp[i2][_i ^ (1 << i)]);
					}
				}
			}
		}
	}
	printf("%lld\n", dp[n - 1][(1 << n) - 1]);
	return 0;
}
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define all(x) (x).begin(),(x).end()
using namespace std;
constexpr int INF = 1e9, MOD = 1e9 + 7;
constexpr int64_t LINF = 5e18, LMOD = 998244353;
// #include <atcoder/all>
// using namespace atcoder;
// const int dy[]={0,-1,0,1,1,-1,-1,1};
// const int dx[]={1,0,-1,0,1,1,-1,-1};

// BEGIN CUT HERE
/**
 * @brief セグメント木
 * @tparam S 要素モノイド
 */
template<class S>
struct SegmentTree {
public:
	/**
	 * @brief 単位元で初期化
	 * @param N サイズ
	 * @param F 二項演算 (S, S) -> S 
 	 * @param e 要素モノイドの単位元
	 */
	SegmentTree(const size_t N, const function<S(S, S)>& F, const S& e) : SegmentTree(vector<S>(N, e), F, e) {}
	/**
	 * @brief 与配列で初期化
	 * @param v 配列
	 * @param F 二項演算 (S, S) -> S 
 	 * @param e 要素モノイドの単位元
	 */
	SegmentTree(const vector<S>& v, const function<S(S, S)>& F, const S& e) : F(F), e(e) {
		N = 1 << pow2(v.size());
		val = vector<S>(N << 1, e);
		for(size_t i = 0; i < v.size(); ++i) {
			val[N + i] = v[i];
		}
		for(size_t i = N - 1; i >= 1; --i) {
			update(i);
		}
	}
	/**
	 * @brief 1点更新
	 */
	void set(size_t idx, const S& _val) {
		assert(idx < N);
		idx += N;
		val[idx] = _val;
		while(idx > 1) {
			idx >>= 1;
			update(idx);
		}
	}
	/**
	 * @brief 区間取得[l, r)
	 */
	S get(size_t l, size_t r) {
		assert(l < r and r <= N);
		S valL = e;
		S valR = e;
		for(l += N, r += N; l < r; l >>= 1, r >>= 1) {
			if(l & 1) {
				valL = F(valL, val[l++]);
			}
			if(r & 1) {
				valR = F(val[--r], valR);
			}
		}
		return F(valL, valR);
	}
	/**
	 * @brief 1点取得
	 */
	S get(const size_t idx) {
		assert(idx < N);
		return val[N + idx];
	}
	/**
	 * @brief 1点取得
	 */
	S operator[](const size_t idx) {
		assert(idx < N);
		return val[N + idx];
	}
private:
	const function<S(S, S)> F;
	const S e;
	size_t N;
	vector<S> val;
	/**
	 * @return n <= 2^k なる最小の k
	 */
	size_t pow2(const size_t n) {
		size_t k = 0;
		while((1 << k) < n) {
			++k;
		}
		return k;
	}
	/**
	 * @brief v[idx]の更新
	 */
	void update(size_t idx) {
		val[idx] = F(val[idx << 1], val[(idx << 1) | 1]);
	}
};
// END CUT HERE

int main() {
	int n, m, q; cin >> n >> m >> q;
	vector<tuple<int, int, int>> query;
	vector<int> val;
	rep(i,q) {
		int t, x, y; cin >> t >> x >> y;
		query.emplace_back(t, x - 1, y);
		val.emplace_back(y);
	}
	val.emplace_back(0);
	sort(val.begin(), val.end());
	val.erase(unique(val.begin(), val.end()), val.end());
	int vn = val.size();
	SegmentTree<int64_t> seg_a(vn, [](int64_t l, int64_t r) {
		return l + r;
	}, 0);
	SegmentTree<int64_t> seg_b(vn, [](int64_t l, int64_t r) {
		return l + r;
	}, 0);
	SegmentTree<int64_t> seg_a_cnt(vn, [](int64_t l, int64_t r) {
		return l + r;
	}, 0);
	SegmentTree<int64_t> seg_b_cnt(vn, [](int64_t l, int64_t r) {
		return l + r;
	}, 0);
	int64_t ans = 0;
	int a[n] = {};
	int b[m] = {};
	seg_a_cnt.set(0, n);
	seg_b_cnt.set(0, m);
	for(auto [t, x, y] : query) {
		if(t == 1) {
			ans -= seg_b.get(a[x], vn);
			if(0 < a[x]) ans -= seg_b_cnt.get(0, a[x]) * val[a[x]];

			seg_a.set(a[x], seg_a[a[x]] - val[a[x]]);
			seg_a_cnt.set(a[x], seg_a_cnt[a[x]] - 1);
			a[x] = lower_bound(val.begin(), val.end(), y) - val.begin();
			seg_a.set(a[x], seg_a[a[x]] + val[a[x]]);
			seg_a_cnt.set(a[x], seg_a_cnt[a[x]] + 1);

			ans += seg_b.get(a[x], vn);
			if(0 < a[x]) ans += seg_b_cnt.get(0, a[x]) * val[a[x]];
		} else {
			ans -= seg_a.get(b[x], vn);
			if(0 < b[x]) ans -= seg_a_cnt.get(0, b[x]) * val[b[x]];

			seg_b.set(b[x], seg_b[b[x]] - val[b[x]]);
			seg_b_cnt.set(b[x], seg_b_cnt[b[x]] - 1);
			b[x] = lower_bound(val.begin(), val.end(), y) - val.begin();
			seg_b.set(b[x], seg_b[b[x]] + val[b[x]]);
			seg_b_cnt.set(b[x], seg_b_cnt[b[x]] + 1);

			ans += seg_a.get(b[x], vn);
			if(0 < b[x]) ans += seg_a_cnt.get(0, b[x]) * val[b[x]];
		}
		cout << ans << '\n';
	}
	return 0;
}
