#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1E4 + 5, INF = 1e9;
const int ROW = MAXN, COL = 2 * MAXN;
struct edge {
  int u, v, w;
  edge(int _u, int _v, int _w) : u(_u), v(_v), w(_w) {}
};
int n, m, k, start;
int r[MAXN], c[MAXN], d[3 * MAXN];
vector<pair<int, int> > adj[3 * MAXN], pr[MAXN], pc[MAXN];
int main() {
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 0; i < k; ++i) {
    scanf("%d %d", &r[i], &c[i]);
    pr[r[i]].push_back(pair<int, int>(c[i], i));
    pc[c[i]].push_back(pair<int, int>(r[i], i));
    if (r[i] == 1 && c[i] == 1) start = i;
    if (r[i] == n && c[i] == m) adj[i].push_back(pair<int, int>(k, 0));
    adj[i].push_back(pair<int, int>(ROW + r[i] - 1, 0));
    adj[i].push_back(pair<int, int>(ROW + r[i], 0));
    adj[i].push_back(pair<int, int>(ROW + r[i] + 1, 0));
    adj[ROW + r[i] - 1].push_back(pair<int, int>(i, 1));
    adj[ROW + r[i]].push_back(pair<int, int>(i, 1));
    adj[ROW + r[i] + 1].push_back(pair<int, int>(i, 1));
    adj[i].push_back(pair<int, int>(COL + c[i] - 1, 0));
    adj[i].push_back(pair<int, int>(COL + c[i], 0));
    adj[i].push_back(pair<int, int>(COL + c[i] + 1, 0));
    adj[COL + c[i] - 1].push_back(pair<int, int>(i, 1));
    adj[COL + c[i]].push_back(pair<int, int>(i, 1));
    adj[COL + c[i] + 1].push_back(pair<int, int>(i, 1));
  }
  adj[ROW + n].push_back(pair<int, int>(k, 1));
  adj[COL + m].push_back(pair<int, int>(k, 1));
  for (int i = 0; i < MAXN; ++i) {
    sort(pr[i].begin(), pr[i].end());
    sort(pc[i].begin(), pc[i].end());
    for (int j = 1; j < pr[i].size(); ++j) {
      int u = pr[i][j].second, v = pr[i][j - 1].second;
      if (pr[i][j].first - pr[i][j - 1].first <= 1) {
        adj[u].push_back(pair<int, int>(v, 0));
        adj[v].push_back(pair<int, int>(u, 0));
      }
    }
    for (int j = 1; j < pc[i].size(); ++j) {
      int u = pc[i][j].second, v = pc[i][j - 1].second;
      if (pc[i][j].first - pc[i][j - 1].first <= 1) {
        adj[u].push_back(pair<int, int>(v, 0));
        adj[v].push_back(pair<int, int>(u, 0));
      }
    }
  }
  for (int i = 0; i < 3 * MAXN; ++i) d[i] = INF;
  priority_queue<pair<int, int> > lis;
  lis.push(pair<int, int>(0, start));
  while (!lis.empty()) {
    pair<int, int> top = lis.top();
    lis.pop();
    int pos = top.second, dist = -top.first;
    if (d[pos] <= dist) continue;
    d[pos] = dist;
    for (pair<int, int> i : adj[pos]) {
      lis.push(pair<int, int>(-dist - i.second, i.first));
    }
  }
  if (d[k] == INF) {
    printf("-1\n");
  } else {
    printf("%d\n", d[k]);
  }
}
