H, W, x, y, N = map(int, input().split()) dp = [0, -1 << 60] for nx, ny, c in [map(int, input().split()) for _ in range(N)]: ndp = [dp[1] + c, max(dp)] if x == nx or y == ny: ndp[0] = max(ndp[0], dp[0] + c) x = nx y = ny dp = ndp print(max(dp))