from collections import defaultdict from heapq import * h, w, n = map(int, input().split()) warp = dict() for _ in range(n): a, b, c, d = map(lambda x: int(x) - 1, input().split()) warp[(a, b)] = (c, d) q = [[0, 0, 0]] INF = float("INF") dist = defaultdict(lambda: INF) dist[(0, 0)] = 0 while q: dis, i, j = heappop(q) if (i, j) in dist and dist[(i, j)] != dis: continue # warpを使う場合 if (i, j) in warp: ti, tj = warp[(i, j)] if dist[(ti, tj)] > dist[(i, j)] + 1: dist[(ti, tj)] = dist[(i, j)] + 1 heappush(q, [dist[(ti, tj)], ti, tj]) # i,j から別のワープ a,b まで移動 for (a, b), (c, d) in warp.items(): md = abs(i - a) + abs(j - b) if dist[(a, b)] > dist[(i, j)] + md: dist[(a, b)] = dist[(i, j)] + md heappush(q, [dist[(a, b)], a, b]) # H,Wまで移動 md = abs(i - h + 1) + abs(j - w + 1) if dist[(h - 1, w - 1)] > dist[(i, j)] + md: dist[(h - 1, w - 1)] = dist[(i, j)] + md heappush(q, [dist[((h - 1, w - 1))], h - 1, w - 1]) print(dist[(h - 1, w - 1)])