#include #include #include using namespace std; typedef struct{ int x; int y; }dir; const dir D[]={ {0,-1},{0,1},{-1,0},{1,0} }; int main(){ int W,H,N; scanf("%d%d%d",&W,&H,&N); vector >M(H*2+1); for(auto &e:M)e.resize(W*2+1); for(;N--;){ int n,x,y; scanf("%d%d",&n,&x); for(int i=0;i0?y-x:x-y; if(k0)M[cy][cx+1]++,M[cy][cx+k*2+1]--; else M[cy][cx-k*2+1]++,M[cy][cx+1]--; }else{ k/=W; if(y-x>0)M[cy+1][cx]++,M[cy+k*2+1][cx]--; else M[cy-k*2+1][cx]++,M[cy+1][cx]--; } } } for(int i=1;i q; for(q.push({0,0});!q.empty();){ dir cur=q.front();q.pop(); if(W*2+H*2-4-cur.x-cur.y==0)return !printf("%d\n",M[cur.y][cur.x]-1); for(auto &d:D){ if(0<=cur.x+d.x&&cur.x+d.x