## https://yukicoder.me/problems/no/2695 import heapq MAX_INT = 10 ** 18 def main(): H, W, N = map(int, input().split()) warps = [] for _ in range(N): a, b, c, d = map(int, input().split()) warps.append((a, b, c, d)) points = [(1, 1)] for a, b, c, d in warps: points.append((a, b)) for a, b, c, d in warps: points.append((c, d)) points.append((H, W)) # dijkstra fix = [MAX_INT] * (2 * N + 2) seen = [MAX_INT] * (2 * N + 2) queue = [] seen[0] = 0 heapq.heappush(queue, (0, 0)) while len(queue) > 0: cost, v = heapq.heappop(queue) if fix[v] < MAX_INT: continue fix[v] = cost # ワープ if 1 <= v <= N: w = v + N if fix[w] == MAX_INT: h_v, w_v = points[v] h_w, w_w = points[w] new_cost = cost + 1 if seen[w] > new_cost: seen[w] = new_cost heapq.heappush(queue, (new_cost, w)) # 普通の移動 for w in range(2 * N + 2): if fix[w] < MAX_INT: continue h_v, w_v = points[v] h_w, w_w = points[w] new_cost = cost + (abs(h_v - h_w) + abs(w_v - w_w)) if seen[w] > new_cost: seen[w] = new_cost heapq.heappush(queue, (new_cost, w)) print(fix[-1]) if __name__ == "__main__": main()