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>
int main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  int numRow, numCol, numQuery;
  std::cin >> numRow >> numCol >> numQuery;
  if (numCol == 1) {
    std::cout << "0\n";
    return 0;
  }
  struct Info {
    int component;
    int index;
  };
  std::vector<std::vector<Info>> info(numRow,
                                      std::vector<Info>(numCol, {-1, -1}));
  struct Point {
    int row, col;
  };
  std::vector<std::vector<Point>> components;
  int result{};
  for (int _ = 0; _ < numQuery; ++_) {
    struct Adjacent {
      int component;
      std::vector<Point> points;
      int index;
      bool operator<(Adjacent const& other) const {
        return component < other.component;
      }
      bool operator==(Adjacent const& other) const {
        return component == other.component;
      }
    };
    std::vector<Adjacent> adjacent;
    int row, col;
    std::cin >> row >> col;
    --row;
    --col;
    for (int row1 = std::max(0, row - 1); row1 < std::min(numRow, row + 2);
         ++row1)
      for (int col1 = col - 1; col1 < col + 2; ++col1)
        if (row1 != row or col1 != col) {
          int index = 0;
          auto col2 = col1;
          if (col2 < 0) {
            col2 += numCol;
            --index;
          } else if (col2 >= numCol) {
            col2 -= numCol;
            ++index;
          }
          if (auto const c = info[row1][col2]; c.component >= 0) {
            adjacent.push_back({c.component, {}, c.index - index});
          }
        }
    std::sort(begin(adjacent), end(adjacent));
    for (int index = 1; index < (int)adjacent.size(); ++index)
      if (adjacent[index - 1].component == adjacent[index].component and
          adjacent[index - 1].index != adjacent[index].index) {
        goto nextCell;
      }
    if (false) {
    nextCell:
      continue;
    }
    ++result;
    if (adjacent.empty()) {
      info[row][col] = {(int)components.size(), 0};
      components.push_back({{row, col}});
      continue;
    }
    adjacent.erase(std::unique(begin(adjacent), end(adjacent)), end(adjacent));
    for (auto& [component, points, index] : adjacent) {
      points = std::move(components[component]);
      assert(not points.empty());
    }
    {
      auto& tmp =
          *std::max_element(begin(adjacent), end(adjacent),
                            [&](Adjacent const& first, Adjacent const& sec) {
                              return first.points.size() < sec.points.size();
                            });
      if (&tmp != &adjacent.back()) std::swap(adjacent.back(), tmp);
    }
    auto main = std::move(adjacent.back());
    adjacent.pop_back();
    for (auto& cur : adjacent) {
      auto const deltaIndex = main.index - cur.index;
      auto const delta = numCol * deltaIndex;
      for (auto [row, col] : cur.points) {
        main.points.push_back({row, col + delta});
        auto col1 = col % numCol;
        if (col1 < 0) col1 += numCol;
        assert(info[row][col1].component == cur.component);
        info[row][col1].component = main.component;
        info[row][col1].index += deltaIndex;
      }
    }
    main.points.push_back({row, col});
    assert(info[row][col].component == -1);
    info[row][col] = {main.component, main.index};
    components[main.component] = std::move(main.points);
  }
  std::cout << result << '\n';
}
