from heapq import heappush,heappop H,W,N = map(int,input().split()) ten = {} cnt = 1 warp = {} ten[(1,1)] = 1 cnt += 1 for i in range(N): a,b,c,d = map(int,input().split()) #a=頂点,b=頂点,c=コスト if (a,b) not in ten: ten[(a,b)] = cnt cnt+=1 if (c,d) not in ten: ten[(c,d)] = cnt cnt+=1 warp[(a,b,c,d)] = True ten[(H,W)] = cnt G = [[] for _ in range(cnt+1)] for ind1,val1 in ten.items(): for ind2,val2 in ten.items(): if val1 == val2: continue aa,bb,cc,dd = ind1[0],ind1[1],ind2[0],ind2[1] if (aa,bb,cc,dd) in warp: G[val1].append([val2,1]) else: cost = abs(aa-cc) + abs(bb-dd) G[val1].append([val2,cost]) dist = [-1]*(cnt+1) visited = [False]*(cnt+1) Q = [] heappush(Q,(0,1)) #◆=出発地点コスト(通常は0),◇=出発地点の頂点 dist[1] = 0 while len(Q)>0: a,b = heappop(Q) #a=コスト(◆),b=頂点(◇) if visited[b]: continue visited[b] = True for c,d in G[b]: #c=頂点,d=コスト if dist[c] == -1 or dist[c] > dist[b]+d: dist[c] = dist[b]+d heappush(Q,(dist[c],c)) print(dist[-1])