#include <bits/stdc++.h>

#define int long long

#define INF 1145141919810LL
#define MAX_K 200005

using namespace std;

struct Edge {
    int to, cost;
};

struct Node {
    int dir, num, dst;
    bool operator<(const Node &r) const {
        return dst > r.dst;
    }
};

struct Point {
    int num, p, pp;
    bool operator<(const Point &r) const {
        if(p == r.p) {
            return pp < r.pp;
        } else {
            return p < r.p;
        }
    }
};

int n, m, k;
int x[MAX_K], y[MAX_K];

int dst[2][MAX_K];
vector<Edge> edge[2][MAX_K];

int ans = -1;
int live_start = -1, live_goal = -1;

main() {

    cin >> m >> n >> k;

    {
        vector<Point> tate;
        vector<Point> yoko;
        for(int i = 0; i < k; ++i) {
            cin >> x[i] >> y[i];
            tate.push_back((Point){ i, x[i], y[i] });
            yoko.push_back((Point){ i, y[i], x[i] });
            if(x[i] == 1 && y[i] == 1) live_start = i;
            if(x[i] == m && y[i] == n) live_goal = i;
        }

        if(live_start == -1) {
            live_start = k;
            tate.push_back((Point){ k, 1, 1 });
            x[k] = 1;
            y[k] = 1;
            ++k;
        } 

        if(live_goal == -1) {
            live_goal = k;
            tate.push_back((Point){ k, m, n });
            yoko.push_back((Point){ k, n, m });
            x[k] = m;
            y[k] = n;
            ++k;
        }

        //cout << live_start << " " << live_goal << endl;

        sort(tate.begin(), tate.end());
        sort(yoko.begin(), yoko.end());

        for(int i = 1; i < tate.size(); ++i) {
            if(tate[i - 1].p == tate[i].p) {
                edge[0][tate[i - 1].num].push_back((Edge){ tate[i].num, tate[i].pp - tate[i - 1].pp });
                edge[0][tate[i].num].push_back((Edge){ tate[i - 1].num, tate[i].pp - tate[i - 1].pp });
            }
        }

        for(int i = 1; i < yoko.size(); ++i) {
            if(yoko[i - 1].p == yoko[i].p) {
                edge[1][yoko[i - 1].num].push_back((Edge){ yoko[i].num, yoko[i].pp - yoko[i - 1].pp });
                edge[1][yoko[i].num].push_back((Edge){ yoko[i - 1].num, yoko[i].pp - yoko[i - 1].pp });
            }
        }
    }
   
    /*
    for(int i = 0; i < k; ++i) {
        cout << x[i] << " " << y[i] << " tate" << endl;
        for(int j = 0; j < edge[0][i].size(); ++j) {
            int e = edge[0][i][j].to;
            cout << x[e] << "  " << y[e] << endl;
        }
        cout << x[i] << " " << y[i] << " yoko" << endl;
        for(int j = 0; j < edge[1][i].size(); ++j) {
            int e = edge[1][i][j].to;
            cout << x[e] << "  " << y[e] << endl;
        }
    }
    */

    priority_queue<Node> q;
    q.push((Node){ 0, live_start, 0 });

    for(int i = 0; i < 2; ++i) {
        for(int j = 0; j < k; ++j) {
            dst[i][j] = INF; 
        }
    }

    dst[0][live_start] = 0;

    while(!q.empty()) {

        Node now = q.top(); q.pop();

        // tate
        for(int j = 0; j < edge[0][now.num].size(); ++j) {
            int next = edge[0][now.num][j].to;
            int cost = dst[now.dir][now.num] + edge[0][now.num][j].cost;
            if(now.dir == 0) {
                if(cost < dst[0][next]) {
                    dst[0][next] = cost;
                    q.push((Node){ 0, next, cost });
                }
            } else {
                ++cost;
                if(cost < dst[0][next]) {
                    dst[0][next] = cost;
                    q.push((Node){ 0, next, cost });
                }
            }
        }

        // yoko
        for(int j = 0; j < edge[1][now.num].size(); ++j) {
            int next = edge[1][now.num][j].to;
            int cost = dst[now.dir][now.num] + edge[1][now.num][j].cost;
            if(now.dir == 0) {
                ++cost;
                if(cost < dst[1][next]) {
                    dst[1][next] = cost;
                    q.push((Node){ 1, next, cost });
                }
            } else {
                if(cost < dst[1][next]) {
                    dst[1][next] = cost;
                    q.push((Node){ 1, next, cost });
                }
            }
        }

    }

    if(dst[0][live_goal] != INF || dst[1][live_goal] != INF) {
        cout << min(dst[0][live_goal], dst[1][live_goal]) << endl;
    } else {
        cout << -1 << endl;
    }

}

Measured memory_bytes: 43284