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 <cstdio>
#include <algorithm>
const	int	maxd = 3010;
const	int	maxn = 300010;
int	map[maxd][maxd], a[maxn], v[maxn];
int	r, c, n, t, lq, cnt, x, y, f;
struct	qs
{
	int	x, y;
	qs()	{}
	qs(int x, int y) : x(x), y(y) {}
}	q[8];
bool	operator<(qs a, qs b) {return a.x < b.x;}
inline	int	p(int x) {return x>1 ? x-1 : c;}
inline	int	s(int x) {return x<c ? x+1 : 1;}
inline	void	g(int x)
{
	int	t = x, t1, vv = 0;
	while (a[t] != t) vv += v[t], t = a[t];
	while (a[x] != t)
	{
		t1 = a[x];
		a[x] = t;
		vv -= v[x];
		v[x] += vv;
		x = t1;
	}
}
int	main()
{
	scanf("%d%d%d", &r, &c, &n);
	if (c == 1) {printf("0\n"); return 0;}
	for (int i=1; i<=n; i++) a[i] = i;
	cnt = 0;
	for (int i=1; i<=n; i++)
	{
		scanf("%d%d", &x, &y); lq = 0;
		if (x > 1)
		{
			if (t = map[x-1][p(y)]) g(t), q[++lq] = qs(a[t], 1 - v[t]);
			if (t = map[x-1][y]) g(t), q[++lq] = qs(a[t], -v[t]);
			if (t = map[x-1][s(y)]) g(t), q[++lq] = qs(a[t], -1 - v[t]);
		}
		if (t = map[x][p(y)]) g(t), q[++lq] = qs(a[t], 1 - v[t]);
		if (t = map[x][s(y)]) g(t), q[++lq] = qs(a[t], -1 - v[t]);
		if (x < r)
		{
			if (t = map[x+1][p(y)]) g(t), q[++lq] = qs(a[t], 1 - v[t]);
			if (t = map[x+1][y]) g(t), q[++lq] = qs(a[t], -v[t]);
			if (t = map[x+1][s(y)]) g(t), q[++lq] = qs(a[t], -1 - v[t]);
		}
		std::sort(q + 1, q + lq + 1);
		f = 0;
		for (int j=1; j<lq; j++) if (q[j].x == q[j+1].x && q[j].y != q[j+1].y) {f = 1; break;}
		if (f) continue;
		map[x][y] = ++cnt;
		for (int j=1; j<=lq; j++) a[q[j].x] = cnt, v[q[j].x] = q[j].y;
	}
	printf("%d\n", cnt);
	return 0;
}