rt
#include <bits/stdc++.h>
using namespace std;
#define inf 1000000000000000
#define V 10000100
#define E 2000100
#define mp make_pair
#define fo(i,a,b) for(int i = a; i <= b; i++)
typedef long long int ll;
struct edge {
int to, next;
ll capa;
};
int cnt = 0, head[V], n, m, k; edge node[E];
inline void add(int fir, int nxt, ll w) {
node[cnt].to = nxt,
node[cnt].capa = w,
node[cnt].next = head[fir],
head[fir] = cnt++;
}
int s, t, dep[V], gap[V], cur[V]; queue<int>que; ll sum = 0;
inline void initing() {
memset(dep, -1, V * sizeof(int));
memcpy(cur, head, (t + 1) * sizeof(int));
}
inline void bfs() {
int fro, ito;
que.push(t); dep[t] = 0; ++gap[dep[t]];
while (!que.empty()) {
fro = que.front(); que.pop();
for (register int i = head[fro]; i != -1; i = node[i].next) {
ito = node[i].to;
if (dep[ito] == -1) {
dep[ito] = dep[fro] + 1;
que.push(ito);
++gap[dep[ito]];
}
}
}
}
ll dfs(int u, ll flow) {
if (u == t || flow == 0)return flow; ll used = 0, wei = 0;
for (register int i = cur[u]; i != -1; i = node[i].next) {
cur[u] = i;
if (dep[u] == dep[node[i].to] + 1 && node[i].capa) {
wei = dfs(node[i].to, min(flow - used, node[i].capa));
if (wei) {
node[i].capa -= wei;
node[i ^ 1].capa += wei;
used += wei;
}
}
if (used == flow)return used;
}
--gap[dep[u]];
if (!gap[dep[u]])dep[s] = t + 1;
++gap[++dep[u]];
return used;
}
ll ISAP() {
initing(); bfs();
while (dep[s] < t) {
sum += dfs(s, inf);
memcpy(cur, head, (t + 1) * sizeof(int));
}
return sum;
}
inline void addE(int u, int v, ll w) {
add(u, v, w);
add(v, u, 0);
}
inline int col(int x, int y) {
x = (x - 1) % 4 + 1, y = (y - 1) % 2 + 1;
if (x == 1 && y == 2 || x == 4 && y == 1)return 0;
else if (x == 1 && y == 1 || x == 4 && y == 2)return 1;
else if (x == 2 && y == 1 || x == 3 && y == 2)return 2;
else return 3;
}//red:0, yellow:1, blue: 2, green: 3
inline int ok(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m; }
inline int in(int x, int y) { return (x - 1) * m + y; }
map<pair<int, int>, ll>mmp;
map<pair<int, int>, ll>::iterator iter;
int dx[5] = { 0 , 1, 0, -1, 0 }, dy[5] = { 0, 0, 1, 0, -1 };
int main() {
ios::sync_with_stdio(false);
cin.tie(); cout.tie();
cin >> n >> m >> k; s = n * m + 1, t = n * m + 2;
memset(head, -1, V * sizeof(int));
int x, y; ll w; int c;
while(k--){
cin >> x >> y >> w;
c = col(x, y);
if (!c)addE(s, in(x, y), w);
else if (c == 3)addE(in(x, y), t, w);
if(c != 3)mmp[mp(x, y)] = w;
}
iter = mmp.begin(); int nx, ny;
while (iter != mmp.end()) {
x = iter->first.first, y = iter->first.second;
c = col(x, y);
fo(i, 1, 4) {
nx = x + dx[i], ny = y + dy[i], w = iter->second;
if (!ok(nx, ny) || (!mmp.count(mp(nx, ny)) && col(nx, ny) != 3))continue;
if (c == 1) {
if (col(nx, ny) == 2)addE(in(x, y), in(nx, ny), min(w, mmp[mp(nx, ny)]));
else if (col(nx, ny) == 0)addE(in(nx, ny), in(x, y), inf);
}
else if (col(nx, ny) == 3 && c == 2)addE(in(x, y), in(nx, ny), inf);
}
++iter;
}
cout << ISAP();
return 0;
}