#yukicoder423E Warp Zone #入力受取 点は座標圧縮する H, W, N = map(int, input().split()) P = set([(1, 1), (H, W)]) X = [tuple(map(int, input().split())) for _ in range(N)] for a, b, c, d in X: P.add((a, b)) P.add((c, d)) P = sorted(P) R = {(x, y): i for i, (x, y) in enumerate(P)} M = len(P) #二点間距離をdistに格納 これ以降、座標を陽に持つ必要がなくなる dist = lambda P1, P2: abs(P1[0] - P2[0]) + abs(P1[1] - P2[1]) D = [[dist(P1, P2) for P2 in P] for P1 in P] del P #ワープを反映 G = [[] for _ in range(M)] for a, b, c, d in X: G[ R[(a, b)] ].append( R[(c, d)] ) S, E = R[(1, 1)], R[(H, W)] del R #オリジナルダイクストラ法 DP = [10 ** 18] * M checked = [False] * M DP[S] = 0 for _ in range(M): d, i = min([(DP[i], i) for i in range(M) if checked[i] == False]) checked[i] = True for j in range(M): if checked[j]: assert DP[j] <= DP[i] + D[i][j] DP[j] = min(DP[j], DP[i] + D[i][j]) for j in G[i]: if checked[j]: assert DP[j] <= DP[i] + 1 DP[j] = min(DP[j], DP[i] + 1) #答えを出力 print(DP[E])