#include <bits/stdc++.h>
const int inf = 0x3f3f3f3f;
const int maxn = 10050;
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 100010;
struct qnode {
  int v;
  int c;
  qnode(int _v = 0, int _c = 0) : v(_v), c(_c) {}
  bool operator<(const qnode &r) const { return c > r.c; }
};
struct Edge {
  int v, cost;
  Edge(int _v = 0, int _cost = 0) : v(_v), cost(_cost) {}
};
vector<Edge> E[MAXN];
bool vis[MAXN];
int dist[MAXN];
void Dijkstra(int n, int start) {
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; i++) dist[i] = INF;
  priority_queue<qnode> que;
  while (!que.empty()) que.pop();
  dist[start] = 0;
  que.push(qnode(start, 0));
  qnode tmp;
  while (!que.empty()) {
    tmp = que.top();
    que.pop();
    int u = tmp.v;
    if (vis[u]) continue;
    vis[u] = true;
    for (int i = 0; i < E[u].size(); i++) {
      int v = E[tmp.v][i].v;
      int cost = E[u][i].cost;
      if (!vis[v] && dist[v] > dist[u] + cost) {
        dist[v] = dist[u] + cost;
        que.push(qnode(v, dist[v]));
      }
    }
  }
}
void addedge(int u, int v, int w) { E[u].push_back(Edge(v, w)); }
vector<int> row[maxn], col[maxn];
map<pair<int, int>, int> mp;
int n, m, k;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int cal(int x, int y) { return mp[make_pair(x, y)]; }
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> m >> k;
  for (int i = 1; i <= k; i++) {
    int x, y;
    cin >> x >> y;
    mp[make_pair(x, y)] = i;
    row[x].push_back(y);
    col[y].push_back(x);
  }
  if (!mp[make_pair(n, m)]) {
    mp[make_pair(n, m)] = k + 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < row[i].size(); j++) {
      addedge(cal(i, row[i][j]), k + i, 0);
      addedge(k + n + i, cal(i, row[i][j]), 0);
      for (int q = 0; q < 4; q++)
        if (mp[make_pair(i + dx[q], row[i][j] + dy[q])])
          addedge(cal(i, row[i][j]), cal(i + dx[q], row[i][j] + dy[q]), 0);
    }
    addedge(k + i, k + i + n, 1);
    addedge(k + i, k + i + n + 1, 1);
    addedge(k + i, k + i + n + 2, 1);
    if (i > 1) addedge(k + i, k + i + n - 1, 1);
    if (i > 2) addedge(k + i, k + i + n - 2, 1);
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j < col[i].size(); j++) {
      addedge(cal(col[i][j], i), k + 2 * n + i, 0);
      addedge(k + 2 * n + m + i, cal(col[i][j], i), 0);
    }
    addedge(k + 2 * n + i, k + i + 2 * n + m, 1);
    addedge(k + 2 * n + i, k + i + 2 * n + m + 1, 1);
    addedge(k + 2 * n + i, k + i + 2 * n + m + 2, 1);
    if (i > 1) addedge(k + 2 * n + i, k + i + 2 * n + m - 1, 1);
    if (i > 2) addedge(k + 2 * n + i, k + i + 2 * n + m - 2, 1);
  }
  if (mp[make_pair(n, m)] == k + 1) {
    Dijkstra(k + 2 * n + 2 * m, 1);
    int ans = inf;
    for (int i = 0; i < row[n].size(); i++)
      ans = min(ans, dist[mp[make_pair(n, row[n][i])]]);
    for (int i = 0; i < row[n - 1].size(); i++)
      ans = min(ans, dist[mp[make_pair(n - 1, row[n - 1][i])]]);
    for (int i = 0; i < col[m].size(); i++)
      ans = min(ans, dist[mp[make_pair(col[m][i], m)]]);
    for (int i = 0; i < col[m - 1].size(); i++)
      ans = min(ans, dist[mp[make_pair(col[m - 1][i], m - 1)]]);
    if (ans == inf)
      cout << -1 << endl;
    else
      cout << ans + 1 << endl;
  } else {
    Dijkstra(k + 2 * n + 2 * m, 1);
    cout << dist[k] << endl;
  }
}
