# https://yukicoder.me/problems/no/3597 def same_line(h0, w0, h1, w1): if h0 == h1 or w0 == w1: return True elif h0 - w0 == h1 - w1: return True elif h0 + w0 == h1 + w1: return True return False def main(): H, W, sx, sy, N = map(int, input().split()) xyc = [] for _ in range(N): x, y, c = map(int, input().split()) xyc.append((x, y, c)) dp = [-1] * 2 dp[0] = 0 prev_x = sx prev_y = sy for x, y, c in xyc: new_dp = [-1] * 2 new_dp[0] = max(new_dp[0], dp[0]) new_dp[0] = max(new_dp[0], dp[1]) new_dp[1] = max(new_dp[1], dp[0] + c) if same_line(prev_x, prev_y, x, y): new_dp[1] = max(new_dp[1], dp[1] + c) dp = new_dp prev_x = x prev_y = y print(max(dp)) if __name__ == "__main__": main()