#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
const int inf = 1e9;
int n, m, k;
int adjx[4] = {0, 0, -1, 1};
int adjy[4] = {-1, 1, 0, 0};
vector<int> satr[maxn], soton[maxn];
map<pair<int, int>, bool> lit;
map<pair<int, int>, int> h;
void bfs() {
  deque<pair<int, int>> Q;
  Q.push_front({0, 0});
  h[{0, 0}] = 1;
  while (!Q.empty()) {
    pair<int, int> v = Q.front();
    Q.pop_front();
    if (v.first == -1) {
      for (int i = max(v.second - 1, 0); i <= min(v.second + 1, m - 1); i++) {
        for (int u : soton[i]) {
          if (lit[{u, i}] &&
              (!h[{u, i}] || h[{v.first, v.second}] < h[{u, i}])) {
            h[{u, i}] = h[{v.first, v.second}];
            Q.push_front({u, i});
          }
        }
      }
    } else if (v.second == -1) {
      for (int i = max(v.first - 1, 0); i <= min(v.first + 1, n - 1); i++) {
        for (int u : satr[i]) {
          if (lit[{i, u}] &&
              (!h[{i, u}] || h[{v.first, v.second}] < h[{i, u}])) {
            h[{i, u}] = h[{v.first, v.second}];
            Q.push_front({i, u});
          }
        }
      }
    } else {
      for (int i = max(v.second - 1, 0); i <= min(v.second + 1, m - 1); i++) {
        if (!h[{-1, i}] || h[{v.first, v.second}] + 1 < h[{-1, i}]) {
          h[{-1, i}] = h[{v.first, v.second}] + 1;
          Q.push_back({-1, i});
        }
      }
      for (int i = max(v.first - 1, 0); i <= min(v.first + 1, n - 1); i++) {
        if (!h[{i, -1}] || h[{v.first, v.second}] + 1 < h[{i, -1}]) {
          h[{i, -1}] = h[{v.first, v.second}] + 1;
          Q.push_back({i, -1});
        }
      }
      for (int i = 0; i < 4; i++) {
        int nx = v.first + adjx[i];
        int ny = v.second + adjy[i];
        if (nx < 0 || nx >= n || ny < 0 || ny >= m || !lit[{nx, ny}]) {
          continue;
        }
        if (!h[{nx, ny}] || h[{v.first, v.second}] < h[{nx, ny}]) {
          h[{nx, ny}] = h[{v.first, v.second}];
          Q.push_front({nx, ny});
        }
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m >> k;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    lit[{--x, --y}] = true;
    satr[x].push_back(y);
    soton[y].push_back(x);
  }
  bfs();
  if (lit[{n - 1, m - 1}]) {
    cout << h[{n - 1, m - 1}] - 1 << '\n';
  } else {
    if (h[{-1, m - 1}] && h[{n - 1, -1}]) {
      cout << min(h[{-1, m - 1}], h[{n - 1, -1}]) - 1 << '\n';
    } else if (h[{-1, m - 1}]) {
      cout << h[{-1, m - 1}] - 1 << '\n';
    } else if (h[{n - 1, -1}]) {
      cout << h[{n - 1, -1}] - 1 << '\n';
    } else {
      cout << -1 << '\n';
    }
  }
}
