2 5 10
2 2
1 1
1 4
2 1
1 2
1 5
2 4
2 5
1 3
2 3
 2 2 2
1 1
2 2
 3 3 9
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
 7 5 35
5 3
3 5
1 1
2 5
6 4
7 2
5 1
3 4
6 5
7 3
4 1
2 2
4 2
2 4
4 3
3 3
6 1
1 2
4 5
1 3
2 1
5 5
7 5
7 1
6 2
1 4
1 5
3 2
4 4
5 2
2 3
6 3
7 4
5 4
3 1
 1 5 4
1 1
1 2
1 4
1 5
 1 1 1
1 1
 5 7 21
4 4
4 5
5 5
5 6
5 7
5 1
5 2
4 2
3 2
3 1
3 7
3 6
2 6
1 6
1 7
1 1
1 2
1 3
1 4
2 4
3 4
 2 1 1
1 1
 2 2 4
2 1
1 1
1 2
2 2
 3000 3000 1
2222 738
 5 7 35
1 2
2 3
5 1
5 7
4 4
5 3
3 1
2 6
4 5
2 7
4 1
2 1
2 2
1 7
5 6
3 6
4 6
1 6
1 1
3 3
3 4
4 2
2 5
5 5
3 5
2 4
1 5
1 3
4 3
5 4
1 4
3 7
4 7
3 2
5 2
 5 4 1
4 1
 3 4 12
2 3
3 2
2 2
2 1
1 2
3 1
1 4
1 3
3 3
2 4
3 4
1 1
 3 3 3
1 1
2 2
3 3

8
 1
 6
 26
 4
 0
 20
 0
 2
 1
 28
 1
 9
 3

#include <bits/stdc++.h>
using namespace std;
struct item {
  int x, y, val;
  item(int x, int y, int val) : x(x), y(y), val(val) {}
};
bool operator<(item a, item b) {
  return a.val > b.val ||
         (a.val == b.val && (a.x < b.x || (a.x == b.x && a.y < b.y)));
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int maxn = 3000 + 5;
int r, c, n;
bool A[maxn][maxn], B[maxn][maxn], used[maxn][maxn];
pair<int, int> Q[maxn * maxn];
bool fl[maxn][maxn];
int D[maxn][maxn];
int x[maxn], y[maxn];
pair<int, int> getn(int x, int y, int i) {
  int sx = x + dx[i], sy = y + dy[i];
  if (sy < 0) {
    sy = c - 1;
  }
  if (sy >= c) {
    sy = 0;
  }
  return make_pair(sx, sy);
}
bool is_good(int m) {
  memset(A, false, sizeof(A));
  memset(used, false, sizeof(used));
  for (int i = 0; i <= m; ++i) {
    A[x[i]][y[i]] = true;
  }
  int l = 0, R = -1;
  for (int i = 0; i < c; ++i) {
    if (!A[0][i]) {
      Q[++R] = make_pair(0, i);
      used[0][i] = true;
    }
  }
  bool succ = false;
  while (l <= R) {
    int x = Q[l].first, y = Q[l].second;
    ++l;
    if (x == r - 1) {
      succ = true;
      break;
    }
    for (int i = 0; i < 4; ++i) {
      pair<int, int> pi = getn(x, y, i);
      int sx = pi.first, sy = pi.second;
      if (A[sx][sy] || used[sx][sy]) {
        continue;
      }
      Q[++R] = make_pair(sx, sy);
      used[sx][sy] = true;
    }
  }
  return succ;
}
void dfs0(int x, int y) {
  used[x][y] = true;
  fl[x][y] = true;
  for (int i = 0; i < 4; ++i) {
    pair<int, int> pi = getn(x, y, i);
    int sx = pi.first, sy = pi.second;
    if (used[sx][sy] || B[sx][sy] == -1 || sx < 0 || sx >= r) {
      continue;
    }
    dfs0(sx, sy);
  }
}
int dfs(int x, int y, int cnt, bool st) {
  if (x == 0 || x == r - 1) {
    return cnt;
  }
  used[x][y] = true;
  int rres = 0;
  while (true) {
    int fx = -1, fy = -1;
    int val = -1;
    for (int i = 0; i < 4; ++i) {
      pair<int, int> pi = getn(x, y, i);
      int sx = pi.first, sy = pi.second;
      if (used[sx][sy] || B[sx][sy] == -1 || sx < 0 || sx >= r) {
        continue;
      }
      if (B[sx][sy] > val && (fl[sx][sy] == st)) {
        val = B[sx][sy];
        fx = sx;
        fy = sy;
      }
    }
    if (fx == -1) return -1;
    int res = dfs(fx, fy, cnt + (B[fx][fy] < n), 0);
    if (res != -1) return res;
  }
  return rres;
}
int main() {
  scanf("%d%d%d", &r, &c, &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &x[i], &y[i]);
    --x[i], --y[i];
  }
  int l = 0, R = n;
  while (l + 1 < R) {
    int a = (l + R) / 2;
    if (is_good(a)) {
      l = a;
    } else {
      R = a;
    }
  }
  if (R == n) {
    cout << n << endl;
    return 0;
  }
  for (int i = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j) {
      B[i][j] = n;
    }
  }
  for (int i = 0; i <= R; ++i) {
    B[x[i]][y[i]] = -1;
  }
  for (int i = R + 1; i < n; ++i) {
    B[x[i]][y[i]] = i;
  }
  memset(used, false, sizeof(used));
  for (int i = 0; i < c; ++i) {
    if (B[0][i] != -1 && !used[0][i]) {
      dfs0(0, i);
    }
  }
  memset(used, false, sizeof(used));
  cout << n - 1 - dfs(x[R], y[R], 0, 1) - dfs(x[R], y[R], 0, 0) << endl;
  return 0;
}
