x=raw_input() x=x.split(' ') x=map(lambda e:int(e),x) n=x[0] y=x[2] x=x[1] temp=n g=[] while n>=1: f=raw_input() f=f.split(' ') f=map(lambda e:int(e),f) i=[] i.append(f[0]) i.append(f[1]) i.append(f[3]) use=(f[2],i) g.append(list(use)) n=n-1 g.sort() cnt=1 vis=0 fut=1 pts=g[0][1][2] while cnt!=temp: if abs(g[fut][1][0]-g[vis][1][0]) <=x and abs(g[fut][1][1]-g[vis][1][1])<=y and vis!=fut: cnt=cnt+1 pts=pts+g[fut][1][2] jst=g[fut] g.remove(g[vis]) vis=g.index(jst) fut=0 else: fut=fut+1 print pts