import sys



inp = [int(x) for x in sys.stdin.read().split()]; ii = 0



rows, cols, k = inp[ii:ii+3]; ii += 3

items = [[] for _ in range(rows+1)]

best = [0]*(cols+1)

for _ in range(k):

	r, c, v = inp[ii:ii+3]; ii += 3

	items[r].append([c, v])

for i in range(rows+1):

	items[i].sort()

ans = 0

for r in range(1, rows+1):

	dp = []

	best1, best2 = 0, 0

	Row = items[r]

	for c, v in Row:

		v1 = best[c] + v

		v2 = best1 + v

		v3 = best2 + v

		V = v1

		if V < v2:

			V = v2

		if V < v3:

			V = v3

		dp.append(V)

		if v1 > best1:

			best1 = v1

		if v2 > best2:

			best2 = v2

		if ans < V:

			ans = V

	for j in range(len(dp)):

		if best[Row[j][0]] < dp[j]:

			best[Row[j][0]] = dp[j]

	for j in range(2, cols+1):

		if best[j] < best[j-1]:

			best[j] = best[j-1]

print(ans)

import sys



inp = [int(x) for x in sys.stdin.read().split()]; ii = 0

shift = int(10**9 + 1)

rows, cols, k = inp[ii:ii+3]; ii += 3

items = [[] for _ in range(rows+1)]

best = [0]*(cols+1)

for _ in range(k):

	r, c, v = inp[ii:ii+3]; ii += 3

	items[r].append(c*shift+v)

for i in range(rows+1):

	items[i].sort()

ans = 0

for r in range(1, rows+1):

	dp = []

	best1, best2 = 0, 0

	Row = items[r]

	for X in Row:

		c, v = X//shift, X%shift

		v1 = best[c] + v

		v2 = best1 + v

		v3 = best2 + v

		V = v1

		if V < v2:

			V = v2

		if V < v3:

			V = v3

		dp.append(V)

		if v1 > best1:

			best1 = v1

		if v2 > best2:

			best2 = v2

		if ans < V:

			ans = V

	for j in range(len(dp)):

		c = Row[j]//shift

		if best[c] < dp[j]:

			best[c] = dp[j]

	for j in range(2, cols+1):

		if best[j] < best[j-1]:

			best[j] = best[j-1]

print(ans)

import sys

inp = [int(x) for x in sys.stdin.read().split()]
ii = 0
rows, cols, k = inp[ii : ii + 3]
ii += 3
items = [[] for _ in range(rows + 1)]
best = [0] * (cols + 1)
for _ in range(k):
    r, c, v = inp[ii : ii + 3]
    ii += 3
    items[r].append([c, v])
for i in range(rows + 1):
    items[i].sort()
ans = 0
for r in range(1, rows + 1):
    dp = []
    best1, best2 = 0, 0
    Row = items[r]
    for c, v in Row:
        v1 = best[c] + v
        v2 = best1 + v
        v3 = best2 + v
        V = v1
        if V < v2:
            V = v2
        if V < v3:
            V = v3
        dp.append(V)
        if v1 > best1:
            best1 = v1
        if v2 > best2:
            best2 = v2
        if ans < V:
            ans = V
    for j in range(len(dp)):
        if best[Row[j][0]] < dp[j]:
            best[Row[j][0]] = dp[j]
    for j in range(2, cols + 1):
        if best[j] < best[j - 1]:
            best[j] = best[j - 1]
print(ans)

import sys

inp = [int(x) for x in sys.stdin.read().split()]
ii = 0
shift = int(10**9 + 1)
rows, cols, k = inp[ii : ii + 3]
ii += 3
items = [[] for _ in range(rows + 1)]
best = [0] * (cols + 1)
for _ in range(k):
    r, c, v = inp[ii : ii + 3]
    ii += 3
    items[r].append(c * shift + v)
for i in range(rows + 1):
    items[i].sort()
ans = 0
for r in range(1, rows + 1):
    dp = []
    best1, best2 = 0, 0
    Row = items[r]
    for X in Row:
        c, v = X // shift, X % shift
        v1 = best[c] + v
        v2 = best1 + v
        v3 = best2 + v
        V = v1
        if V < v2:
            V = v2
        if V < v3:
            V = v3
        dp.append(V)
        if v1 > best1:
            best1 = v1
        if v2 > best2:
            best2 = v2
        if ans < V:
            ans = V
    for j in range(len(dp)):
        c = Row[j] // shift
        if best[c] < dp[j]:
            best[c] = dp[j]
    for j in range(2, cols + 1):
        if best[j] < best[j - 1]:
            best[j] = best[j - 1]
print(ans)

+shift = int(10**9 + 1) -    items[r].append([c, v]) +    items[r].append(c * shift + v) -    for c, v in Row: +    for X in Row: +        c, v = X // shift, X % shift -        if best[Row[j][0]] < dp[j]: -            best[Row[j][0]] = dp[j] +        c = Row[j] // shift +        if best[c] < dp[j]: +            best[c] = dp[j]