#include <bits/stdc++.h>
using namespace std;
const int MaxN = 300010;
int N, Q;
int S[MaxN * 2], sn;
int com[MaxN][3];
int tree[MaxN];
int mp[MaxN];
int pos[MaxN];
struct Node {
  int id, l, r;
  Node() {}
  Node(int id, int l, int r) : id(id), l(l), r(r) {}
} pQ[MaxN];
int pn;
bool operator<(const Node &a, const Node &b) {
  if (pos[a.id] - pos[a.l] == pos[b.id] - pos[b.l])
    return pos[a.id] > pos[b.id];
  else
    return pos[a.id] - pos[a.l] > pos[b.id] - pos[b.l];
}
int getID(int key) { return upper_bound(S, S + sn, key) - S; }
int read(int k) {
  int ret = 0;
  while (k) {
    ret += tree[k];
    k -= k & -k;
  }
  return ret;
}
void update(int k, int v) {
  while (k <= sn) {
    tree[k] += v;
    k += k & -k;
  }
}
void refresh_down(int k) {
  while (k * 2 <= pn) {
    int idx = k * 2;
    if (k * 2 + 1 <= pn && pQ[k * 2 + 1] < pQ[k * 2]) {
      idx = k * 2 + 1;
    }
    if (pQ[idx] < pQ[k]) {
      swap(pQ[k], pQ[idx]);
      mp[pQ[k].id] = k;
      mp[pQ[idx].id] = idx;
      k = idx;
    } else
      break;
  }
}
void refresh_up(int k) {
  while (k / 2 && pQ[k] < pQ[k / 2]) {
    swap(pQ[k], pQ[k / 2]);
    mp[pQ[k].id] = k;
    mp[pQ[k / 2].id] = k / 2;
    k /= 2;
  }
}
void Delete(int id) {
  int k = mp[id];
  Node u = pQ[k];
  mp[id] = 0;
  update(getID(pos[id]), -1);
  if (k < pn) {
    pQ[k] = pQ[pn--];
    mp[pQ[k].id] = k;
    refresh_down(k);
    refresh_up(k);
  } else
    pn--;
  pQ[mp[u.l]].r = u.r;
  pQ[mp[u.r]].l = u.l;
  refresh_up(mp[u.r]);
}
void Insert(int id, int numQ) {
  Node u = pQ[1];
  pQ[1] = pQ[pn--];
  mp[pQ[1].id] = 1;
  refresh_down(1);
  pos[id] = (pos[u.id] + pos[u.l] + 1) / 2;
  update(getID(pos[id]), 1);
  pQ[mp[u.l]].r = id;
  Node v = Node(id, u.l, u.id);
  u.l = id;
  pQ[++pn] = u;
  mp[u.id] = pn;
  refresh_up(pn);
  pQ[++pn] = v;
  mp[v.id] = pn;
  refresh_up(pn);
}
int main() {
  cin >> N >> Q;
  for (int i = 0; i < Q; i++) {
    scanf("%d", &com[i][0]);
    if (com[i][0]) {
      S[sn++] = com[i][0];
    } else {
      scanf("%d %d", &com[i][1], &com[i][2]);
    }
  }
  sort(S, S + sn);
  sn = unique(S, S + sn) - S;
  for (int i = 0; i < Q; i++) {
    if (com[i][0]) {
      com[i][0] = getID(com[i][0]);
    }
  }
  sn = 0;
  for (int i = 0; i < Q; i++) {
    if (!com[i][0]) {
      S[sn++] = com[i][1];
      S[sn++] = com[i][2];
      S[sn++] = com[i][2] + 1;
    }
  }
  S[sn++] = -1;
  S[sn++] = N + 1;
  sort(S, S + sn);
  sn = unique(S, S + sn) - S;
  pQ[++pn] = Node(Q + 1, Q + 2, -1);
  mp[Q + 1] = pn;
  pos[Q + 1] = N + 1;
  pQ[++pn] = Node(Q + 2, Q + 2, Q + 1);
  mp[Q + 2] = pn;
  pos[Q + 2] = 0;
  for (int i = 0; i < Q; i++) {
    if (!com[i][0]) {
      printf("%d\n", read(getID(com[i][2])) - read(getID(com[i][1] - 1)));
    } else {
      if (mp[com[i][0]]) {
        Delete(com[i][0]);
      } else {
        Insert(com[i][0], i);
      }
    }
  }
  return 0;
}
