from heapq import heappush, heappop from collections import defaultdict H, W, N = map(int, input().split()) warp = [list(map(int, input().split())) for _ in range(N)] G = defaultdict(list) G[(1, 1)].append((H, W, (H-1)+(W-1))) for i in range(N): a, b, c, d = warp[i] G[(1, 1)].append((a, b, (a-1)+(b-1))) G[(a, b)].append((c, d, 1)) G[(c, d)].append((H, W, (H-c)+(W-d))) for j in range(N): a2, b2, c2, d2 = warp[j] G[(c, d)].append((a2, b2, abs(a2-c)+abs(b2-d))) def dijkstra(start): dist = defaultdict(int) dist[start] = 0 visited = set() que = [(0, start)] while que: d, (h, w) = heappop(que) if (h, w) in visited: continue visited.add((h, w)) for nh, nw, weight in G[(h, w)]: if (nh, nw) not in dist or dist[(h, w)]+weight < dist[(nh, nw)]: dist[(nh, nw)] = dist[(h, w)]+weight heappush(que, (dist[(nh, nw)], (nh, nw))) return dist print(dijkstra((1, 1))[(H, W)])