import sys input = sys.stdin.readline from operator import itemgetter from heapq import heappop,heappush H,W,N=map(int,input().split()) WA=[list(map(int,input().split())) for i in range(N)] WA=[[1,1,1,1]]+WA+[[H,W,H,W]] DP=[1<<63]*len(WA) WA.sort(key=itemgetter(0)) WA.sort(key=itemgetter(1)) DP[0]=0 Q=[(0,0)] while Q: time,ind=heappop(Q) if DP[ind]!=time: continue a,b,c,d=WA[ind] for j in range(len(WA)): e,f,g,h=WA[j] if a<=e and b<=f: dis=(e-a)+(f-b) if DP[j]>DP[ind]+dis: DP[j]=DP[ind]+dis heappush(Q,(DP[j],j)) if c<=e and d<=f: dis=(e-c)+(f-d) if DP[j]>DP[ind]+dis+1: DP[j]=DP[ind]+dis+1 heappush(Q,(DP[j],j)) print(DP[-1])