#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
double PI = 4 * atan(1);
int dr[10001], dc[10001];
vector<int> row[10001], col[10001];
priority_queue<pair<int, int> > todo;
vector<pair<int, int> > lights;
int dist[10001];
map<pair<int, int>, int> label;
int xdir[4] = {1, 0, -1, 0}, ydir[4] = {0, 1, 0, -1};
int n, m, k;
void filrow(int i, int val) {
  if (i >= 1 && i <= n && dr[i] == 0) {
    dr[i] = 1;
    for (int x : row[i])
      if (val < dist[x]) {
        dist[x] = val;
        todo.push({-dist[x], x});
      }
  }
}
void filcol(int i, int val) {
  if (i >= 1 && i <= m && dc[i] == 0) {
    dc[i] = 1;
    for (int x : col[i])
      if (val < dist[x]) {
        dist[x] = val;
        todo.push({-dist[x], x});
      }
  }
}
void ad(int x, int y, int val) {
  if (label.find({x, y}) != label.end())
    if (dist[label[{x, y}]] > val) {
      dist[label[{x, y}]] = val;
      todo.push({-val, label[{x, y}]});
    }
}
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < k; i++) {
    int r, c;
    cin >> r >> c;
    lights.push_back({r, c});
    row[r].push_back(i);
    col[c].push_back(i);
    label[{r, c}] = i;
  }
  for (int i = 0; i < 10001; i++) dist[i] = MOD;
  if (label.find({n, m}) == label.end()) {
    filrow(n - 1, 1);
    filrow(n, 1);
    filcol(m - 1, 1);
    filcol(m, 1);
  } else
    todo.push({0, label[{n, m}]});
  while (todo.size()) {
    auto a = todo.top();
    todo.pop();
    a.first = -a.first;
    if (a.first > dist[a.second]) continue;
    for (int i = 0; i < 4; i++)
      ad(lights[a.second].first + xdir[i], lights[a.second].second + ydir[i],
         a.first);
    for (int i = lights[a.second].first - 2; i < lights[a.second].first + 3;
         i++)
      filrow(i, a.first + 1);
    for (int i = lights[a.second].second - 2; i < lights[a.second].second + 3;
         i++)
      filcol(i, a.first + 1);
  }
  if (dist[0] != MOD)
    cout << dist[0];
  else
    cout << -1;
}
