# これ解くのに 998244353 時間かかった! # ぼくが弱いだけで, ★3.0 …かも? # よく考えると T が大きければ何もする必要はないし, 時を戻すときも N+M-1 以上時を戻す必要もない. # そう思うと, 時間の初期値を200として, 時間を400までもつ. # -> 時間は 0 より戻れないし, 400 より進めない. でもそれでいい. # その間で, (時間, 位置) の拡張 dijkstra 法を行うとよい. # O((N+M)NM log(NM)) になる. import heapq n, m, k, t = map(int,input().split()) ikeru = [[[] for j in range(m)] for i in range(n)] for i in range(n): for j in range(m): for x, y in [(-1, 0), (1, 0), (0, 1), (0, -1)]: if 0 <= i + x < n and 0 <= j + y < m: ikeru[i][j].append((i + x, j + y)) modoru = [[(-1,-1) for j in range(m)] for i in range(n)] for i in range(k): a, b, c, d = map(int,input().split()) a -= 1 b -= 1 modoru[a][b] = (c, d) d = [[[10 ** 18] * 401 for i in range(m)] for i in range(n)] d[0][0][200] = 0 pq = [(-d[0][0][200], 0, 0, 200)] while pq: tmp, a, b, nowt = heapq.heappop(pq) tmp = -tmp if tmp > d[a][b][nowt]: continue for x, y in ikeru[a][b]: dist = tmp if dist < d[x][y][min(nowt+1, 400)]: d[x][y][min(nowt+1, 400)] = dist heapq.heappush(pq, (-dist, x, y, min(nowt+1, 400))) if modoru[a][b][0] == -1: continue c, dd = modoru[a][b] dist = tmp + dd if dist < d[a][b][max(0, nowt-c)]: d[a][b][max(0, nowt-c)] = dist heapq.heappush(pq, (-dist, a, b, max(0, nowt-c))) ans = 10 ** 18 for ts in range(401): if ts - 200 > t: continue ans = min(ans, d[n-1][m-1][ts]) if ans == 10 ** 18: print(-1) else: print(ans)