from sys import stdin input = stdin.readline INF = 1<<60 def judge(a, b, c, d): return a == c or b == d or a+b == c+d or a-b == c-d H, W, sx, sy, N = map(int, input().split()) query = [list(map(int, input().split())) for _ in range(N)] query = [[sx, sy, 0]]+query dp = [-INF]*(N+1) dp[0] = 0 MAX = -INF for i in range(1, N+1): x, y, c = query[i] if 2 <= i: MAX = max(MAX, dp[i-2]+c) dp[i] = MAX if judge(x, y, *query[i-1][:2]): dp[i] = max(dp[i], dp[i-1]+c) print(max(dp))