#include using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) void solve() { ll h, w, n; cin >> h >> w >> n; vector> vs; vs.reserve(n * 2 + 2); rep(i, n) rep(_, 2) { ll a, b; cin >> a >> b; vs.emplace_back(a, b); } vs.emplace_back(1, 1); vs.emplace_back(h, w); auto calc_dist = [&](ll i, ll j) -> ll { if (i < n * 2 && i % 2 == 0 && i + 1 == j) { return 1; } ll dx = vs[i].first - vs[j].first; ll dy = vs[i].second - vs[j].second; return abs(dx) + abs(dy); }; priority_queue> que; vector dist(n * 2 + 2, 1e18); que.emplace(0, n * 2); dist[n * 2] = 0; while (!que.empty()) { auto [cd, i] = que.top(); que.pop(); cd = -cd; if (dist[i] < cd) continue; rep(j, vs.size()) { if (i == j) continue; ll nd = cd + calc_dist(i, j); if (dist[j] <= nd) continue; dist[j] = nd; que.emplace(-nd, j); } } cout << dist[n * 2 + 1] << '\n'; } int main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); int T = 1; for (int t = 0; t < T; t++) { solve(); } return 0; }