#include<cstdio>
#include<vector>
#include<cassert>
#include<map>

using namespace std;

struct BIT {
  int N;
  vector<int> B;
  BIT(int n = 0) {
    N = n;
    B.resize(n+2, 0);
  }
  void add(int x, int v) {
    x++;
    while(x <= N) {
      B[x] += v;
      x += x & -x;
    }
  }
  int sum(int x) {
    int s = 0;
    x++;
    while(x > 0) {
      s += B[x];
      x -= x & -x;
    }
    return s;
  }
  int sum(int a, int b) {
    return sum(b)-sum(a-1);
  }
};

int main()
{
  int h, m, n;
  scanf("%d%d%d", &h, &m, &n);

  long long sol = 0;
  map<int, pair<int, int> > pos;

  vector<BIT> X;
  vector<bool> visited(h, false);
  vector<int> ai(h), bi(h);
  for(int i=0; i<h; ++i) {
    if(!visited[i]) {
      int cnt = 0;
      for(int j=i; !visited[j]; j=(j+m)%h) {
	visited[j] = true;
	ai[j] = X.size(); bi[j] = cnt;
	cnt++;
      }
      X.push_back(BIT(cnt));
    }
  }

  for(int i=0; i<n; ++i) {
    char op[8];
    scanf("%s", op);

    int id, hs;
    if(op[0] == '-') {
      scanf("%d", &id);
      X[pos[id].first].add(pos[id].second, -1);
    } else {
      scanf("%d%d", &id, &hs);

      int xi = ai[hs], ti = bi[hs];
      assert(X[xi].sum(0, X[xi].N-1) < X[xi].N);

      if(X[xi].sum(ti, X[xi].N-1) == X[xi].N-ti) {
	int lo = -1, hi = ti-1;
	sol += X[xi].N - ti;
	while(hi-lo > 1) {
	  int mid = (hi+lo) / 2;
	  if(X[xi].sum(0, mid) == mid+1) lo = mid;
	  else hi = mid;
	}
	sol += hi;
	X[xi].add(hi, 1);
	pos[id] = make_pair(xi, hi);
      } else {
	int lo = ti-1, hi = X[xi].N-1;
	while(hi-lo > 1) {
	  int mid = (hi+lo) / 2;
	  if(X[xi].sum(ti, mid) == mid-ti+1) lo = mid;
	  else hi = mid;
	}
	sol += hi - ti;
	X[xi].add(hi, 1);
	pos[id] = make_pair(xi, hi);
      }
    }
  }

  printf("%I64d\n", sol);
  return 0;
}


#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>

using namespace std;

inline void getc(char & c)
{
  c = ' ';
  while (c != '+' && c != '-') {
    c = getchar();
  }
}

inline void getd(int & d)
{
  d = 0;
  char c = ' ';
  while (c < '0' || c > '9') {
    c = getchar();
  }

  while ('0' <= c && c <= '9') {
    d *= 10;
    d += c - '0';
    c = getchar();
  }
}

typedef long long ll;

const int maxh = 200 * 1000 + 5;

map <int, int> M;
bool A[maxh];
int sq[maxh];

int h, m, n;
ll shft;
ll shftm;

ll add(int hash, int & nhash)
{
  ll res = 0;
  while (A[hash]) {
    if (sq[hash] == shft) {
      hash += shftm;
      if (hash >= h) {
        hash -= h;
      }
      res += shft;
    }
    else {
      hash += m;
      if (hash >= h) {
        hash -= h;
      }
      ++res;
    }
  }

  A[hash] = 1;
  nhash = hash;

  for (int i = 0; i < shft; ++i) {
    ++sq[hash];
    hash -= m;
    if (hash < 0) {
      hash += h;
    }
  }

  return res;
}

void del(int hash)
{
  A[hash] = 0;

  for (int i = 0; i < shft; ++i) {
    --sq[hash];
    hash -= m;
    if (hash < 0) {
      hash += h;
    }
  }
}

int main()
{
  //freopen("C.in", "r", stdin);
  ios::sync_with_stdio(0);
  //cin >> h >> m >> n;
  scanf("%d %d %d", &h, &m, &n);

  shft = sqrt(h) / 2;
  shft = max(shft, 1LL);
  shft = min(shft, (ll)h);

  shftm = (shft * m) % h;

  ll res = 0;
  for (int i = 0; i < n; ++i) {
    char op;
    int id;
    //cin >> op >> id;
    getc(op);
    //scanf("%d", &id);
    getd(id);
    if (op == '+') {
      int hash;
      //cin >> hash;
      //scanf("%d", &hash);
      getd(hash);

      int nhash;
      res += add(hash, nhash);
      M[id] = nhash;
    }
    else {
      int hash = M[id];
      del(hash);
    }
  }

  cout << res << '\n';

  return 0;
}


#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<map>
#define N 250000
using namespace std;
map <int,int> id;
map <int,int> q[N];
long long ans;
int i,j,k,l,n,m,h,be[N],X[N],Y[N],num,pos[N],M,x,y;
char ch[10];
int main(){
	scanf("%d%d%d",&h,&m,&n);
	for(int i=0;i<h;++i)if(!be[i]){
		int j=i;
		num=0;
		++M;
		while(!be[j]){
			num++;
			be[j]=M;
			pos[j]=num;
			j=(j+m)%h;
			q[M][num]=1;
		}
	}
	for(int i=1;i<=n;++i){
		scanf("%s",ch);
		if(ch[0]=='+'){
			scanf("%d%d",&x,&y);
			int res,tmp;
			map <int,int> :: iterator it=q[be[y]].lower_bound(pos[y]);
			if(it==q[be[y]].end())it=q[be[y]].lower_bound(0);
			tmp=it->first;
			if(tmp<pos[y])res=num-pos[y]+tmp;
			else res=tmp-pos[y];
			ans+=res;

			id[x]=i;
			q[be[y]].erase(it);
			X[i]=be[y];
			Y[i]=tmp;
		}else{
			scanf("%d",&x);
			y=id[x];
			q[X[y]][Y[y]]=1;
		}
	}
	printf("%I64d\n",ans);
}


#include <cstdio>
#include <map>

using namespace std;

typedef long long int64;
const int N = 200000 + 10;
map<int, int> A;
int pos[N], start[N], end[N];
int c[N];
int n;

void gen_pos(int m) {
  int last = 1;
  for (int i = 0; i < n; ++i) {
    int k = last;
    for (int j = i; !pos[j]; j = (j + m) % n) {
      pos[j] = last++;
      start[j] = k;
    }
    end[k] = last - 1;
  }
}

void seg_modify(int x, int y) {
  for (; x <= n; x += x & -x) {
    c[x] += y;
  }
}

int seg_sum(int x) {
  int ret = 0;
  for (; x; x -= x & -x) {
    ret += c[x];
  }
  return ret;
}

int seg_find_idx(int sum) {
  int idx = 0;
  int step = 1;
  while (step <= n) step <<= 1;
  step >>= 1;
  while (step && idx < n) {
    int next_idx = idx + step;
    if (next_idx <= n && sum >= c[next_idx]) {
      idx = next_idx;
      sum -= c[next_idx];
    }
    step >>= 1;
  }
  return sum ? -1 : idx;
}

int main() {
  int m, L;
  scanf("%d %d %d", &n, &m, &L);
  gen_pos(m);
  for (int i = 1; i <= n; ++i) seg_modify(i, 1);
  char op[3];
  int64 ret = 0;
  while (L--) {
    int x, y;
    scanf("%s", op);
    if (op[0] == '+') {
      scanf("%d %d", &x, &y);
      int u = start[y], v = end[u];
//      printf("position: %d %d %d\n", pos[y], u, v);
      int sum = seg_sum(pos[y] - 1);
      int idx = seg_find_idx(sum) + 1;
//      printf("sum, idx: %d %d\n", sum, idx);
      if (idx > 0 && idx <= v) {
        seg_modify(idx, -1);
        A[x] = idx;
        ret += idx - pos[y];
//        printf("no wrap: %d, place %d\n", idx - pos[y], idx);
      } else {
        sum = seg_sum(u - 1);
        idx = seg_find_idx(sum) + 1;
        seg_modify(idx, -1);
        A[x] = idx;
        ret += (v - pos[y]) + (idx - u) + 1;
//        printf("wrap: %d, place %d\n", idx - u + v - pos[y] + 1, idx);
      }
    } else {
      scanf("%d", &x);
      seg_modify(A[x], 1);
    }
  }
  printf("%I64d\n", ret);
  return 0;
}


#include <cstdio>
#include <map>
using namespace std;

typedef pair<int, int> P;
const int SIZE = 100009;// * 20;
int sum[SIZE * 20];

int query(int base, int idx, int l, int r, int s, int e)
{
    if(sum[base + idx] == (r - l))
	return -1;
    else if(!sum[base + idx])
        return max(l, s);
    else
    {
	int m = (l + r) / 2;

	if(s < m)
	{
	    int t = query(base, idx * 2, l, m, s, e);
	    if(t != -1)
		return t;
	}

	return query(base, idx * 2 + 1, m, r, s, e);
    }
}

void update(int base, int idx, int l, int r, int pos, int v)
{
    if(r - l == 1)
	sum[base + idx] += v;
    else
    {
        int m = (l + r) / 2;
	if(pos < m)
	    update(base, idx * 2, l, m, pos, v);
	else
	    update(base, idx * 2 + 1, m, r, pos, v);

	sum[base + idx] += v;
    }
}

int gcd(int a, int b)
{
    if(b)
	return gcd(b, a % b);
    else
	return a;
}

int main()
{
    int H, M, N;
    scanf("%d%d%d", &H, &M, &N);

    int k = gcd(H, M);

    map<int, P> table;

    int gap = N / k * 5;

    long long int ans = 0;
    while(N--)
    {
	char c;
	scanf(" %c", &c);
	if(c == '+')
	{
	    int id, h;
	    scanf("%d%d", &id, &h);

	    int t = h % k;
	    int pos = h / k;

	    int dum = 0;
	    int res = query(gap * t, 1, 0, H / k, pos, H / k);

	    if(res != -1)
		dum = res - pos;
	    else
	    {
                res = query(gap * t, 1, 0, H / k, 0, H / k);
		dum = H / k - pos + res;
	    }

	    update(gap * t, 1, 0, H / k, res, 1);
	    table[id] = P(gap * t, res);
	    ans += dum;
	}
	else
	{
	    int id;
	    scanf("%d", &id);

	    P p = table[id];
	    update(p.first, 1, 0, H / k, p.second, -1);
	}
    }

    printf("%I64d\n", ans);

    return 0;
}


#include <cstdio>
#include <map>
using namespace std;

typedef pair<int, int> P;
const int SIZE = 100009;// * 20;
int sum[SIZE * 30];

int query(int base, int idx, int l, int r, int s, int e)
{
    if(sum[base + idx] == (r - l))
	return -1;
    else if(!sum[base + idx])
        return max(l, s);
    else
    {
	int m = (l + r) / 2;

	if(s < m)
	{
	    int t = query(base, idx * 2, l, m, s, e);
	    if(t != -1)
		return t;
	}

	return query(base, idx * 2 + 1, m, r, s, e);
    }
}

void update(int base, int idx, int l, int r, int pos, int v)
{
    if(r - l == 1)
	sum[base + idx] += v;
    else
    {
        int m = (l + r) / 2;
	if(pos < m)
	    update(base, idx * 2, l, m, pos, v);
	else
	    update(base, idx * 2 + 1, m, r, pos, v);

	sum[base + idx] += v;
    }
}

int gcd(int a, int b)
{
    if(b)
	return gcd(b, a % b);
    else
	return a;
}

P table2[300009];

int main()
{
    int H, M, N;
    scanf("%d%d%d", &H, &M, &N);

    int k = gcd(H, M);

    map<int, P> table;

    int gap = H / k * 10;

    long long int ans = 0;

    for(int i = 0; i < k; i++)
	for(int j = 0, v = i; j < H / k; j++, v = (v + M) % H)
	    table2[v] = P(i, j);

    while(N--)
    {
	char c;
	scanf(" %c", &c);
	if(c == '+')
	{
	    int id, h;
	    scanf("%d%d", &id, &h);

	    int t = table2[h].first;
	    int pos = table2[h].second;

	    int dum = 0;
	    int res = query(gap * t, 1, 0, H / k, pos, H / k);

	    if(res != -1)
		dum = res - pos;
	    else
	    {
                res = query(gap * t, 1, 0, H / k, 0, H / k);
		dum = H / k - pos + res;
	    }

	    update(gap * t, 1, 0, H / k, res, 1);
	    table[id] = P(gap * t, res);
	    ans += dum;
	}
	else
	{
	    int id;
	    scanf("%d", &id);

	    P p = table[id];
	    update(p.first, 1, 0, H / k, p.second, -1);
	}
    }

    printf("%I64d\n", ans);

    return 0;
}


#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<map>
#define N 250000
using namespace std;
map <int,int> id;
map <int,int> q[51000];
long long ans;
int i,j,k,l,n,m,h,be[N],X[N],Y[N],num,pos[N],M,x,y;
char ch[10];
int main(){
	scanf("%d%d%d",&h,&m,&n);
	for(int i=0;i<h;++i)if(!be[i]){
		int j=i;
		num=0;
		++M;
		while(!be[j]){
			num++;
			be[j]=M;
			pos[j]=num;
			j=(j+m)%h;
			q[M][num]=1;
		}
	}
	for(int i=1;i<=n;++i){
		scanf("%s",ch);
		if(ch[0]=='+'){
			scanf("%d%d",&x,&y);
			int res,tmp;
			map <int,int> :: iterator it=q[be[y]].lower_bound(pos[y]);
			if(it==q[be[y]].end())it=q[be[y]].lower_bound(0);
			tmp=it->first;
			if(tmp<pos[y])res=num-pos[y]+tmp;
			else res=tmp-pos[y];
			ans+=res;

			id[x]=i;
			q[be[y]].erase(it);
			X[i]=be[y];
			Y[i]=tmp;
		}else{
			scanf("%d",&x);
			y=id[x];
			q[X[y]][Y[y]]=1;
		}
	}
	printf("%I64d\n",ans);
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<string>
#include<cmath>
#define pb push_back
#define mp make_pair
using namespace std;
typedef __int64 big;
const int N=200020;
map<int,int>in;
int belong[N],h[N],n,m,H,IN[N],TMP;
big ans=0;
bool ok;
bool vis[N];
int TOP=0;
struct node
{
	int l,r,sum;
	node *c[2];
}mem[N*2],*tr[N],*cur=mem;
vector<int>M[N];
vector<node*>p;
node *maketree(int l,int r)
{
	node *p=cur++;
	p->l=l;p->r=r;p->sum=0;
	if(l==r)return p;
	int mid=l+r>>1;
	p->c[0]=maketree(l,mid);
	p->c[1]=maketree(mid+1,r);
	return p;
}
inline void update(node *now)
{
	now->sum=now->c[0]->sum+now->c[1]->sum;
}
void ins(node *now,int x)
{
	if(now->l==now->r)
	{
		TMP=now->l;
		now->sum++;
		return ;
	}
	int mid=now->l+now->r>>1;
	ins(now->c[x>mid],x);
	update(now);
}
int query(node *now,int l,int r)
{
	if(now->l>=l&&now->r<=r)return now->sum;
	int mid=now->l+now->r>>1,Sum=0;
	if(l<=mid)Sum+=query(now->c[0],l,r);
	if(r>mid)Sum+=query(now->c[1],l,r);
	return Sum;
}
void del(node *now,int x)
{
	if(now->l==now->r)
	{
		now->sum=0;
		return ;
	}
	int mid=now->l+now->r>>1;
	del(now->c[x>mid],x);
	update(now);
}
void getp(node *now,int l,int r)
{
	if(now->l==l&&now->r==r)
	{
		p.pb(now);
		return ;
	}
	int mid=now->l+now->r>>1;
	if(l<=mid)getp(now->c[0],l,mid);
	if(r>mid)getp(now->c[1],mid+1,r);
}
void I(node *now)
{
	if(now->sum==now->r-now->l+1)return ;
	if(now->l==now->r)
	{
		TMP=now->l;
		ok=true;
		return ;
	}
	if(now->c[0]->sum<now->c[0]->r-now->c[0]->l+1)I(now->c[0]);
	else I(now->c[1]);
}
bool cmp(const node *a,const node *b){return a->l<b->l;}
void Ins(int id,int t)
{
	node *now=tr[belong[t]];
	int i,mid,s=IN[t],e=M[belong[t]].size();
	ok=false;p.clear();
	getp(now,s,e);
	//printf("%d\n",p.size());
	for(i=0;i<p.size();i++)
	{
		I(p[i]);
		if(ok)
		{
			ins(now,TMP);
			ans+=TMP-IN[t];
			break;
		}
	}
	if(!ok)
	{
		p.clear();
		getp(now,1,M[belong[t]].size());
		for(i=0;i<p.size();i++)
		{
			I(p[i]);
			if(ok)
			{
				ins(now,TMP);
				ans+=TMP;
				break;
			}
		}
	}
	in[id]=M[belong[t]][TMP-1];
}
void Del(int id)
{
	int tmp=in[id],K=IN[tmp];
	node *now=tr[belong[tmp]];
	del(now,K);
}
void init()
{
	int i,j;
	for(i=0;i<H;i++)
	if(!vis[i])
	{
		TOP++;
		for(j=i;!vis[j];j=(j+m)%H)
		{
			vis[j]=true;
			belong[j]=TOP;
			M[TOP].pb(j);
			IN[j]=M[TOP].size();
		}
		tr[TOP]=maketree(1,M[TOP].size());
	}
}
int main()
{
	int i,id,t;
	char cmd[3];
	scanf("%d%d%d",&H,&m,&n);
	init();
	while(n--)
	{
		scanf("%s",cmd);
		if(cmd[0]=='+')
		{
			scanf("%d%d",&id,&t);
			Ins(id,t);
		}
		else
		{
			scanf("%d",&id);
			Del(id);
		}
	}
	cout<<ans<<endl;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<string>
#include<cmath>
#define pb push_back
#define mp make_pair
using namespace std;
typedef __int64 big;
const int N=200020;
map<int,int>in;
int belong[N],h[N],n,m,H,IN[N],TMP;
big ans=0;
bool ok;
bool vis[N];
int TOP=0;
struct node
{
	int l,r,sum;
	node *c[2];
}mem[N*2],*tr[N],*cur=mem;
vector<int>M[N];
vector<node*>p;
node *maketree(int l,int r)
{
	node *p=cur++;
	p->l=l;p->r=r;p->sum=0;
	if(l==r)return p;
	int mid=l+r>>1;
	p->c[0]=maketree(l,mid);
	p->c[1]=maketree(mid+1,r);
	return p;
}
inline void update(node *now)
{
	now->sum=now->c[0]->sum+now->c[1]->sum;
}
void ins(node *now,int x)
{
	if(now->l==now->r)
	{
		TMP=now->l;
		now->sum++;
		return ;
	}
	int mid=now->l+now->r>>1;
	ins(now->c[x>mid],x);
	update(now);
}
int query(node *now,int l,int r)
{
	if(now->l>=l&&now->r<=r)return now->sum;
	int mid=now->l+now->r>>1,Sum=0;
	if(l<=mid)Sum+=query(now->c[0],l,r);
	if(r>mid)Sum+=query(now->c[1],l,r);
	return Sum;
}
void del(node *now,int x)
{
	if(now->l==now->r)
	{
		now->sum=0;
		return ;
	}
	int mid=now->l+now->r>>1;
	del(now->c[x>mid],x);
	update(now);
}
void getp(node *now,int l,int r)
{
	if(now->l>=l&&now->r<=r)
	{
		p.pb(now);
		return ;
	}
	int mid=now->l+now->r>>1;
	if(l<=mid)getp(now->c[0],l,r);
	if(r>mid)getp(now->c[1],l,r);
}
void I(node *now)
{
	if(now->sum==now->r-now->l+1)return ;
	if(now->l==now->r)
	{
		TMP=now->l;
		ok=true;
		return ;
	}
	if(now->c[0]->sum<now->c[0]->r-now->c[0]->l+1)I(now->c[0]);
	else I(now->c[1]);
}
bool cmp(const node *a,const node *b){return a->l<b->l;}
void Ins(int id,int t)
{
	node *now=tr[belong[t]];
	int i,mid,s=IN[t],e=M[belong[t]].size();
	ok=false;p.clear();
	getp(now,s,e);
	//printf("%d\n",p.size());
	sort(p.begin(),p.end(),cmp);
	for(i=0;i<p.size();i++)
	{
		I(p[i]);
		if(ok)
		{
			ins(now,TMP);
			ans+=TMP-IN[t];
			break;
		}
	}
	if(!ok)
	{
		p.clear();
		getp(now,1,M[belong[t]].size());
		sort(p.begin(),p.end(),cmp);
		for(i=0;i<p.size();i++)
		{
			I(p[i]);
			if(ok)
			{
				ins(now,TMP);
				ans+=TMP;
				break;
			}
		}
	}
	in[id]=M[belong[t]][TMP-1];
}
void Del(int id)
{
	int tmp=in[id],K=IN[tmp];
	node *now=tr[belong[tmp]];
	del(now,K);
}
void init()
{
	int i,j;
	for(i=0;i<H;i++)
	if(!vis[i])
	{
		TOP++;
		for(j=i;!vis[j];j=(j+m)%H)
		{
			vis[j]=true;
			belong[j]=TOP;
			M[TOP].pb(j);
			IN[j]=M[TOP].size();
		}
		tr[TOP]=maketree(1,M[TOP].size());
	}
}
int main()
{
	int i,id,t;
	char cmd[3];
	scanf("%d%d%d",&H,&m,&n);
	init();
	while(n--)
	{
		scanf("%s",cmd);
		if(cmd[0]=='+')
		{
			scanf("%d%d",&id,&t);
			Ins(id,t);
		}
		else
		{
			scanf("%d",&id);
			Del(id);
		}
	}
	cout<<ans<<endl;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<string>
#include<cmath>
#define pb push_back
#define mp make_pair
using namespace std;
typedef __int64 big;
const int N=200020;
map<int,int>in;
int belong[N],h[N],n,m,H,IN[N],TMP;
big ans=0;
bool ok;
bool vis[N];
int TOP=0;
struct node
{
	int l,r,sum;
	node *c[2];
}mem[N*2],*tr[N],*cur=mem;
vector<int>M[N];
vector<node*>p;
node *maketree(int l,int r)
{
	node *p=cur++;
	p->l=l;p->r=r;p->sum=0;
	if(l==r)return p;
	int mid=l+r>>1;
	p->c[0]=maketree(l,mid);
	p->c[1]=maketree(mid+1,r);
	return p;
}
inline void update(node *now)
{
	now->sum=now->c[0]->sum+now->c[1]->sum;
}
void ins(node *now,int x)
{
	if(now->l==now->r)
	{
		TMP=now->l;
		now->sum++;
		return ;
	}
	int mid=now->l+now->r>>1;
	ins(now->c[x>mid],x);
	update(now);
}
int query(node *now,int l,int r)
{
	if(now->l>=l&&now->r<=r)return now->sum;
	int mid=now->l+now->r>>1,Sum=0;
	if(l<=mid)Sum+=query(now->c[0],l,r);
	if(r>mid)Sum+=query(now->c[1],l,r);
	return Sum;
}
void del(node *now,int x)
{
	if(now->l==now->r)
	{
		now->sum=0;
		return ;
	}
	int mid=now->l+now->r>>1;
	del(now->c[x>mid],x);
	update(now);
}
void getp(node *now,int l,int r)
{
	if(now->l==l&&now->r==r)
	{
		p.pb(now);
		return ;
	}
	int mid=now->l+now->r>>1;
	if(l<=mid)getp(now->c[0],l,mid);
	if(r>mid)getp(now->c[1],mid+1,r);
}
void I(node *now)
{
	if(now->sum==now->r-now->l+1)return ;
	if(now->l==now->r)
	{
		TMP=now->l;
		ok=true;
		return ;
	}
	if(now->c[0]->sum<now->c[0]->r-now->c[0]->l+1)I(now->c[0]);
	else I(now->c[1]);
}
bool cmp(const node *a,const node *b){return a->l<b->l;}
void Ins(int id,int t)
{
	node *now=tr[belong[t]];
	int i,mid,s=IN[t],e=M[belong[t]].size();
	ok=false;p.clear();
	getp(now,s,e);
	//printf("%d\n",p.size());
	for(i=0;i<p.size();i++)
	{
		I(p[i]);
		if(ok)
		{
			ins(now,TMP);
			ans+=TMP-IN[t];
			break;
		}
	}
	if(!ok)
	{
		p.clear();ans+=e-IN[t];
		getp(now,1,M[belong[t]].size());
		for(i=0;i<p.size();i++)
		{
			I(p[i]);
			if(ok)
			{
				ins(now,TMP);
				ans+=TMP;
				break;
			}
		}
	}
	in[id]=M[belong[t]][TMP-1];
}
void Del(int id)
{
	int tmp=in[id],K=IN[tmp];
	node *now=tr[belong[tmp]];
	del(now,K);
}
void init()
{
	int i,j;
	for(i=0;i<H;i++)
	if(!vis[i])
	{
		TOP++;
		for(j=i;!vis[j];j=(j+m)%H)
		{
			vis[j]=true;
			belong[j]=TOP;
			M[TOP].pb(j);
			IN[j]=M[TOP].size();
		}
		tr[TOP]=maketree(1,M[TOP].size());
	}
}
int main()
{
	int i,id,t;
	char cmd[3];
	scanf("%d%d%d",&H,&m,&n);
	init();
	while(n--)
	{
		scanf("%s",cmd);
		if(cmd[0]=='+')
		{
			scanf("%d%d",&id,&t);
			Ins(id,t);
		}
		else
		{
			scanf("%d",&id);
			Del(id);
		}
	}
	cout<<ans<<endl;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<string>
#include<cmath>
#define pb push_back
#define mp make_pair
using namespace std;
typedef __int64 big;
const int N=200020;
map<int,int>in;
int belong[N],h[N],n,m,H,IN[N],TMP;
big ans=0;
bool ok;
bool vis[N];
int TOP=0;
struct node
{
	int l,r,sum;
	node *c[2];
}mem[N*2],*tr[N],*cur=mem;
vector<int>M[N];
vector<node*>p;
node *maketree(int l,int r)
{
	node *p=cur++;
	p->l=l;p->r=r;p->sum=0;
	if(l==r)return p;
	int mid=l+r>>1;
	p->c[0]=maketree(l,mid);
	p->c[1]=maketree(mid+1,r);
	return p;
}
inline void update(node *now)
{
	now->sum=now->c[0]->sum+now->c[1]->sum;
}
void ins(node *now,int x)
{
	if(now->l==now->r)
	{
		TMP=now->l;
		now->sum++;
		return ;
	}
	int mid=now->l+now->r>>1;
	ins(now->c[x>mid],x);
	update(now);
}
int query(node *now,int l,int r)
{
	if(now->l>=l&&now->r<=r)return now->sum;
	int mid=now->l+now->r>>1,Sum=0;
	if(l<=mid)Sum+=query(now->c[0],l,r);
	if(r>mid)Sum+=query(now->c[1],l,r);
	return Sum;
}
void del(node *now,int x)
{
	if(now->l==now->r)
	{
		now->sum=0;
		return ;
	}
	int mid=now->l+now->r>>1;
	del(now->c[x>mid],x);
	update(now);
}
void getp(node *now,int l,int r)
{
	if(now->l==l&&now->r==r)
	{
		p.pb(now);
		return ;
	}
	int mid=now->l+now->r>>1;
	if(l<=mid)getp(now->c[0],l,mid);
	if(r>mid)getp(now->c[1],mid+1,r);
}
void I(node *now)
{
	if(now->sum==now->r-now->l+1)return ;
	if(now->l==now->r)
	{
		TMP=now->l;
		ok=true;
		return ;
	}
	if(now->c[0]->sum<now->c[0]->r-now->c[0]->l+1)I(now->c[0]);
	else I(now->c[1]);
}
bool cmp(const node *a,const node *b){return a->l<b->l;}
void Ins(int id,int t)
{
	node *now=tr[belong[t]];
	int i,mid,s=IN[t],e=M[belong[t]].size();
	ok=false;p.clear();
	getp(now,s,e);
	//printf("%d\n",p.size());
	for(i=0;i<p.size();i++)
	{
		I(p[i]);
		if(ok)
		{
			ins(now,TMP);
			ans+=TMP-IN[t];
			break;
		}
	}
	if(!ok)
	{
		p.clear();ans+=TMP-IN[t];
		getp(now,1,M[belong[t]].size());
		for(i=0;i<p.size();i++)
		{
			I(p[i]);
			if(ok)
			{
				ins(now,TMP);
				ans+=TMP;
				break;
			}
		}
	}
	in[id]=M[belong[t]][TMP-1];
}
void Del(int id)
{
	int tmp=in[id],K=IN[tmp];
	node *now=tr[belong[tmp]];
	del(now,K);
}
void init()
{
	int i,j;
	for(i=0;i<H;i++)
	if(!vis[i])
	{
		TOP++;
		for(j=i;!vis[j];j=(j+m)%H)
		{
			vis[j]=true;
			belong[j]=TOP;
			M[TOP].pb(j);
			IN[j]=M[TOP].size();
		}
		tr[TOP]=maketree(1,M[TOP].size());
	}
}
int main()
{
	int i,id,t;
	char cmd[3];
	scanf("%d%d%d",&H,&m,&n);
	init();
	while(n--)
	{
		scanf("%s",cmd);
		if(cmd[0]=='+')
		{
			scanf("%d%d",&id,&t);
			Ins(id,t);
		}
		else
		{
			scanf("%d",&id);
			Del(id);
		}
	}
	cout<<ans<<endl;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<string>
#include<cmath>
#define pb push_back
#define mp make_pair
using namespace std;
typedef __int64 big;
const int N=200020;
map<int,int>in;
int belong[N],h[N],n,m,H,IN[N],TMP;
big ans=0;
bool ok;
bool vis[N];
int TOP=0;
struct node
{
	int l,r,sum;
	node *c[2];
}mem[N*2],*tr[N],*cur=mem;
vector<int>M[N];
node *maketree(int l,int r)
{
	node *p=cur++;
	p->l=l;p->r=r;p->sum=0;
	if(l==r)return p;
	int mid=l+r>>1;
	p->c[0]=maketree(l,mid);
	p->c[1]=maketree(mid+1,r);
	return p;
}
inline void update(node *now)
{
	now->sum=now->c[0]->sum+now->c[1]->sum;
}
void ins(node *now,int x)
{
	if(now->l==now->r)
	{
		TMP=now->l;
		now->sum++;
		return ;
	}
	int mid=now->l+now->r>>1;
	ins(now->c[x>mid],x);
	update(now);
}
int query(node *now,int l,int r)
{
	if(now->l>=l&&now->r<=r)return now->sum;
	int mid=now->l+now->r>>1,Sum=0;
	if(l<=mid)Sum+=query(now->c[0],l,r);
	if(r>mid)Sum+=query(now->c[1],l,r);
	return Sum;
}
void del(node *now,int x)
{
	if(now->l==now->r)
	{
		now->sum=0;
		return ;
	}
	int mid=now->l+now->r>>1;
	del(now->c[x>mid],x);
	update(now);
}
void Ins(int id,int t)
{
	static node *now;now=tr[belong[t]];
	static int mid,s,e;mid,s=IN[t],e=M[belong[t]].size();
	while(s<=e)
	{
		mid=s+e>>1;
		if(mid-IN[t]+1>query(now,IN[t],mid))e=mid-1;
		else s=mid+1;
	}
	if(s<=M[belong[t]].size())ins(now,s),ans+=s-IN[t];
	else
	{
		ans+=M[belong[t]].size()-IN[t];
		s=1,e=IN[t]-1;;
		while(s<=e)
		{
			mid=s+e>>1;
			if(mid>query(now,1,mid))e=mid-1;
			else s=mid+1;
		}
		ans+=s;ins(now,s);
	}
	in[id]=M[belong[t]][TMP-1];
}
void Del(int id)
{
	int tmp=in[id],K=IN[tmp];
	node *now=tr[belong[tmp]];
	del(now,K);
}
void init()
{
	int i,j;
	for(i=0;i<H;i++)
	if(!vis[i])
	{
		TOP++;
		for(j=i;!vis[j];j=(j+m)%H)
		{
			vis[j]=true;
			belong[j]=TOP;
			M[TOP].pb(j);
			IN[j]=M[TOP].size();
		}
		tr[TOP]=maketree(1,M[TOP].size());
	}
}
int main()
{
	int i,id,t;
	char cmd[3];
	scanf("%d%d%d",&H,&m,&n);
	init();
	while(n--)
	{
		scanf("%s",cmd);
		if(cmd[0]=='+')
		{
			scanf("%d%d",&id,&t);
			Ins(id,t);
		}
		else
		{
			scanf("%d",&id);
			Del(id);
		}
	}
	cout<<ans<<endl;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<string>
#include<cmath>
#define pb push_back
#define mp make_pair
using namespace std;
typedef __int64 big;
const int N=200020;
map<int,int>in;
int belong[N],h[N],n,m,H,IN[N],TMP;
big ans=0;
bool ok;
bool vis[N];
int TOP=0;
struct node
{
	int l,r,sum;
	node *c[2];
}mem[N*2],*tr[N],*cur=mem;
vector<int>M[N];
vector<node*>p;
node *maketree(int l,int r)
{
	node *p=cur++;
	p->l=l;p->r=r;p->sum=0;
	if(l==r)return p;
	int mid=l+r>>1;
	p->c[0]=maketree(l,mid);
	p->c[1]=maketree(mid+1,r);
	return p;
}
inline void update(node *now)
{
	now->sum=now->c[0]->sum+now->c[1]->sum;
}
void ins(node *now,int x)
{
	if(now->l==now->r)
	{
		TMP=now->l;
		now->sum++;
		return ;
	}
	int mid=now->l+now->r>>1;
	ins(now->c[x>mid],x);
	update(now);
}
int query(node *now,int l,int r)
{
	if(now->l>=l&&now->r<=r)return now->sum;
	int mid=now->l+now->r>>1,Sum=0;
	if(l<=mid)Sum+=query(now->c[0],l,r);
	if(r>mid)Sum+=query(now->c[1],l,r);
	return Sum;
}
void del(node *now,int x)
{
	if(now->l==now->r)
	{
		now->sum=0;
		return ;
	}
	int mid=now->l+now->r>>1;
	del(now->c[x>mid],x);
	update(now);
}
void getp(node *now,int l,int r)
{
	if(now->l>=l&&now->r<=r)
	{
		p.pb(now);
		return ;
	}
	int mid=now->l+now->r>>1;
	if(l<=mid)getp(now->c[0],l,r);
	if(r>mid)getp(now->c[1],l,r);
}
void I(node *now)
{
	if(now->sum==now->r-now->l+1)return ;
	if(now->l==now->r)
	{
		TMP=now->l;
		ok=true;
		return ;
	}
	if(now->c[0]->sum<now->c[0]->r-now->c[0]->l+1)I(now->c[0]);
	else I(now->c[1]);
}
bool cmp(const node *a,const node *b){return a->l<b->l;}
void Ins(int id,int t)
{
	node *now=tr[belong[t]];
	int i,mid,s=IN[t],e=M[belong[t]].size();
	ok=false;p.clear();
	getp(now,s,e);
	sort(p.begin(),p.end(),cmp);
	//printf("%d\n",p.size());
	for(i=0;i<p.size();i++)
	{
		I(p[i]);
		if(ok)
		{
			ins(now,TMP);
			ans+=TMP-IN[t];
			break;
		}
	}
	if(!ok)
	{
		p.clear();ans+=M[belong[t]].size()-IN[t];
		getp(now,1,M[belong[t]].size());
		sort(p.begin(),p.end(),cmp);
		for(i=0;i<p.size();i++)
		{
			I(p[i]);
			if(ok)
			{
				ins(now,TMP);
				ans+=TMP;
				break;
			}
		}
	}
	in[id]=M[belong[t]][TMP-1];
}
void Del(int id)
{
	int tmp=in[id],K=IN[tmp];
	node *now=tr[belong[tmp]];
	del(now,K);
}
void init()
{
	int i,j;
	for(i=0;i<H;i++)
	if(!vis[i])
	{
		TOP++;
		for(j=i;!vis[j];j=(j+m)%H)
		{
			vis[j]=true;
			belong[j]=TOP;
			M[TOP].pb(j);
			IN[j]=M[TOP].size();
		}
		tr[TOP]=maketree(1,M[TOP].size());
	}
}
int main()
{
	int i,id,t;
	char cmd[3];
	scanf("%d%d%d",&H,&m,&n);
	init();
	while(n--)
	{
		scanf("%s",cmd);
		if(cmd[0]=='+')
		{
			scanf("%d%d",&id,&t);
			Ins(id,t);
		}
		else
		{
			scanf("%d",&id);
			Del(id);
		}
	}
	cout<<ans<<endl;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<string>
#include<cmath>
#define pb push_back
#define mp make_pair
using namespace std;
typedef __int64 big;
const int N=200020;
map<int,int>in;
int belong[N],h[N],n,m,H,IN[N],TMP;
big ans=0;
bool ok;
bool vis[N];
int TOP=0;
struct node
{
	int l,r,sum;
	node *c[2];
}mem[N*2],*tr[N],*cur=mem;
vector<int>M[N];
vector<node*>p;
node *maketree(int l,int r)
{
	node *p=cur++;
	p->l=l;p->r=r;p->sum=0;
	if(l==r)return p;
	int mid=l+r>>1;
	p->c[0]=maketree(l,mid);
	p->c[1]=maketree(mid+1,r);
	return p;
}
inline void update(node *now)
{
	now->sum=now->c[0]->sum+now->c[1]->sum;
}
void ins(node *now,int x)
{
	if(now->l==now->r)
	{
		TMP=now->l;
		now->sum++;
		return ;
	}
	int mid=now->l+now->r>>1;
	ins(now->c[x>mid],x);
	update(now);
}
int query(node *now,int l,int r)
{
	if(now->l>=l&&now->r<=r)return now->sum;
	int mid=now->l+now->r>>1,Sum=0;
	if(l<=mid)Sum+=query(now->c[0],l,r);
	if(r>mid)Sum+=query(now->c[1],l,r);
	return Sum;
}
void del(node *now,int x)
{
	if(now->l==now->r)
	{
		now->sum=0;
		return ;
	}
	int mid=now->l+now->r>>1;
	del(now->c[x>mid],x);
	update(now);
}
void getp(node *now,int l,int r)
{
	if(now->l>=l&&now->r<=r)
	{
		p.pb(now);
		return ;
	}
	int mid=now->l+now->r>>1;
	if(l<=mid)getp(now->c[0],l,r);
	if(r>mid)getp(now->c[1],l,r);
}
void I(node *now)
{
	if(now->sum==now->r-now->l+1)
	{
		ok=false;
		return ;
	}
	if(now->l==now->r)
	{
		TMP=now->l;
		ok=true;
		return ;
	}
	if(now->c[0]->sum<now->c[0]->r-now->c[0]->l+1)I(now->c[0]);
	else I(now->c[1]);
}
void Ins(int id,int t)
{
	node *now=tr[belong[t]];
	int i,mid,s=IN[t],e=M[belong[t]].size();
	ok=false;p.clear();
	getp(now,s,e);
	for(i=0;i<p.size();i++)
	{
		I(p[i]);
		if(ok)
		{
			ins(now,TMP);
			ans+=TMP-IN[t];
			break;
		}
	}
	if(!ok)
	{
		p.clear();
		getp(now,1,M[belong[t]].size());
		for(i=0;i<p.size();i++)
		{
			I(p[i]);
			if(ok)
			{
				ins(now,TMP);
				ans+=TMP;
				break;
			}
		}
	}
	in[id]=M[belong[t]][TMP-1];
}
void Del(int id)
{
	int tmp=in[id],K=IN[tmp];
	node *now=tr[belong[tmp]];
	del(now,K);
}
void init()
{
	int i,j;
	for(i=0;i<H;i++)
	if(!vis[i])
	{
		TOP++;
		for(j=i;!vis[j];j=(j+m)%H)
		{
			vis[j]=true;
			belong[j]=TOP;
			M[TOP].pb(j);
			IN[j]=M[TOP].size();
		}
		tr[TOP]=maketree(1,M[TOP].size());
	}
}
int main()
{
	int i,id,t;
	char cmd[3];
	scanf("%d%d%d",&H,&m,&n);
	init();
	while(n--)
	{
		scanf("%s",cmd);
		if(cmd[0]=='+')
		{
			scanf("%d%d",&id,&t);
			Ins(id,t);
		}
		else
		{
			scanf("%d",&id);
			Del(id);
		}
	}
	cout<<ans<<endl;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<string>
#include<cmath>
#define pb push_back
#define mp make_pair
using namespace std;
typedef __int64 big;
const int N=200020;
map<int,int>in;
int belong[N],h[N],n,m,H,IN[N],TMP;
big ans=0;
bool ok;
bool vis[N];
int TOP=0;
struct node
{
	int l,r,sum;
	node *c[2];
}mem[N*2],*tr[N],*cur=mem;
vector<int>M[N];
node *maketree(int l,int r)
{
	node *p=cur++;
	p->l=l;p->r=r;p->sum=0;
	if(l==r)return p;
	int mid=l+r>>1;
	p->c[0]=maketree(l,mid);
	p->c[1]=maketree(mid+1,r);
	return p;
}
inline void update(node *now)
{
	now->sum=now->c[0]->sum+now->c[1]->sum;
}
void ins(node *now,int x)
{
	if(now->l==now->r)
	{
		TMP=now->l;
		now->sum++;
		return ;
	}
	int mid=now->l+now->r>>1;
	ins(now->c[x>mid],x);
	update(now);
}
int query(node *now,int l,int r)
{
	if(now->l>=l&&now->r<=r)return now->sum;
	int mid=now->l+now->r>>1,Sum=0;
	if(l<=mid)Sum+=query(now->c[0],l,r);
	if(r>mid)Sum+=query(now->c[1],l,r);
	return Sum;
}
void del(node *now,int x)
{
	if(now->l==now->r)
	{
		now->sum=0;
		return ;
	}
	int mid=now->l+now->r>>1;
	del(now->c[x>mid],x);
	update(now);
}
void Ins(int id,int t)
{
	node *now=tr[belong[t]];
	int mid,s=IN[t],e=M[belong[t]].size();
	while(s<=e)
	{
		mid=s+e>>1;
		if(mid-IN[t]+1>query(now,IN[t],mid))e=mid-1;
		else s=mid+1;
	}
	if(s<=M[belong[t]].size())ins(now,s),ans+=s-IN[t];
	else
	{
		ans+=M[belong[t]].size()-IN[t];
		s=1,e=M[belong[t]].size();
		while(s<=e)
		{
			mid=s+e>>1;
			if(mid>query(now,1,mid))e=mid-1;
			else s=mid+1;
		}
		ans+=s;ins(now,s);
	}
	in[id]=M[belong[t]][TMP-1];
}
void Del(int id)
{
	int tmp=in[id],K=IN[tmp];
	node *now=tr[belong[tmp]];
	del(now,K);
}
void init()
{
	int i,j;
	for(i=0;i<H;i++)
	if(!vis[i])
	{
		TOP++;
		for(j=i;!vis[j];j=(j+m)%H)
		{
			vis[j]=true;
			belong[j]=TOP;
			M[TOP].pb(j);
			IN[j]=M[TOP].size();
		}
		tr[TOP]=maketree(1,M[TOP].size());
	}
}
int main()
{
	int i,id,t;
	char cmd[3];
	scanf("%d%d%d",&H,&m,&n);
	init();
	while(n--)
	{
		scanf("%s",cmd);
		if(cmd[0]=='+')
		{
			scanf("%d%d",&id,&t);
			Ins(id,t);
		}
		else
		{
			scanf("%d",&id);
			Del(id);
		}
	}
	cout<<ans<<endl;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<string>
#include<cmath>
#define pb push_back
#define mp make_pair
using namespace std;
typedef __int64 big;
const int N=200020;
map<int,int>in;
int belong[N],h[N],n,m,H,IN[N],TMP;
big ans=0;
bool ok;
bool vis[N];
int TOP=0;
struct node
{
	int l,r,sum;
	node *c[2];
}mem[N*2],*tr[N],*cur=mem;
vector<int>M[N];
vector<node*>p;
node *maketree(int l,int r)
{
	node *p=cur++;
	p->l=l;p->r=r;p->sum=0;
	if(l==r)return p;
	int mid=l+r>>1;
	p->c[0]=maketree(l,mid);
	p->c[1]=maketree(mid+1,r);
	return p;
}
inline void update(node *now)
{
	now->sum=now->c[0]->sum+now->c[1]->sum;
}
void ins(node *now,int x)
{
	if(now->l==now->r)
	{
		TMP=now->l;
		now->sum++;
		return ;
	}
	int mid=now->l+now->r>>1;
	ins(now->c[x>mid],x);
	update(now);
}
int query(node *now,int l,int r)
{
	if(now->l>=l&&now->r<=r)return now->sum;
	int mid=now->l+now->r>>1,Sum=0;
	if(l<=mid)Sum+=query(now->c[0],l,r);
	if(r>mid)Sum+=query(now->c[1],l,r);
	return Sum;
}
void del(node *now,int x)
{
	if(now->l==now->r)
	{
		now->sum=0;
		return ;
	}
	int mid=now->l+now->r>>1;
	del(now->c[x>mid],x);
	update(now);
}
void getp(node *now,int l,int r)
{
	if(now->l==l&&now->r==r)
	{
		p.pb(now);
		return ;
	}
	int mid=now->l+now->r>>1;
	if(l<=mid)getp(now->c[0],l,mid);
	if(r>mid)getp(now->c[1],mid+1,r);
}
void I(node *now)
{
	if(now->sum==now->r-now->l+1)return ;
	if(now->l==now->r)
	{
		TMP=now->l;
		ok=true;
		return ;
	}
	if(now->c[0]->sum<now->c[0]->r-now->c[0]->l+1)I(now->c[0]);
	else I(now->c[1]);
}
bool cmp(const node *a,const node *b){return a->l<b->l;}
void Ins(int id,int t)
{
	node *now=tr[belong[t]];
	int i,mid,s=IN[t],e=M[belong[t]].size();
	ok=false;p.clear();
	getp(now,s,e);
	sort(p.begin(),p.end(),cmp);
	//printf("%d\n",p.size());
	for(i=0;i<p.size();i++)
	{
		I(p[i]);
		if(ok)
		{
			ins(now,TMP);
			ans+=TMP-IN[t];
			break;
		}
	}
	if(!ok)
	{
		p.clear();ans+=M[belong[t]].size()-IN[t];
		getp(now,1,M[belong[t]].size());
		sort(p.begin(),p.end(),cmp);
		for(i=0;i<p.size();i++)
		{
			I(p[i]);
			if(ok)
			{
				ins(now,TMP);
				ans+=TMP;
				break;
			}
		}
	}
	in[id]=M[belong[t]][TMP-1];
}
void Del(int id)
{
	int tmp=in[id],K=IN[tmp];
	node *now=tr[belong[tmp]];
	del(now,K);
}
void init()
{
	int i,j;
	for(i=0;i<H;i++)
	if(!vis[i])
	{
		TOP++;
		for(j=i;!vis[j];j=(j+m)%H)
		{
			vis[j]=true;
			belong[j]=TOP;
			M[TOP].pb(j);
			IN[j]=M[TOP].size();
		}
		tr[TOP]=maketree(1,M[TOP].size());
	}
}
int main()
{
	int i,id,t;
	char cmd[3];
	scanf("%d%d%d",&H,&m,&n);
	init();
	while(n--)
	{
		scanf("%s",cmd);
		if(cmd[0]=='+')
		{
			scanf("%d%d",&id,&t);
			Ins(id,t);
		}
		else
		{
			scanf("%d",&id);
			Del(id);
		}
	}
	cout<<ans<<endl;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;

int H, M, N;
set<int> a[200005];
int p[200005], seq[200005], r=0;
int u[200005];
map<int, pair<int, int> > history;
void release(pair<int, int> P) {
	a[P.first].insert(P.second);
}
int main(void) {
	scanf("%d%d%d", &H, &M, &N);
	for(int i=0;i<H;i++) {
		if(!u[i]) {
			for(int c=0, j=i;!u[j];j=(j+M)%H, ++c) {
				p[j] = r; seq[j] = c;
				a[r].insert(c);
				u[j] = 1;
			}
			++r;
		}
	}
	long long ans = 0;
	while(N--) {
		char x[10];
		int id, hash;
		scanf("%s%d", x, &id);
		if(x[0] == '-') release(history[id]);
		else {
			scanf("%d", &hash);
			hash %= H;
			int A = p[hash], B = seq[hash];
			set<int>::iterator it = a[A].lower_bound(B);
			if(it == a[A].end()) {
				it = a[A].begin();
				ans += *it + H/r - B;
			} else {
				ans += *it - B;
			}
			//printf("%d (%d, %d)\n", id, A, *it);
			history[id] = make_pair(A, *it);
			a[A].erase(it);
		}
	}
	printf("%I64d\n", ans);
	return 0;
}


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
#include <random>
#include <cassert>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MEM(a,b) memset((a),(b),sizeof(a))
const LL INF = 1e9 + 7;
const int N = 2e5 + 10;
class BIT
{
public:
	int a[N];
	void add(int x, int val)
	{
		while (x < N)
		{
			a[x] += val;
			x += x & -x;
		}
	}
	int sum(int x)
	{
		int ret = 0;
		while (x)
		{
			ret += a[x];
			x -= x & -x;
		}
		return ret;
	}
} b;
int L[N], R[N], pos[N];
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int h, m, q;
	scanf("%d%d%d", &h, &m, &q);
	int used = 0;
	for (int i = 0; i < h; i++)
	{
		if (pos[i]) continue;
		int l = used + 1;
		int r = used + 1;
		int cur = i;
		while (pos[cur] == 0)
		{
			pos[cur] = ++used;
			r = used;
			cur = (cur + m) % h;
		}
		for (int i = l; i <= r; i++)
			L[i] = l, R[i] = r;
	}
	map<int, int> mc;
	LL ans = 0;
	while (q--)
	{
		char op;
		int id, x;
		scanf(" %c %d", &op, &id);
		if (op == '-')
		{
			int o = mc[id];
			mc.erase(id);
			b.add(o, -1);
		}
		else
		{
			scanf("%d", &x);
			x = pos[x];
			int l = L[x], r = R[x];
			if (b.sum(r) - b.sum(x - 1) == r - x + 1)
			{
				ans += r - x + 1;
				x = l;
			}
			int l1 = x, r1 = r;
			int key = r;
			while (l1 <= r1)
			{
				int mid = (l1 + r1) / 2;
				if (b.sum(mid) - b.sum(x - 1) == mid - x + 1) l1 = mid + 1;
				else key = mid, r1 = mid - 1;
			}
			ans += key - x;
			mc[id] = key;
			b.add(key, 1);
		}
	}
	printf("%lld\n", ans);
	return 0;
}


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
#include <random>
#include <cassert>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MEM(a,b) memset((a),(b),sizeof(a))
const LL INF = 1e9 + 7;
const int N = 2e5 + 10;
class BIT
{
public:
	int a[N];
	void add(int x, int val)
	{
		while (x < N)
		{
			a[x] += val;
			x += x & -x;
		}
	}
	int sum(int x)
	{
		int ret = 0;
		while (x)
		{
			ret += a[x];
			x -= x & -x;
		}
		return ret;
	}
} b;
int L[N], R[N], pos[N];
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int h, m, q;
	scanf("%d%d%d", &h, &m, &q);
	int used = 0;
	for (int i = 0; i < h; i++)
	{
		if (pos[i]) continue;
		int l = used + 1;
		int r = used + 1;
		int cur = i;
		while (pos[cur] == 0)
		{
			pos[cur] = ++used;
			r = used;
			cur = (cur + m) % h;
		}
		for (int i = l; i <= r; i++)
			L[i] = l, R[i] = r;
	}
	map<int, int> mc;
	LL ans = 0;
	while (q--)
	{
		char op;
		int id, x;
		scanf(" %c %d", &op, &id);
		if (op == '-')
		{
			int o = mc[id];
			mc.erase(id);
			b.add(o, -1);
		}
		else
		{
			scanf("%d", &x);
			x = pos[x];
			int l = L[x], r = R[x];
			if (b.sum(r) - b.sum(x - 1) == r - x + 1)
			{
				x = l;
				ans += r - x + 1;
			}
			int l1 = x, r1 = r;
			int key = r;
			while (l1 <= r1)
			{
				int mid = (l1 + r1) / 2;
				if (b.sum(mid) - b.sum(x - 1) == mid - l + 1) l1 = mid + 1;
				else key = mid, r1 = mid - 1;
			}
			ans += key - x;
			mc[id] = key;
			b.add(key, 1);
		}
	}
	printf("%lld\n", ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define per(i, a, b) for(int i = (b) - 1; i >= (a); --i)
#define sz(a) (int)a.size()
#define de(c) cout << #c << " = " << c << endl
#define dd(c) cout << #c << " = " << c << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll<<(x))
#define endl "\n"
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 202020;

int h, m, n, L;
int num[N], id[N], l[N], r[N];
bool vis[N];
map<int, int> pos;

struct Seg {
#define ls rt << 1
#define rs ls | 1
	static const int N = ::N << 2;
	int cnt[N];
	void upd(int p, int c, int l, int r, int rt) {
		cnt[rt] += c;
		if(l == r) return ;
		int mid = l + r >> 1;
		if(p <= mid) upd(p, c, l, mid, ls);
		else upd(p, c, mid + 1, r, rs);
	}
	int qry(int L, int R, int l, int r, int rt) {
		if(L <= l && r <= R && (l == r || cnt[rt] == r - l + 1)) {
			if(cnt[rt] == r - l + 1) return 0;
			return l;
		}
		int mid = l + r >> 1;
		if(L <= mid) {
			int t = qry(L, R, l, mid, ls);
			if(t) return t;
		}
		if(R > mid) {
			int t = qry(L, R, mid + 1, r, rs);
			if(t) return t;
		}
		return 0;
	}
}seg;

int main() {
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	cin >> h >> m >> n;
	rep(i, 0, h) if(!vis[i]) {
		int st = L + 1;
		for(int j = i; !vis[j]; (j += m) %= h) num[j] = ++L, id[L] = j, vis[j] = 1;
		for(int j = i; !l[j]; (j += m) %= h) l[j] = st, r[j] = L;
	}
	ll ans = 0;
	rep(i, 0, n) {
		string o;
		int c, p;
		cin >> o >> c;
		if(o[0] == '+') {
			cin >> p;
			int t = seg.qry(num[p], r[p], 1, h, 1);
			if(t) ans += t - num[p];
			else t = seg.qry(l[p], num[p] - 1, 1, h, 1), ans += r[p] - num[p] + t - l[p] + 1;
			seg.upd(pos[c] = t, 1, 1, h, 1);
		} else {
			seg.upd(pos[c], -1, 1, h, 1);
			pos[c] = 0;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long lli;
typedef vector<lli> vi;
typedef vector< vi > graph;
#define EB push_back
#define endl "\n"
#define fr(i,n) for(int i=0;i<n;i++)
#define mset(a,n) memset(a,n,sizeof(a))
const lli MOD=1000000007;

lli tot;

struct SegT{
	#define mid ((l+r)>>1)
	int nn;
	vi t;
	SegT(){}
	SegT(int n){
		nn=n;
		t.resize(4*n);
	}
	void build(int id,int l,int r){
		if(l==r){
			t[id]=l;
			return;
		}
		build(id<<1,l,mid);
		build(id<<1|1,mid+1,r);
		t[id]=min(t[id<<1],t[id<<1|1]);
	}
	void update(int id,int l,int r,int pos){
		if(pos>r||pos<l)return;
		if(l==r){
			t[id]=(t[id]==nn?l:nn);
			return;
		}
		update(id<<1,l,mid,pos);
		update(id<<1|1,mid+1,r,pos);
		t[id]=min(t[id<<1],t[id<<1|1]);
	}
	int query(int id,int l,int r,int lq,int rq){
		if(lq>r||l>rq)return nn;
		else if(lq<=l&&r<=rq)return t[id];
		return min(query(id<<1,l,mid,lq,rq),query(id<<1|1,mid+1,r,lq,rq));
	}
	int work(int ll){
		int xx=query(1,0,nn-1,ll,nn-1);
		if(xx==nn){
			xx=query(1,0,nn-1,0,nn-1);
		}
		update(1,0,nn-1,xx);
		if(xx>=ll)tot+=xx-ll;
		else tot+=nn-(ll-xx);
		return xx;
	}
};

int h,m,n;
int cc=0;
int cnt[200200];
int root[200200];
int ssz[200200];
vector<SegT> sg;
map <int, pair< int,int > > sid;

void ins(int id,int hash){
	int comp=root[hash];
	int ll=cnt[hash];
	int ab=sg[comp].work(ll);
	sid[id]=make_pair(comp,ab);
}

void del(int id){
	pair<int,int> tt=sid[id];
	sid.erase(id);
	sg[tt.first].update(1,0,sg[tt.first].nn-1,tt.second);
}
void pr(string s,int x){
	cout<<s<<":"<<x<<endl;
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>h>>m>>n;
	mset(root,-1);
	for(int i=0;i<h;i++){
		if(root[i]==-1){
			int rr=i;
			int mm=0;
			while(root[rr]==-1){
				root[rr]=cc;
				cnt[rr]=mm;
				mm++;
				rr=(rr+m)%h;
			}
			ssz[cc]=mm;
			cc++;
		}
	}
	//pr("cc",cc);
	sg.resize(cc);
	fr(i,cc){
		sg[i]=SegT(ssz[i]);
		sg[i].build(1,0,ssz[i]-1);
	}
	fr(i,n){
		char ch;
		cin>>ch;
		if(ch=='+'){
			int id,hash;
			cin>>id>>hash;
			ins(id,hash);
		}else{
			int id;
			cin>>id;
			del(id);
		}
	}
	cout<<tot<<endl;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1234567;

int chain[N];
set<int> t[N];
map<int, int> pos;
vector<int> v[N];
int nom[N];

int main() {
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  int htot, m, n;
  scanf("%d%d%d", &htot, &m, &n);
  long long ans = 0;
  memset(chain, -1, sizeof(chain));
  int cc = 0;
  for (int i = 0; i < htot; i++) {
    if (chain[i] != -1) {
      continue;
    }
    int cid = 0;
    for (int j = i; chain[j] == -1; j = (j + m) % htot) {
      nom[j] = cid++;
      chain[j] = cc;
      t[cc].insert(nom[j]);
      v[cc].push_back(j);
    }
    cc++;
  }
  while (n--) {
    char op;
    int id;
    scanf(" %c%d", &op, &id);
    if (op == '-') {
      int x = pos[id];
      t[chain[x]].insert(nom[x]);
    } else {
      int h;
      scanf("%d", &h);
      auto it = t[chain[h]].lower_bound(nom[h]);
      if (it == t[chain[h]].end()) {
        it = t[chain[h]].begin();
      }
      int x = *it;
      pos[id] = v[chain[h]][x];
      ans += (nom[h] <= x ? x - nom[h] : (int) v[chain[h]].size() + x - nom[h]);
      t[chain[h]].erase(x);
    }
  }
  printf("%lld\n", ans);
  return 0;
}


#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define sz(a) ((int)(a).size())
#define rep(i, a, b) for(int (i) = (a); (i) < (b); (i)++)
#define dec(i, a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define clr(a,v) memset(a, v, sizeof(a))
#define all(a) (a).begin(),(a).end()
#define MAXN 202020
#define LOGN 20
#define EPS 1e-2
#define fcin ios_base::sync_with_stdio(false)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,ll> pii;

int n, h, m, id[MAXN], ciclo[MAXN], tam[MAXN], jav[MAXN];
set<pii> livre[MAXN];
unordered_map<int,int> pos;
ll ans;

void tira(int p){ livre[ciclo[p]].erase(mp(id[p],p));}
void bota(int p){ livre[ciclo[p]].insert(mp(id[p],p)); }

int main(){
	scanf("%d%d%d", &h, &m, &n);
	rep(i,0,h) if(!jav[i]){
		int p = i;
		rep(j,0,MAXN){
			int pr = (p+m)%h;
			tam[i]++;
			livre[i].insert(mp(j,p));
			ciclo[p] = i;
			id[p] = j;
			jav[p] = 1;
			p = pr;
			if(p==i) break;
		}
	}
	rep(i,0,n){
		char op;
		int v;
		scanf(" %c %d", &op, &v);
		if(op == '+'){
			int ha;
			scanf("%d", &ha);
			set<pii>::iterator it = livre[ciclo[ha]].lower_bound(mp(id[ha],-1));
			if(it == livre[ciclo[ha]].end()) it = livre[ciclo[ha]].begin();
			int p = it->y, d = id[p] - id[ha];
			if(d < 0) d += tam[ciclo[ha]];
			pos[v] = p;
			tira(p);
			ans += d;
		}else bota(pos[v]);
	}
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>
#define fr(a,b,c) for(int a=b;a<c;a++)
using namespace std;
typedef long long ll;
map<int,int> obj;
int rev[234567];
int rind[234567];
vector<vector<int>> fens;
int sz;
void bit_add(int bn,int pos, int val){
    pos++;
    while(pos<=sz){
        fens[bn][pos-1]+=val;
        pos+=pos&-pos;
    }
}

int bit_qry(int bn,int pos){
    pos++;
    int ret=0;
    while(pos){
        ret+=fens[bn][pos-1];
        pos-=pos&-pos;
    }
    return ret;
}

int qry(int w,int a,int b){
    int ret;
   if(a<=b){
       ret=bit_qry(w,b)-bit_qry(w,a-1);
   }else{
       ret=bit_qry(w,fens[w].size()-1)-bit_qry(w,a-1)+bit_qry(w,b);
   }
  // cout<<w<<" "<<a<<" "<<b<<" = "<<ret<<endl;
   return ret;
}
int main(){
    memset(rev,-1,sizeof rev);
    cin.tie(0);
    ios::sync_with_stdio(0);
    int h,m,n;
    cin>>h>>m>>n;
    int fenc=0;
    fr(i,0,h){
        if(rev[i]==-1){
            fens.push_back(vector<int>());
            fens[fenc].push_back(0);
            rind[i]=0;
            rev[i]=fenc;
            int i2=(i+m)%h;
            while(i2!=i){
                rind[i2]=fens[fenc].size();
                fens[fenc].push_back(0);
                rev[i2]=fenc;
                i2=(i2+m)%h;
            }
            fenc++;
        }
    }long long sans=0;
    sz=fens[0].size();
    fr(i,0,n){
        string s;
        cin>>s;
        if(s=="+"){
            int hsh, id;
            cin>>hsh>>id;
            int l=0, r=sz-1;
            {
                while(l+1<r){
                    int md=(l+r)/2;
                    if(qry(rev[id],rind[id],(rind[id]+md)%sz)==md+1)l=md;
                    else r=md;
                }
                if(qry(rev[id],rind[id],(rind[id]+l)%sz)<l+1)r=l;
            }
            sans+=r;
            id=(id+ll(m)*r)%h;
           // cout<<id<<endl;
            obj[hsh]=id;
            bit_add(rev[id],rind[id],1);
        }else{
            int id;
            cin>>id;
            id=obj[id];
            bit_add(rev[id],rind[id],-1);
        }
    }cout<<sans<<endl;
}


#include <bits/stdc++.h>
#define fr(a,b,c) for(int a=b;a<c;a++)
using namespace std;
map<int,int> obj;
int rev[1234567];
int rind[1234567];
vector<vector<int>> fens;
void bit_add(int bn,int pos, int val){
    pos++;
    while(pos<=fens[bn].size()){
        fens[bn][pos-1]+=val;
        pos+=pos&-pos;
    }
}

int bit_qry(int bn,int pos){
    pos++;
    int ret=0;
    while(pos){
        ret+=fens[bn][pos-1];
        pos-=pos&-pos;
    }
    return ret;
}

int qry(int w,int a,int b){
    int ret;
   if(a<=b){
       ret=bit_qry(w,b)-bit_qry(w,a-1);
   }else{
       ret=bit_qry(w,fens[w].size()-1)-bit_qry(w,a-1)+bit_qry(w,b);
   }
  // cout<<w<<" "<<a<<" "<<b<<" = "<<ret<<endl;
   return ret;
}
int main(){
    memset(rev,-1,sizeof rev);
    long long h,m,n;
    cin>>h>>m>>n;
    int fenc=0;
    fr(i,0,h){
        if(rev[i]==-1){
            fens.push_back(vector<int>());
            fens[fenc].push_back(0);
            rind[i]=0;
            rev[i]=fenc;
            int i2=(i+m)%h;
            while(i2!=i){
                rind[i2]=fens[fenc].size();
                fens[fenc].push_back(0);
                rev[i2]=fenc;
                i2=(i2+m)%h;
            }
            fenc++;
        }
    }long long sans=0;
    fr(i,0,n){
        string s;
        cin>>s;
        if(s=="+"){
            int hsh, id;
            cin>>hsh>>id;
            int l=0, r=fens[rev[id]].size()-1;
            {
                while(l+1<r){
                    int md=(l+r)/2;
                    if(qry(rev[id],rind[id],(rind[id]+md)%fens[rev[id]].size())==md+1)l=md;
                    else r=md;
                }
                if(qry(rev[id],rind[id],(rind[id]+l)%fens[rev[id]].size())<l+1)r=l;
            }
            sans+=r;
            id=(id+m*r)%h;
           // cout<<id<<endl;
            obj[hsh]=id;
            bit_add(rev[id],rind[id],1);
        }else{
            int id;
            cin>>id;
            id=obj[id];
            bit_add(rev[id],rind[id],-1);
        }
    }cout<<sans<<endl;
}


/*
Author : Hocky Yudhiono
8/6/2020 12:11:45 PM

1. You can sort the query if offline!
2. Don't bring the dp remaining state when dfsing on DP on Tree.
3. Try to reverse (Think from the back) if you stuck.
4. Be careful when submitting Div. 2 D-F, dont waste it on stupid WAs.
5. Try to reduce a problem, think of it when you're making subtasks
   like when problemsetting.
*/

#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

typedef long long LL;
typedef long long ll;
typedef long double LD;
typedef vector<int> vi;
typedef pair<LL,LL> PLL;
typedef pair<LL,int> PLI;
typedef pair<int,int> PII;
typedef pair<int,int> pii;
typedef vector<vector<LL>> VVL;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define popf pop_front
#define pf push_front
#define popb pop_back
#define mp make_pair
#define pb push_back
#define remove erase
#define fi first
#define se second

// If the time limit is strict, try not to use long double

// Remember to undefine if the problem is interactive
#define endl '\n'
#define DEBUG(X) cout << ">>> DEBUG(" << __LINE__ << ") " << #X << " = " << (X) << endl

const double EPS = 1e-9;
const int INFMEM = 63;
const int INF = 1061109567;
const LL LINF = 4557430888798830399LL;
const double DINF = numeric_limits<double>::infinity();
const LL MOD = 1000000007;
const int dx[8] = {0,0,1,-1,1,-1,1,-1};
const int dy[8] = {1,-1,0,0,1,-1,-1,1};
// Do dir^1 to get reverse direction
const char dch[4] = {'R','L','D','U'};
// const string ds[8] = {"E","W","S","N","SE","NW","SW","NE"};
const double PI = 3.141592653589793;

inline void open(string a){
    freopen((a+".in").c_str(),"r",stdin);
    freopen((a+".out").c_str(),"w",stdout);
}

inline void fasterios(){
    // Do not use if interactive
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // cout << fixed << setprecision(10);
}

#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

gp_hash_table<LL, LL, custom_hash> comp;

struct Query{
    bool isAdd;
    int id;
    int hash;
};

LL n,m,h;
PII posID[100005];
PII posInBIT[100005];

vector <Query> query;
vector <int> revComp;

struct BIT{
    int n,logN; vector <LL> BITVal, start;
    BIT(){}
    BIT(int N, int _start = 1) : start(_start), n(N), BITVal(N+1), logN(31-__builtin_clz(N)) {}
    void upd(LL pos, LL val){
        pos++;
        if(pos <= 0) return;
        for(;pos <= n;pos += (pos&(-pos))) BITVal[pos] += val;
    }
    void change(LL pos, LL val){
        pos++;
        if(pos <= 0) return;
        upd(pos,val-get(pos,pos));
    }
    int size(){ return n; }
    LL get(LL pos){
        pos++;
        LL ret = 0;
        for(;pos > 0;pos -= (pos&(-pos))) ret += BITVal[pos];
        return ret;
    }
    LL get(LL a, LL b){
        return get(b) - (a > 1 ? get(a-1) : 0);
    }
    // Find the first index x such that get(x) >= sum, lower bound
    LL findOrder(LL sum){
        LL pos = 0;
        for(int k = logN;k >= 0;k--){
            LL nx = pos+(1<<k);
            if(nx <= n && BITVal[nx] < sum)
                pos = nx, sum -= BITVal[pos];
        }
        return pos+2;
    }
};

vector <BIT> allBIT;

LL findFirst(LL idx, LL pos){
    BIT &curBIT = allBIT[idx];
    LL kiri = pos;
    LL kanan = sz(curBIT);
    LL kurang = curBIT.get(pos-1);
    // cout << "Cari " << kiri << " " << kanan << endl;
    while(kiri < kanan){
        LL mid = (kiri+kanan)>>1;
        // cout << mid << " " << curBIT.get(mid) << endl;
        if(curBIT.get(mid)-kurang != (mid-pos+1)) kanan = mid;
        else kiri = mid+1;
    }
    return kiri;
}

int main(){
    fasterios();
    cin >> h >> m >> n;
    for(int i = 1;i <= n;i++){
        char ch; cin >> ch;
        Query tmp = {(ch == '+'), -1, -1};
        cin >> tmp.id;
        if(ch == '+') cin >> tmp.hash;
        query.pb(tmp);
        revComp.pb(tmp.id);
    }
    // cout << "Here" << endl;

    sort(revComp.begin(),revComp.end());
    revComp.erase(unique(revComp.begin(),revComp.end()),revComp.end());
    rep(i,0,sz(revComp)) comp[revComp[i]] = i;

    rep(i,0,n) posInBIT[i] = {-1,-1};
    rep(i,0,n){
        if(posInBIT[i].fi != -1) continue;
        int pos = i;
        int bitSize = 0;
        // cout << "Make new " << endl;
        while(posInBIT[pos].fi == -1){
            posInBIT[pos] = {sz(allBIT), bitSize++};
            pos = (pos+m)%h;
        }
        allBIT.pb(BIT(bitSize));
    }

    LL ans = 0;
    trav(curQuery, query){
        curQuery.id = comp[curQuery.id];
        if(curQuery.isAdd){
            PII &curPos = posInBIT[curQuery.hash];
            // cout << curPos.fi << " " << curPos.se << endl;
            int putPos = findFirst(curPos.fi, curPos.se);
            if(putPos == sz(allBIT[curPos.fi])) putPos = findFirst(curPos.fi, sz(allBIT[curPos.fi])-1);
            // cout << "Penaruhan " << curPos.se << " " << putPos << endl;
            ans += (curPos.se <= putPos ? putPos-curPos.se : sz(allBIT[curPos.fi])-curPos.se+putPos+1);

            allBIT[curPos.fi].upd(putPos, 1);

            posID[curQuery.id] = {curPos.fi, putPos};
        }else{
            allBIT[posID[curQuery.id].fi].upd(posID[curQuery.id].se, -1);
        }
    }
    cout << ans << endl;
    return 0;
}


/*
Author : Hocky Yudhiono
8/6/2020 12:11:45 PM

1. You can sort the query if offline!
2. Don't bring the dp remaining state when dfsing on DP on Tree.
3. Try to reverse (Think from the back) if you stuck.
4. Be careful when submitting Div. 2 D-F, dont waste it on stupid WAs.
5. Try to reduce a problem, think of it when you're making subtasks
   like when problemsetting.
*/

#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

typedef long long LL;
typedef long long ll;
typedef long double LD;
typedef vector<int> vi;
typedef pair<LL,LL> PLL;
typedef pair<LL,int> PLI;
typedef pair<int,int> PII;
typedef pair<int,int> pii;
typedef vector<vector<LL>> VVL;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define popf pop_front
#define pf push_front
#define popb pop_back
#define mp make_pair
#define pb push_back
#define remove erase
#define fi first
#define se second

// If the time limit is strict, try not to use long double

// Remember to undefine if the problem is interactive
#define endl '\n'
#define DEBUG(X) cout << ">>> DEBUG(" << __LINE__ << ") " << #X << " = " << (X) << endl

const double EPS = 1e-9;
const int INFMEM = 63;
const int INF = 1061109567;
const LL LINF = 4557430888798830399LL;
const double DINF = numeric_limits<double>::infinity();
const LL MOD = 1000000007;
const int dx[8] = {0,0,1,-1,1,-1,1,-1};
const int dy[8] = {1,-1,0,0,1,-1,-1,1};
// Do dir^1 to get reverse direction
const char dch[4] = {'R','L','D','U'};
// const string ds[8] = {"E","W","S","N","SE","NW","SW","NE"};
const double PI = 3.141592653589793;

inline void open(string a){
    freopen((a+".in").c_str(),"r",stdin);
    freopen((a+".out").c_str(),"w",stdout);
}

inline void fasterios(){
    // Do not use if interactive
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // cout << fixed << setprecision(10);
}

#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

gp_hash_table<LL, LL, custom_hash> comp;

struct Query{
    bool isAdd;
    int id;
    int hash;
};

LL n,m,h;
PII posID[100005];
PII posInBIT[100005];

vector <Query> query;
vector <int> revComp;

struct BIT{
    int n,logN; vector <LL> BITVal, start;
    BIT(){}
    BIT(int N, int _start = 1) : start(_start), n(N), BITVal(N+1), logN(31-__builtin_clz(N)) {}
    void upd(LL pos, LL val){
        pos++;
        if(pos <= 0) return;
        for(;pos <= n;pos += (pos&(-pos))) BITVal[pos] += val;
    }
    void change(LL pos, LL val){
        pos++;
        if(pos <= 0) return;
        upd(pos,val-get(pos,pos));
    }
    int size(){ return n; }
    LL get(LL pos){
        pos++;
        LL ret = 0;
        for(;pos > 0;pos -= (pos&(-pos))) ret += BITVal[pos];
        return ret;
    }
    LL get(LL a, LL b){
        return get(b) - (a > 1 ? get(a-1) : 0);
    }
    // Find the first index x such that get(x) >= sum, lower bound
    LL findOrder(LL sum){
        LL pos = 0;
        for(int k = logN;k >= 0;k--){
            LL nx = pos+(1<<k);
            if(nx <= n && BITVal[nx] < sum)
                pos = nx, sum -= BITVal[pos];
        }
        return pos+2;
    }
};

vector <BIT> allBIT;

LL findFirst(LL idx, LL pos){
    BIT &curBIT = allBIT[idx];
    LL kiri = pos;
    LL kanan = sz(curBIT);
    LL kurang = curBIT.get(pos-1);
    while(kiri < kanan){
        LL mid = (kiri+kanan)>>1;
        if(curBIT.get(mid)-kurang != (mid-pos+1)) kanan = mid;
        else kiri = mid+1;
    }
    return kiri;
}

int main(){
    fasterios();
    cin >> h >> m >> n;
    for(int i = 1;i <= n;i++){
        char ch; cin >> ch;
        Query tmp = {(ch == '+'), -1, -1};
        cin >> tmp.id;
        if(ch == '+') cin >> tmp.hash;
        query.pb(tmp);
        revComp.pb(tmp.id);
    }
    // cout << "Here" << endl;

    sort(revComp.begin(),revComp.end());
    revComp.erase(unique(revComp.begin(),revComp.end()),revComp.end());
    rep(i,0,sz(revComp)) comp[revComp[i]] = i;

    rep(i,0,n) posInBIT[i] = {-1,-1};
    rep(i,0,n){
        if(posInBIT[i].fi != -1) continue;
        int pos = i;
        int bitSize = 0;
        // cout << "Make new " << endl;
        while(posInBIT[pos].fi == -1){
            posInBIT[pos] = {sz(allBIT), bitSize++};
            pos = (pos+m)%h;
        }

        allBIT.pb(BIT(bitSize));
    }

    LL ans = 0;
    trav(curQuery, query){
        curQuery.id = comp[curQuery.id];
        if(curQuery.isAdd){
            PII &curPos = posInBIT[curQuery.hash];
            int putPos = findFirst(curPos.fi, curPos.se);
            if(putPos == sz(allBIT[curPos.fi])) putPos = findFirst(curPos.fi, 0);
            ans += (curPos.se <= putPos ? putPos-curPos.se : sz(allBIT[curPos.fi])-curPos.se+putPos);

            allBIT[curPos.fi].upd(putPos, 1);

            posID[curQuery.id] = {curPos.fi, putPos};
        }else{
            allBIT[posID[curQuery.id].fi].upd(posID[curQuery.id].se, -1);
        }
    }
    cout << ans << endl;
    return 0;
}


/*
Author : Hocky Yudhiono
8/6/2020 12:11:45 PM

1. You can sort the query if offline!
2. Don't bring the dp remaining state when dfsing on DP on Tree.
3. Try to reverse (Think from the back) if you stuck.
4. Be careful when submitting Div. 2 D-F, dont waste it on stupid WAs.
5. Try to reduce a problem, think of it when you're making subtasks
   like when problemsetting.
*/

#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

typedef long long LL;
typedef long long ll;
typedef long double LD;
typedef vector<int> vi;
typedef pair<LL,LL> PLL;
typedef pair<LL,int> PLI;
typedef pair<int,int> PII;
typedef pair<int,int> pii;
typedef vector<vector<LL>> VVL;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define popf pop_front
#define pf push_front
#define popb pop_back
#define mp make_pair
#define pb push_back
#define remove erase
#define fi first
#define se second

// If the time limit is strict, try not to use long double

// Remember to undefine if the problem is interactive
#define endl '\n'
#define DEBUG(X) cout << ">>> DEBUG(" << __LINE__ << ") " << #X << " = " << (X) << endl

const double EPS = 1e-9;
const int INFMEM = 63;
const int INF = 1061109567;
const LL LINF = 4557430888798830399LL;
const double DINF = numeric_limits<double>::infinity();
const LL MOD = 1000000007;
const int dx[8] = {0,0,1,-1,1,-1,1,-1};
const int dy[8] = {1,-1,0,0,1,-1,-1,1};
// Do dir^1 to get reverse direction
const char dch[4] = {'R','L','D','U'};
// const string ds[8] = {"E","W","S","N","SE","NW","SW","NE"};
const double PI = 3.141592653589793;

inline void open(string a){
    freopen((a+".in").c_str(),"r",stdin);
    freopen((a+".out").c_str(),"w",stdout);
}

inline void fasterios(){
    // Do not use if interactive
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // cout << fixed << setprecision(10);
}

#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

gp_hash_table<LL, LL, custom_hash> comp;

struct Query{
    bool isAdd;
    int id;
    int hash;
};

LL n,m,h;
PII posID[200005];
PII posInBIT[200005];

vector <Query> query;
vector <int> revComp;

struct BIT{
    int n,logN; vector <LL> BITVal, start;
    BIT(){}
    BIT(int N, int _start = 1) : start(_start), n(N), BITVal(N+1), logN(31-__builtin_clz(N)) {}
    void upd(LL pos, LL val){
        pos++;
        if(pos <= 0) return;
        for(;pos <= n;pos += (pos&(-pos))) BITVal[pos] += val;
    }
    void change(LL pos, LL val){
        pos++;
        if(pos <= 0) return;
        upd(pos,val-get(pos,pos));
    }
    int size(){ return n; }
    LL get(LL pos){
        pos++;
        LL ret = 0;
        for(;pos > 0;pos -= (pos&(-pos))) ret += BITVal[pos];
        return ret;
    }
    LL get(LL a, LL b){
        return get(b) - (a > 1 ? get(a-1) : 0);
    }
    // Find the first index x such that get(x) >= sum, lower bound
    LL findOrder(LL sum){
        LL pos = 0;
        for(int k = logN;k >= 0;k--){
            LL nx = pos+(1<<k);
            if(nx <= n && BITVal[nx] < sum)
                pos = nx, sum -= BITVal[pos];
        }
        return pos+2;
    }
};

vector <BIT> allBIT;

LL findFirst(LL idx, LL pos){
    BIT &curBIT = allBIT[idx];
    LL kiri = pos;
    LL kanan = sz(curBIT);
    LL kurang = curBIT.get(pos-1);
    while(kiri < kanan){
        LL mid = (kiri+kanan)>>1;
        if(curBIT.get(mid)-kurang != (mid-pos+1)) kanan = mid;
        else kiri = mid+1;
    }
    return kiri;
}

int main(){
    fasterios();
    cin >> h >> m >> n;
    for(int i = 1;i <= n;i++){
        char ch; cin >> ch;
        Query tmp = {(ch == '+'), -1, -1};
        cin >> tmp.id;
        if(ch == '+') cin >> tmp.hash;
        query.pb(tmp);
        revComp.pb(tmp.id);
    }
    // cout << "Here" << endl;

    sort(revComp.begin(),revComp.end());
    revComp.erase(unique(revComp.begin(),revComp.end()),revComp.end());
    rep(i,0,sz(revComp)) comp[revComp[i]] = i;

    rep(i,0,n) posInBIT[i] = {-1,-1};
    rep(i,0,n){
        if(posInBIT[i].fi != -1) continue;
        int pos = i;
        int bitSize = 0;
        // cout << "Make new " << endl;
        while(posInBIT[pos].fi == -1){
            posInBIT[pos] = {sz(allBIT), bitSize++};
            pos = (pos+m)%h;
        }

        allBIT.pb(BIT(bitSize));
    }

    LL ans = 0;
    trav(curQuery, query){
        curQuery.id = comp[curQuery.id];
        if(curQuery.isAdd){
            PII &curPos = posInBIT[curQuery.hash];
            int putPos = findFirst(curPos.fi, curPos.se);
            if(putPos == sz(allBIT[curPos.fi])) putPos = findFirst(curPos.fi, 0);
            ans += (curPos.se <= putPos ? putPos-curPos.se : sz(allBIT[curPos.fi])-curPos.se+putPos);

            allBIT[curPos.fi].upd(putPos, 1);

            posID[curQuery.id] = {curPos.fi, putPos};
        }else{
            allBIT[posID[curQuery.id].fi].upd(posID[curQuery.id].se, -1);
        }
    }
    cout << ans << endl;
    return 0;
}


/*
Author : Hocky Yudhiono
8/6/2020 12:11:45 PM

1. You can sort the query if offline!
2. Don't bring the dp remaining state when dfsing on DP on Tree.
3. Try to reverse (Think from the back) if you stuck.
4. Be careful when submitting Div. 2 D-F, dont waste it on stupid WAs.
5. Try to reduce a problem, think of it when you're making subtasks
   like when problemsetting.
*/

#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

typedef long long LL;
typedef long long ll;
typedef long double LD;
typedef vector<int> vi;
typedef pair<LL,LL> PLL;
typedef pair<LL,int> PLI;
typedef pair<int,int> PII;
typedef pair<int,int> pii;
typedef vector<vector<LL>> VVL;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define popf pop_front
#define pf push_front
#define popb pop_back
#define mp make_pair
#define pb push_back
#define remove erase
#define fi first
#define se second

// If the time limit is strict, try not to use long double

// Remember to undefine if the problem is interactive
#define endl '\n'
#define DEBUG(X) cout << ">>> DEBUG(" << __LINE__ << ") " << #X << " = " << (X) << endl

const double EPS = 1e-9;
const int INFMEM = 63;
const int INF = 1061109567;
const LL LINF = 4557430888798830399LL;
const double DINF = numeric_limits<double>::infinity();
const LL MOD = 1000000007;
const int dx[8] = {0,0,1,-1,1,-1,1,-1};
const int dy[8] = {1,-1,0,0,1,-1,-1,1};
// Do dir^1 to get reverse direction
const char dch[4] = {'R','L','D','U'};
// const string ds[8] = {"E","W","S","N","SE","NW","SW","NE"};
const double PI = 3.141592653589793;

inline void open(string a){
    freopen((a+".in").c_str(),"r",stdin);
    freopen((a+".out").c_str(),"w",stdout);
}

inline void fasterios(){
    // Do not use if interactive
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // cout << fixed << setprecision(10);
}

#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

gp_hash_table<LL, LL, custom_hash> comp;

struct Query{
    bool isAdd;
    int id;
    int hash;
};

LL n,m,h;
PII posID[200005];
PII posInBIT[200005];

vector <Query> query;
vector <int> revComp;

struct BIT{
    int n,logN; vector <LL> BITVal, start;
    BIT(){}
    BIT(int N, int _start = 1) : start(_start), n(N), BITVal(N+1), logN(31-__builtin_clz(N)) {}
    void upd(LL pos, LL val){
        pos++;
        if(pos <= 0) return;
        for(;pos <= n;pos += (pos&(-pos))) BITVal[pos] += val;
    }
    void change(LL pos, LL val){
        pos++;
        if(pos <= 0) return;
        upd(pos,val-get(pos,pos));
    }
    int size(){ return n; }
    LL get(LL pos){
        pos++;
        LL ret = 0;
        for(;pos > 0;pos -= (pos&(-pos))) ret += BITVal[pos];
        return ret;
    }
    LL get(LL a, LL b){
        return get(b) - (a > 1 ? get(a-1) : 0);
    }
    // Find the first index x such that get(x) >= sum, lower bound
    LL findOrder(LL sum){
        LL pos = 0;
        for(int k = logN;k >= 0;k--){
            LL nx = pos+(1<<k);
            if(nx <= n && BITVal[nx] < sum)
                pos = nx, sum -= BITVal[pos];
        }
        return pos+2;
    }
};

vector <BIT> allBIT;

LL findFirst(LL idx, LL pos){
    BIT &curBIT = allBIT[idx];
    LL kiri = pos;
    LL kanan = sz(curBIT);
    LL kurang = curBIT.get(pos-1);
    while(kiri < kanan){
        LL mid = (kiri+kanan)>>1;
        if(curBIT.get(mid)-kurang != (mid-pos+1)) kanan = mid;
        else kiri = mid+1;
    }
    return kiri;
}

int main(){
    fasterios();
    cin >> h >> m >> n;
    for(int i = 1;i <= n;i++){
        char ch; cin >> ch;
        Query tmp = {(ch == '+'), -1, -1};
        cin >> tmp.id;
        if(ch == '+') cin >> tmp.hash;
        query.pb(tmp);
        revComp.pb(tmp.id);
    }
    // cout << "Here" << endl;

    sort(revComp.begin(),revComp.end());
    revComp.erase(unique(revComp.begin(),revComp.end()),revComp.end());
    rep(i,0,sz(revComp)) comp[revComp[i]] = i;

    rep(i,0,h) posInBIT[i] = {-1,-1};
    rep(i,0,h){
        if(posInBIT[i].fi != -1) continue;
        int pos = i;
        int bitSize = 0;
        // cout << "Make new " << endl;
        while(posInBIT[pos].fi == -1){
            posInBIT[pos] = {sz(allBIT), bitSize++};
            pos = (pos+m)%h;
        }

        allBIT.pb(BIT(bitSize));
    }

    LL ans = 0;
    trav(curQuery, query){
        curQuery.id = comp[curQuery.id];
        if(curQuery.isAdd){
            PII &curPos = posInBIT[curQuery.hash];
            int putPos = findFirst(curPos.fi, curPos.se);
            if(putPos == sz(allBIT[curPos.fi])) putPos = findFirst(curPos.fi, 0);
            ans += (curPos.se <= putPos ? putPos-curPos.se : sz(allBIT[curPos.fi])-curPos.se+putPos);

            allBIT[curPos.fi].upd(putPos, 1);

            posID[curQuery.id] = {curPos.fi, putPos};
        }else{
            allBIT[posID[curQuery.id].fi].upd(posID[curQuery.id].se, -1);
        }
    }
    cout << ans << endl;
    return 0;
}


/*
Author : Hocky Yudhiono
8/6/2020 12:11:45 PM

1. You can sort the query if offline!
2. Don't bring the dp remaining state when dfsing on DP on Tree.
3. Try to reverse (Think from the back) if you stuck.
4. Be careful when submitting Div. 2 D-F, dont waste it on stupid WAs.
5. Try to reduce a problem, think of it when you're making subtasks
   like when problemsetting.
*/

#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

typedef long long LL;
typedef long long ll;
typedef long double LD;
typedef vector<int> vi;
typedef pair<LL,LL> PLL;
typedef pair<LL,int> PLI;
typedef pair<int,int> PII;
typedef pair<int,int> pii;
typedef vector<vector<LL>> VVL;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define popf pop_front
#define pf push_front
#define popb pop_back
#define mp make_pair
#define pb push_back
#define remove erase
#define fi first
#define se second

// If the time limit is strict, try not to use long double

// Remember to undefine if the problem is interactive
#define endl '\n'
#define DEBUG(X) cout << ">>> DEBUG(" << __LINE__ << ") " << #X << " = " << (X) << endl

const double EPS = 1e-9;
const int INFMEM = 63;
const int INF = 1061109567;
const LL LINF = 4557430888798830399LL;
const double DINF = numeric_limits<double>::infinity();
const LL MOD = 1000000007;
const int dx[8] = {0,0,1,-1,1,-1,1,-1};
const int dy[8] = {1,-1,0,0,1,-1,-1,1};
// Do dir^1 to get reverse direction
const char dch[4] = {'R','L','D','U'};
// const string ds[8] = {"E","W","S","N","SE","NW","SW","NE"};
const double PI = 3.141592653589793;

inline void open(string a){
    freopen((a+".in").c_str(),"r",stdin);
    freopen((a+".out").c_str(),"w",stdout);
}

inline void fasterios(){
    // Do not use if interactive
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // cout << fixed << setprecision(10);
}

#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

gp_hash_table<LL, LL, custom_hash> comp;

struct Query{
    bool isAdd;
    int id;
    int hash;
};

LL n,m,h;
PII posID[100005];
PII posInBIT[100005];

vector <Query> query;
vector <int> revComp;

struct BIT{
    int n,logN; vector <LL> BITVal, start;
    BIT(){}
    BIT(int N, int _start = 1) : start(_start), n(N), BITVal(N+1), logN(31-__builtin_clz(N)) {}
    void upd(LL pos, LL val){
        pos++;
        if(pos <= 0) return;
        for(;pos <= n;pos += (pos&(-pos))) BITVal[pos] += val;
    }
    void change(LL pos, LL val){
        pos++;
        if(pos <= 0) return;
        upd(pos,val-get(pos,pos));
    }
    int size(){ return n; }
    LL get(LL pos){
        pos++;
        LL ret = 0;
        for(;pos > 0;pos -= (pos&(-pos))) ret += BITVal[pos];
        return ret;
    }
    LL get(LL a, LL b){
        return get(b) - (a > 1 ? get(a-1) : 0);
    }
    // Find the first index x such that get(x) >= sum, lower bound
    LL findOrder(LL sum){
        LL pos = 0;
        for(int k = logN;k >= 0;k--){
            LL nx = pos+(1<<k);
            if(nx <= n && BITVal[nx] < sum)
                pos = nx, sum -= BITVal[pos];
        }
        return pos+2;
    }
};

vector <BIT> allBIT;

LL findFirst(LL idx, LL pos){
    BIT &curBIT = allBIT[idx];
    LL kiri = pos;
    LL kanan = sz(curBIT);
    LL kurang = curBIT.get(pos-1);
    while(kiri < kanan){
        LL mid = (kiri+kanan)>>1;
        if(curBIT.get(mid)-kurang != (mid-pos+1)) kanan = mid;
        else kiri = mid+1;
    }
    return kiri;
}

int main(){
    fasterios();
    cin >> h >> m >> n;
    for(int i = 1;i <= n;i++){
        char ch; cin >> ch;
        Query tmp = {(ch == '+'), -1, -1};
        cin >> tmp.id;
        if(ch == '+') cin >> tmp.hash;
        query.pb(tmp);
        revComp.pb(tmp.id);
    }
    // cout << "Here" << endl;

    sort(revComp.begin(),revComp.end());
    revComp.erase(unique(revComp.begin(),revComp.end()),revComp.end());
    rep(i,0,sz(revComp)) comp[revComp[i]] = i;

    rep(i,0,n) posInBIT[i] = {-1,-1};
    rep(i,0,n){
        if(posInBIT[i].fi != -1) continue;
        int pos = i;
        int bitSize = 0;
        // cout << "Make new " << endl;
        while(posInBIT[pos].fi == -1){
            posInBIT[pos] = {sz(allBIT), bitSize++};
            pos = (pos+m)%h;
        }

        allBIT.pb(BIT(bitSize));
    }

    LL ans = 0;
    trav(curQuery, query){
        curQuery.id = comp[curQuery.id];
        if(curQuery.isAdd){
            PII &curPos = posInBIT[curQuery.hash];
            int putPos = findFirst(curPos.fi, curPos.se);
            if(putPos == sz(allBIT[curPos.fi])+1) putPos = findFirst(curPos.fi, 0);
            ans += (curPos.se <= putPos ? putPos-curPos.se : sz(allBIT[curPos.fi])-curPos.se+putPos+1);

            allBIT[curPos.fi].upd(putPos, 1);

            posID[curQuery.id] = {curPos.fi, putPos};
        }else{
            allBIT[posID[curQuery.id].fi].upd(posID[curQuery.id].se, -1);
        }
    }
    cout << ans << endl;
    return 0;
}


//Kaori Miyazono, did I reach you ?
//"I can't die because... I'm the one who will protect you." -Asuna Yuuki

#include <bits/stdc++.h>
using namespace std;

const int N = 200200;
const int inf = 1e9;
typedef long long int lli;
typedef pair<lli, lli> ii;

lli h, m, n;
set<ii> gp[N];
lli to[N];
vector<lli> el[N];
lli vis[N];
unordered_map<lli, lli> too;
long long int ans = 0;
lli on = 0;

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	scanf("%lld %lld %lld", &h, &m, &n);
	int now = 0;
	for(int i = 0; i < h; i++) {
		assert(vis[i] != 1);
		if (vis[i] == 2) continue;
		lli hcur = i;
		lli ecur = i;
		set<ii> cur;
		while(vis[ecur] < 2) {
			to[ecur] = on;
			vis[ecur]++;
			cur.insert(ii(hcur, ecur));
			el[ecur].push_back(hcur);
			hcur += m;
			ecur = (ecur + m) % h;
		}
		gp[on++] = cur;
	}
	for(int i = 0; i < h; i++) {
		/*cout << to[i] << '\n';
		for(auto it : el[i]) cout << it << ' ';
		cout << '\n';*/
		sort(el[i].begin(), el[i].end());
		reverse(el[i].begin(), el[i].end());
	}
	while(n--) {
		char op;
		lli id;
		lli hsh;
		scanf(" %c %lld", &op, &id);
		if (op == '+') {
			scanf("%lld", &hsh);
			lli group = to[hsh];
			hsh = el[hsh].back();
			lli dis = 0;
			auto it = gp[group].lower_bound(ii(hsh, -inf));
			ii pnow = *it;
			//cout << (*it).first << ' ' << (*it).second << '\n';
			//assert(it != gp[group].end());
			ans += (pnow.first - hsh);
			too[id] = pnow.second;
			lli now = pnow.second;
			for(auto bora : el[now]) {
				gp[group].erase(ii(bora, now));
			}
			//cout << hsh << ' ' << id << ' ' << too[id] << '\n';
		} else {
			lli group = to[too[id]];
			lli now = too[id];
			//cout << id << ' ' << group << ' ' << too[id] << '\n';
			for(auto bora : el[now]) {
				gp[group].insert(ii(bora, now));
			}
			too.erase(id);
		}
		//cout << '\n';
	}
	cout << ans / m << '\n';
	return 0;
}


//Kaori Miyazono, did I reach you ?
//"I can't die because... I'm the one who will protect you." -Asuna Yuuki

#include <bits/stdc++.h>
using namespace std;

const int N = 200200;
const int inf = 1e9;
typedef long long int lli;
typedef pair<lli, int> ii;

int h, m, n;
set<ii> gp[N];
vector<int> quem[N];
int to[N];
ii el[N];
int vis[N];
map<int, int> too;
long long int ans = 0;
int on = 0;

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	scanf("%d %d %d", &h, &m, &n);
	m %= h;
	int now = 0;
	for(int i = 0; i < h; i++) {
		if (vis[i] == 1) continue;
		lli hcur = i;
		int ecur = i;
		set<ii> cur;
		int x = 0;
		while(vis[ecur] < 1) {
			quem[on].push_back(ecur);
			to[ecur] = on;
			vis[ecur]++;
			gp[on].insert(ii(hcur, x));
			el[ecur] = ii(hcur, x);
			hcur += m;
			ecur = (ecur + m);
			if (ecur >= h) ecur -= h;
			x++;
		}
		on++;
	}
	/*for(int i = 0; i < h; i++) {
		cout << to[i] << ' ' << el[i].first << ' ' << el[i].second << '\n';
	}*/
	while(n--) {
		char op;
		int id;
		int hshh;
		scanf(" %c %d", &op, &id);
		if (op == '+') {
			scanf("%d", &hshh);
			int group = to[hshh];
			//cout << group << '\n';
			lli hsh = hshh;
			int pos = el[hsh].second;
			//cout << pos << '\n';
			hsh = el[hsh].first;
			auto it = gp[group].lower_bound(ii(hsh, -inf));
			if (it == gp[group].end()) {
				it = gp[group].lower_bound(ii(0, -inf));
				ans += (int) quem[group].size() - pos + (*it).second;
			} else {
				ans += (*it).second - pos;
			}
			//cout << (*it).first << ' ' << (*it).second << '\n';
			//cout << (*it).first << ' ' << (*it).second << '\n';
			//assert(it != gp[group].end());
			too[id] = quem[group][(*it).second];
			gp[group].erase(it);
			//cout << hsh << ' ' << id << ' ' << too[id] << '\n';
		} else {
			int group = to[too[id]];
			gp[group].insert(el[too[id]]);
			too.erase(id);
		}
		//cout << ans << '\n' << '\n';
		//cout << '\n';
	}
	cout << ans << '\n';
	return 0;
}


//Kaori Miyazono, did I reach you ?
//"I can't die because... I'm the one who will protect you." -Asuna Yuuki

#include <bits/stdc++.h>
using namespace std;

const int N = 200200;
const int inf = 1e9;
typedef long long int lli;
typedef pair<lli, lli> ii;

int h, m, n;
set<ii> gp[N];
int to[N];
vector<lli> el[N];
int vis[N];
map<int, int> too;
long long int ans = 0;
int on = 0;

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	scanf("%d %d %d", &h, &m, &n);
	m %= h;
	int now = 0;
	for(int i = 0; i < h; i++) {
		assert(vis[i] != 1);
		if (vis[i] == 2) continue;
		lli hcur = i;
		int ecur = i;
		set<ii> cur;
		while(vis[ecur] < 2) {
			to[ecur] = on;
			vis[ecur]++;
			cur.insert(ii(hcur, ecur));
			el[ecur].push_back(hcur);
			hcur += m;
			ecur = (ecur + m);
			if (ecur >= h) ecur -= h;
		}
		gp[on++] = cur;
	}
	while(n--) {
		char op;
		int id;
		lli hsh;
		scanf(" %c %d", &op, &id);
		if (op == '+') {
			scanf("%lld", &hsh);
			lli group = to[hsh];
			hsh = el[hsh][0];
			lli dis = 0;
			auto it = gp[group].lower_bound(ii(hsh, -inf));
			ii pnow = *it;
			//cout << (*it).first << ' ' << (*it).second << '\n';
			//assert(it != gp[group].end());
			ans += (pnow.first - hsh);
			too[id] = pnow.second;
			int now = pnow.second;
			for(auto bora : el[now]) {
				gp[group].erase(ii(bora, now));
			}
			//cout << hsh << ' ' << id << ' ' << too[id] << '\n';
		} else {
			int group = to[too[id]];
			int now = too[id];
			//cout << id << ' ' << group << ' ' << too[id] << '\n';
			for(auto bora : el[now]) {
				gp[group].insert(ii(bora, now));
			}
			too.erase(id);
		}
		//cout << '\n';
	}
	cout << ans / m << '\n';
	return 0;
}


//Kaori Miyazono, did I reach you ?
//"I can't die because... I'm the one who will protect you." -Asuna Yuuki

#include <bits/stdc++.h>
using namespace std;

const int N = 200200;
const int inf = 1e9;
typedef long long int lli;
typedef pair<lli, int> ii;

int h, m, n;
set<ii> gp[N];
int to[N];
vector<lli> el[N];
int vis[N];
map<int, int> too;
long long int ans = 0;
int on = 0;

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	scanf("%d %d %d", &h, &m, &n);
	m %= h;
	int now = 0;
	for(int i = 0; i < h; i++) {
		assert(vis[i] != 1);
		if (vis[i] == 2) continue;
		lli hcur = i;
		int ecur = i;
		set<ii> cur;
		while(vis[ecur] < 2) {
			to[ecur] = on;
			vis[ecur]++;
			cur.insert(ii(hcur, ecur));
			el[ecur].push_back(hcur);
			hcur += m;
			ecur = (ecur + m);
			if (ecur >= h) ecur -= h;
		}
		gp[on++] = cur;
	}
	while(n--) {
		char op;
		int id;
		lli hsh;
		scanf(" %c %d", &op, &id);
		if (op == '+') {
			scanf("%lld", &hsh);
			int group = to[hsh];
			hsh = el[hsh][0];
			lli dis = 0;
			auto it = gp[group].lower_bound(ii(hsh, -inf));
			ii pnow = *it;
			//cout << (*it).first << ' ' << (*it).second << '\n';
			//assert(it != gp[group].end());
			ans += (pnow.first - hsh);
			too[id] = pnow.second;
			int now = pnow.second;
			for(auto bora : el[now]) {
				gp[group].erase(ii(bora, now));
			}
			//cout << hsh << ' ' << id << ' ' << too[id] << '\n';
		} else {
			int group = to[too[id]];
			int now = too[id];
			//cout << id << ' ' << group << ' ' << too[id] << '\n';
			for(auto bora : el[now]) {
				gp[group].insert(ii(bora, now));
			}
			too.erase(id);
		}
		//cout << '\n';
	}
	cout << ans / m << '\n';
	return 0;
}


#include <cstdio>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

typedef long long ll;

const int Maxn = 200005;

int h, m, n;
int inset[Maxn], id[Maxn];

struct pos {
    int x;
    pos(int x = 0): x(x) {}
    bool operator <(const pos &p) const {
        return id[x] < id[p.x];
    }
};

set <pos> S[Maxn];
int siz[Maxn];
map <int, int> M;
ll res;

int main()
{
    scanf("%d %d %d", &h, &m, &n);
    for (int i = 0; i < h; i++) if (!id[i]) {
        int j = i;
        do {
            inset[j] = i; id[j] = S[i].size() + 1; S[i].insert(pos(j));
            j = (j + m) % h;
        } while (!id[j]);
        siz[i] = S[i].size();
    }
    while (n--) {
        char c;
        int v, hsh;
        scanf(" %c", &c);
        if (c == '+') {
            scanf("%d %d", &v, &hsh);
            set <pos>::iterator it = S[inset[hsh]].lower_bound(pos(hsh));
            if (it == S[inset[hsh]].end()) it = S[inset[hsh]].begin();
            res += (id[it->x] - id[hsh] + siz[inset[hsh]]) % siz[inset[hsh]];
            M[v] = it->x; S[inset[hsh]].erase(it);
        } else {
            scanf("%d", &v);
            S[inset[M[v]]].insert(pos(M[v])); M.erase(v);
        }
    }
    printf("%I64d\n", res);
    return 0;
}


import static java.util.Arrays.deepToString;

import java.io.*;
import java.math.*;
import java.util.*;

public class C {

	static int[] sum;
	static int index;

	static int sum(int[] f, int L, int R) {
		if (L <= R)
			return sumP(f, R) - sumP(f, L - 1);
		else
			return sum[index] + sumP(f, R) - sumP(f, L - 1);
	}

	static int sumP(int[] f, int r) {
		if (r < 0)
			return 0;
		int res = 0;
		for (int R = r; R >= 0; R = (R & (R + 1)) - 1) {
			res += f[R];
		}
		return res;
	}

	static void add(int[] f, int i, int x) {
		for (int I = i; I < f.length; I |= I + 1) {
			f[I] += x;
		}
	}

	static int gcd(int a, int b) {
		return a == 0 ? b : gcd(b % a, a);
	}

	static int next_zero(int[] f, int pos) {
		int diff = 0;
		for (int t = 18; t >= 0; t--) {
			int len = (1 << t) + diff;
			if (len >= f.length)
				continue;
			if (sum(f, pos, (pos + len - 1) % f.length) == len) {
				diff += 1 << t;
			}
		}
		int to = (pos + diff) % f.length;
		add(f, to, 1);
		return diff;
	}

	static void solve() {
		int h = nextInt(), m = nextInt(), q = nextInt();
		int d = gcd(m, h);
		int[][] f = new int[d][h / d];
		sum = new int[d];
		int mi = BigInteger.valueOf(m / d).modInverse(BigInteger.valueOf(h / d)).intValue();
		HashMap<Integer, Integer> where = new HashMap<>();
		long ans = 0;
//		boolean[] filled = new boolean[h];
//		int brute_ans = 0;
		for (int i = 0; i < q; i++) {
			String type = next();
			if (type.equals("+")) {
				int id = nextInt(), hash = nextInt();
//				int k = 0, hs = hash;
//				for (; filled[hs]; k++, hs = (hs + m) % h)
//					;
//				filled[hs] = true;
//				brute_ans += k;
				index = hash % d;
				int pos = (int) (((long) hash / d * mi) % (h / d));
				int diff = next_zero(f[index], pos);
				ans += diff;
				where.put(id, (hash + m * diff) % h);
//				debug((hash + m * diff) % h, hs, diff);
//				if ((hash + m * diff) % h != hs)
//					throw new AssertionError();
				sum[index]++;
			} else {
				int id = nextInt();
				int pi = where.get(id);
//				filled[pi] = false;
				index = pi % d;
				int pos = (int) (((long) pi / d * mi) % (h / d));
				add(f[index], pos, -1);
				sum[index]--;
			}
		}
//		debug(ans, brute_ans);
		writer.println(ans);
	}

	public static void main(String[] args) throws Exception {
		reader = new BufferedReader(new InputStreamReader(System.in));
		writer = new PrintWriter(System.out);

		setTime();
		solve();
		printTime();
		printMemory();

		writer.close();
	}

	static BufferedReader reader;
	static PrintWriter writer;
	static StringTokenizer tok = new StringTokenizer("");
	static long systemTime;

	static void debug(Object... o) {
		System.err.println(deepToString(o));
	}

	static void setTime() {
		systemTime = System.currentTimeMillis();
	}

	static void printTime() {
		System.err.println("Time consumed: " + (System.currentTimeMillis() - systemTime));
	}

	static void printMemory() {
		System.err.println("Memory consumed: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000 + "kb");
	}

	static String next() {
		while (!tok.hasMoreTokens()) {
			String w = null;
			try {
				w = reader.readLine();
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (w == null)
				return null;
			tok = new StringTokenizer(w);
		}
		return tok.nextToken();
	}

	static int nextInt() {
		return Integer.parseInt(next());
	}

	static long nextLong() {
		return Long.parseLong(next());
	}

	static double nextDouble() {
		return Double.parseDouble(next());
	}

	static BigInteger nextBigInteger() {
		return new BigInteger(next());
	}
}


/**
 * Created by IntelliJ IDEA.
 * User: Taras_Brzezinsky
 * Date: 2/29/12
 * Time: 7:52 PM
 * To change this template use File | Settings | File Templates.
 */

import javax.swing.text.Segment;
import java.awt.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class TaskA extends Thread {
    public TaskA(int type) {
        try {
            if (type == 0) {
                this.input = new BufferedReader(new InputStreamReader(System.in));
                this.output = new PrintWriter(System.out);
            } else {
                this.input = new BufferedReader(new FileReader(INPUT_FILE));
                this.output = new PrintWriter(OUTPUT_FILE);
            }
            this.setPriority(Thread.MAX_PRIORITY);
        } catch (Throwable e) {
            e.printStackTrace();
            System.exit(666);
        }
    }

    public class SegmentTreeSetPosition {
        final int[] zeros;
        final int n;

        public SegmentTreeSetPosition(int n) {
            this.n = n;
            zeros = new int[4 * n];
            buildTree(1, 0, n - 1);
        }

        void buildTree(int node, int left, int right) {
            zeros[node] = right - left + 1;
            if (left < right) {
                int mid = (left + right) >> 1;
                buildTree(node * 2, left, mid);
                buildTree(node * 2 + 1, mid + 1, right);
            }
        }

        public void setUnused(int pos) {
            setUnused(pos, 1, 0, n - 1);
        }

        void setUnused(int pos, int node, int left, int right) {
            ++zeros[node];
            if (left == right)
                return;
            int mid = (left + right) >> 1;
            if (pos <= mid)
                setUnused(pos, node * 2, left, mid);
            else
                setUnused(pos, node * 2 + 1, mid + 1, right);
        }

        public void setUsed(int pos) {
            setUsed(pos, 1, 0, n - 1);
        }

        void setUsed(int pos, int node, int left, int right) {
            --zeros[node];
            if (left == right)
                return;
            int mid = (left + right) >> 1;
            if (pos <= mid)
                setUsed(pos, node * 2, left, mid);
            else
                setUsed(pos, node * 2 + 1, mid + 1, right);
        }

        public int freeCount(int a, int b) {
            return freeCount(a, b, 1, 0, n - 1);
        }

        int freeCount(int a, int b, int node, int left, int right) {
            if (left >= a && right <= b)
                return zeros[node];
            int mid = (left + right) >> 1;
            int res = 0;
            if (a <= mid)
                res += freeCount(a, b, node * 2, left, mid);
            if (b > mid)
                res += freeCount(a, b, node * 2 + 1, mid + 1, right);
            return res;
        }

        public int firstUnused(int a, int b) {
            return firstUnused(1, 0, n - 1, a, b);
        }

        int firstUnused(int node, int left, int right, int a, int b) {
            if (a > right || b < left || zeros[node] == 0)
                return -1;
            if (left == right)
                return left;
            int mid = (left + right) >> 1;
            int res = firstUnused(node * 2, left, mid, a, b);
            return res < 0 ? firstUnused(node * 2 + 1, mid + 1, right, a, b) : res;
        }
    }

    private void solve() throws Throwable {
        int h = nextInt(), m = nextInt(), n = nextInt();
        int types[] = new int[h];
        Arrays.fill(types, -1);
        int[] type = new int[h];
        int[] ats = new int[h];
        int count = 0;
        HashMap<Integer, Integer> sizes = new HashMap<Integer, Integer>();
        for (int i = 0; i < h; ++i) {
            if (types[i] != -1) continue;
            int at = i;
            int pos = 0;
            do {
                types[at] = count;
                type[at] = count;
                ats[at] = pos++;
                at = (at + m) % h;
            } while (types[at] == -1);
            sizes.put(count, pos);
            count++;
        }
        SegmentTreeSetPosition[] S = new SegmentTreeSetPosition[count];
        for (int i = 0; i < count; ++i) {
            S[i] = new SegmentTreeSetPosition(sizes.get(i));
        }
        int[][] posInverted = new int[count][];
        for (int i = 0; i < count; ++i) {
            posInverted[i] = new int[sizes.get(i)];
        }
        long ret = 0;
        for (int i = 0; i < h; ++i) {
            posInverted[type[i]][ats[i]] = i;
        }
        HashMap<Integer, Integer> IDs = new HashMap<Integer, Integer>();
        for (int operation = 0; operation < n; ++operation) {
            String token = nextToken();
            if (token.charAt(0) == '+') {
                int ID = nextInt(), pos = nextInt();
                int at = ats[pos], typeID = type[pos];
                int next = S[typeID].firstUnused(at, sizes.get(typeID) - 1);

                if (next == -1) {
                    ret += sizes.get(typeID) - at;
                    next = S[typeID].firstUnused(0, at - 1);
                    ret += next;
                } else {
                    ret += next - at;
                }
                S[typeID].setUsed(next);
                IDs.put(ID, posInverted[typeID][next]);
            } else {
                int ID = nextInt();
                int pos = IDs.get(ID);
                IDs.remove(ID);
                int typeID = type[pos], at = ats[pos];
                S[typeID].setUnused(at);
            }
        }
        output.println(ret);
    }

    public void run() {
        try {
            solve();
        } catch (Throwable e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
            System.exit(666);
        } finally {
            output.flush();
            output.close();
        }
    }

    public static void main(String[] args) {
        new TaskA(0).start();
    }

    private int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    private long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    private double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    private String nextToken() throws IOException {
        while (tokens == null || !tokens.hasMoreTokens()) {
            tokens = new StringTokenizer(input.readLine());
        }
        return tokens.nextToken();
    }

    private static final String INPUT_FILE = null;
    private static final String OUTPUT_FILE = null;
    private BufferedReader input;
    private PrintWriter output;
    private StringTokenizer tokens = null;
}