from collections import deque, defaultdict, Counter from bisect import bisect_left, bisect_right, insort from itertools import permutations, combinations, groupby from heapq import heappop, heappush import math, sys input = lambda: sys.stdin.readline().rstrip("\r\n") def printl(li, sep=" "): print(sep.join(map(str, li))) def yn(flag): print(Yes if flag else No) _int = lambda x: int(x)-1 MOD = 998244353 #10**9+7 INF = 1<<60 Yes, No = "Yes", "No" def ctypes(li, types): assert len(li) == len(types) return [t(a) for a, t in zip(li, types)] def tinput(*types): li = input().split() return ctypes(li, types) def same(i, j, a, b): return i == a or j == b or i-j == a-b or i+j == a+b H, W, sx, sy, N = tinput(int, int, _int, _int, int) dp = [0]*(N+1) bsx, bsy = sx, sy XYC = [] mx = 0 for i in range(N): x, y, c = tinput(_int, _int, int) XYC.append((x, y, c)) if i > 0: dp[i+1] = max(dp[i+1], dp[i-1]+c) if same(sx, sy, x, y): dp[i+1] = max(dp[i+1], dp[i]+c) sx, sy = x, y print(max(dp))