#include <bits/stdc++.h>
using namespace std;
int n, m, k;
inline int colDist(int r, int c, int col, int row) {
  if (col < 1 || col > m) return -1;
  if (col != c || row != r) {
    if (abs(row - r) + abs(col - c) == 1)
      return 0;
    else
      return 1;
  }
  return -1;
}
inline int rowDist(int r, int c, int row, int col) {
  if (row < 1 || row > n) return -1;
  if (row != r || col != c) {
    if (abs(row - r) + abs(col - c) == 1)
      return 0;
    else
      return 1;
  }
  return -1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  map<int, vector<int>> R, C;
  cin >> n >> m >> k;
  int r, c;
  for (int i = 0; i < k; i++) {
    cin >> r >> c;
    R[r].push_back(c);
    C[c].push_back(r);
  }
  map<pair<int, int>,
      set<pair<pair<int, int>, int>,
          std::function<bool(const pair<pair<int, int>, int>,
                             const pair<pair<int, int>, int>)>>::iterator>
      dm;
  set<pair<pair<int, int>, int>,
      std::function<bool(const pair<pair<int, int>, int>,
                         const pair<pair<int, int>, int>)>>
      ds([](const pair<pair<int, int>, int> &p1,
            const pair<pair<int, int>, int> &p2) {
        return (p1.second == p2.second) ? (p1.first < p2.first)
                                        : (p1.second < p2.second);
      });
  set<pair<int, int>> visited;
  dm[{1, 1}] = ds.insert({{1, 1}, 0}).first;
  int target = 1e8 + 5;
  auto dij = [&](pair<pair<int, int>, int> it, int cst) {
    if (!visited.count(it.first)) {
      auto dist = 1e8 + 5;
      auto exist = dm.count(it.first);
      dist = (exist) ? dm[it.first]->second : dist;
      if (cst + it.second < dist) {
        if (exist) {
          ds.erase(dm[it.first]);
          dm.erase(it.first);
        }
        dist = cst + it.second;
        dm[it.first] = ds.insert({it.first, dist}).first;
      }
    }
  };
  while (!ds.empty()) {
    auto v = ds.begin()->first;
    auto cst = ds.begin()->second;
    if (v.first == n && v.second == m && cst < target) target = cst;
    if (n - v.first <= 1 && cst + 1 < target) target = cst + 1;
    if (m - v.second <= 1 && cst + 1 < target) target = cst + 1;
    ds.erase(ds.begin());
    dm.erase(v);
    visited.insert(v);
    for (int i = 0; i <= 4; i++) {
      if (R.count(v.first - 2 + i)) {
        for (auto &col : R[v.first - 2 + i]) {
          auto dd = rowDist(v.first, v.second, v.first - 2 + i, col);
          if (dd != -1) dij({{v.first - 2 + i, col}, dd}, cst);
        }
      }
      if (C.count(v.second - 2 + i)) {
        for (auto &row : C[v.second - 2 + i]) {
          auto dd = colDist(v.first, v.second, v.second - 2 + i, row);
          if (dd != -1) dij({{row, v.second - 2 + i}, dd}, cst);
        }
      }
    }
  }
  cout << ((target < 1e8) ? target : -1) << '\n';
  return 0;
}
