#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int maxn = 1e4 + 5;
bool fr[maxn], fc[maxn];
int n, m, k, dist[maxn];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
vector<pair<int, int> > lights;
vector<int> row[maxn], col[maxn];
map<pair<int, int>, int> label;
priority_queue<pair<int, int> > todo;
void fillRow(int r, int val) {
  if (r >= 1 && r <= n && !fr[r]) {
    fr[r] = true;
    for (int x : row[r])
      if (val < dist[x]) {
        dist[x] = val;
        todo.push(make_pair(-val, x));
      }
  }
}
void fillCol(int c, int val) {
  if (c >= 1 && c <= m && !fc[c]) {
    fc[c] = true;
    for (int x : col[c])
      if (val < dist[x]) {
        dist[x] = val;
        todo.push(make_pair(-val, x));
      }
  }
}
void adj(int r, int c, int val) {
  if (label.count(make_pair(r, c))) {
    int x = label[make_pair(r, c)];
    if (dist[x] > val) {
      dist[x] = val;
      todo.push(make_pair(-val, x));
    }
  }
}
int main() {
  int r, c;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < (k); ++i) {
    scanf("%d%d", &r, &c);
    row[r].push_back(i);
    col[c].push_back(i);
    label[make_pair(r, c)] = i;
    lights.push_back(make_pair(r, c));
  }
  for (int i = 0; i < (maxn); ++i) dist[i] = inf;
  if (!label.count(make_pair(n, m))) {
    fillRow(n - 1, 1);
    fillRow(n, 1);
    fillCol(m - 1, 1);
    fillCol(m, 1);
  } else
    todo.push(make_pair(0, label[make_pair(n, m)]));
  while (!todo.empty()) {
    pair<int, int> p = todo.top();
    todo.pop();
    p.first = -p.first;
    if (p.first > dist[p.second]) continue;
    r = lights[p.second].first, c = lights[p.second].second;
    for (int i = 0; i < (4); ++i) adj(r + dx[i], c + dy[i], p.first);
    for (int i = (r - 2); i < (r + 3); ++i) fillRow(i, p.first + 1);
    for (int i = (c - 2); i < (c + 3); ++i) fillCol(i, p.first + 1);
  }
  cout << (dist[0] == inf ? -1 : dist[0]) << endl;
  return 0;
}
