#include <bits/stdc++.h>
using namespace std;
int n, q, t;
struct query {
  int l, r;
} queries[100100];
struct range {
  int l, r;
  range(int l, int r) : l(l), r(r) {}
  bool operator<(range other) const {
    if (r - l == other.r - other.l) return l > other.l;
    return r - l > other.r - other.l;
  }
};
set<range> lft;
map<int, int> ind, inv, pos;
int bit[200200];
void update(int pos, int val) {
  while (pos < 200200) {
    bit[pos] += val;
    pos += pos & -pos;
  }
}
int query(int pos) {
  int ret = 0;
  while (pos) {
    ret += bit[pos];
    pos -= pos & -pos;
  }
  return ret;
}
int main() {
  scanf("%d %d", &n, &q);
  lft.insert(range(1, n));
  inv[1] = n;
  inv[n] = 1;
  for (int i = 0; i < q; i++) {
    scanf("%d", &t);
    if (t == 0)
      scanf("%d %d", &queries[i].l, &queries[i].r);
    else {
      if (pos[t] == 0) {
        range aux = *lft.begin();
        lft.erase(aux);
        inv[aux.l] = inv[aux.r] = 0;
        queries[i].l = aux.l + (aux.r - aux.l + 1) / 2;
        queries[i].r = 0;
        pos[t] = queries[i].l;
        if (aux.l < queries[i].l) {
          lft.insert(range(aux.l, queries[i].l - 1));
          inv[aux.l] = queries[i].l - 1;
          inv[queries[i].l - 1] = aux.l;
        }
        if (aux.r > queries[i].l) {
          lft.insert(range(queries[i].l + 1, aux.r));
          inv[queries[i].l + 1] = aux.r;
          inv[aux.r] = queries[i].l + 1;
        }
      } else {
        queries[i].l = pos[t];
        queries[i].r = -1;
        int p = pos[t];
        pos[t] = 0;
        int l = p, r = p;
        if (inv[p - 1]) {
          l = inv[p - 1];
          inv[p - 1] = 0;
          lft.erase(range(l, p - 1));
        }
        if (inv[p + 1]) {
          r = inv[p + 1];
          inv[p + 1] = 0;
          lft.erase(range(p + 1, r));
        }
        lft.insert(range(l, r));
        inv[l] = r;
        inv[r] = l;
      }
    }
  }
  for (int i = 0; i < q; i++) {
    ind[queries[i].l];
    ind[queries[i].r];
  }
  int cnt = 1;
  for (map<int, int>::iterator it = ind.begin(); it != ind.end(); it++)
    ind[it->first] = cnt++;
  for (int i = 0; i < q; i++) {
    queries[i].l = ind[queries[i].l];
    if (queries[i].r > 0) queries[i].r = ind[queries[i].r];
  }
  for (int i = 0; i < q; i++) {
    if (queries[i].r == 0)
      update(queries[i].l, 1);
    else if (queries[i].r == -1)
      update(queries[i].l, -1);
    else
      printf("%d\n", query(queries[i].r) - query(queries[i].l - 1));
  }
  return 0;
}
