from collections import * N, E, Sx, Sy, C = map(int, input().split()) T = defaultdict(lambda: [0, 0]); T[(0, Sx, Sy)] = [0, 0] K = ((0, 1), (1, 0), (-1, 0), (0, -1)) for _ in range(C): x, y, t = map(int, input().split()) for dx, dy in K: if N>x+dx>-1=e2 and ps>=s2 for pe, ps in D[j]): continue D[j] = [p for p in D[j] if p[0]>e2 or p[1]>s2]; D[j] += [(e2, s2)] print(max(p[1] for u in D for p in u))