func_code_string: #include <bits/stdc++.h>   using namespace std;   const int MAXN = 10010;   vector<pair<short int, short int> > G[MAXN];   vector<short int> cul[MAXN], row[MAXN];   pair<short int, short int> p[MAXN];   void add_edge(int u, int v, int d) { G[u].push_back(make_pair(v, d)); }   void init(int n) {     for (int i = 1; i <= n; i++) {       G[i].clear();     }   }   bool vis[MAXN];   int dis[MAXN];   void dijkstra(int s, int n) {     for (int i = 1; i <= n; i++) vis[i] = 0;     for (int i = 1; i <= n; i++) dis[i] = (i == s ? 0 : 0x3FFFFFFF);     priority_queue<pair<short int, short int>,                    vector<pair<short int, short int> >,                    greater<pair<short int, short int> > >         q;     q.push(make_pair(dis[s], s));     while (!q.empty()) {       pair<short int, short int> p = q.top();       int x = p.second;       q.pop();       if (vis[x]) continue;       vis[x] = 1;       for (int i = 0; i < G[x].size(); i++) {         int y = G[x][i].first;         int d = G[x][i].second;         if (!vis[y] && dis[x] + d < dis[y]) {           dis[y] = dis[x] + d;           q.push(make_pair(dis[y], y));         }       }     }   }   map<pair<short int, short int>, short int> mp;   int main() {     int n, m, k, a, b;     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= k; i++) {       scanf( %d%d , &a, &b);       p[i].first = a;       p[i].second = b;       mp[p[i]] = i;       cul[p[i].second].push_back(p[i].first);       row[p[i].first].push_back(p[i].second);     }     init(k);     for (int i = 1; i <= k; i++) {       short int x = p[i].first, y = p[i].second;       for (int j = 0; j < row[x].size(); j++)         if (row[x][j] != y)           add_edge(i, mp[make_pair(x, row[x][j])],                    row[x][j] - 1 != y && row[x][j] + 1 != y);       for (int j = 0; j < cul[y].size(); j++)         if (cul[y][j] != x)           add_edge(i, mp[make_pair(cul[y][j], y)],                    cul[y][j] - 1 != x && cul[y][j] + 1 != x);       for (int l = -1; l <= 1; l += 2) {         for (int j = 0; j < row[x + l].size(); j++)           if (row[x + l][j] != y)             add_edge(i, mp[make_pair(x + l, row[x + l][j])], 1);         for (int j = 0; j < cul[y + l].size(); j++)           if (cul[y + l][j] != x)             add_edge(i, mp[make_pair(cul[y + l][j], y + l)], 1);       }       for (int l = -2; l <= 2; l = l + 4) {         if (x + l > 0 && x + l <= m + 1)           for (int j = 0; j < row[x + l].size(); j++)             add_edge(i, mp[make_pair(x + l, row[x + l][j])], 1);         if (y + l > 0 && y + l <= n + 1)           for (int j = 0; j < cul[y + l].size(); j++)             add_edge(i, mp[make_pair(cul[y + l][j], y + l)], 1);       }     }     dijkstra(1, k);     int ans = 0x3FFFFFFF;     for (int i = 1; i <= k; i++) {       if (dis[i] == 0x3FFFFFFF || dis[i] > ans) continue;       if (n - p[i].first + m - p[i].second <= 1)         ans = min(ans, dis[i] + (mp[make_pair(n, m)] == 0));       else if (n - p[i].first <= 1 || m - p[i].second <= 1)         ans = min(ans, dis[i] + 1);     }     if (ans == 0x3FFFFFFF)       cout << -1 << endl;     else       cout << ans << endl;     return 0;   }   