#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
const int inf = (-1u) >> 1;
int n, q;
struct empty_seg {
  int l, r;
  empty_seg() {}
  empty_seg(int _l, int _r) : l(_l), r(_r) {}
  bool operator<(const empty_seg& s) const {
    if (r - l == s.r - s.l) {
      return r > s.r;
    }
    return r - l > s.r - s.l;
  }
};
struct dymanic_bit {
  map<int, int> val;
  void clear() { val.clear(); }
  void update(int x, int y) {
    for (; x <= n; x += ((x) & (-(x)))) val[x] += y;
  }
  int getsum(int x) {
    int res = 0;
    for (; x; x -= ((x) & (-(x)))) {
      if (val.find(x) != val.end()) res += val[x];
    }
    return res;
  }
  int getsum(int l, int r) { return getsum(r) - getsum(l - 1); }
};
set<empty_seg> ch;
dymanic_bit db;
struct choose_point {
  set<int> s;
  void clear() {
    s.clear();
    ch.clear(), db.clear();
    ch.insert(empty_seg(1, n));
  }
  void choose(int l, int r) {
    int x = (l + r + 1) >> 1;
    s.insert(x);
    db.update(x, 1);
    if (l <= x - 1) {
      ch.insert(empty_seg(l, x - 1));
    }
    if (x + 1 <= r) {
      ch.insert(empty_seg(x + 1, r));
    }
  }
  void release(int x) {
    set<int>::iterator it = s.find(x), jt = s.find(x);
    int l = (it == s.begin() ? 0 : *(--it));
    int r = (++jt == s.end() ? n + 1 : *jt);
    s.erase(x);
    db.update(x, -1);
    if (l + 1 <= x - 1) {
      ch.erase(empty_seg(l + 1, x - 1));
    }
    if (x + 1 <= r - 1) {
      ch.erase(empty_seg(x + 1, r - 1));
    }
    if (l + 1 <= r - 1) {
      ch.insert(empty_seg(l + 1, r - 1));
    }
  }
};
choose_point cpt;
map<int, int> pos;
int main() {
  while (scanf("%d%d", &n, &q) != EOF) {
    cpt.clear();
    pos.clear();
    while (q--) {
      int op;
      scanf("%d", &op);
      if (op == 0) {
        int a, b;
        scanf("%d%d", &a, &b);
        printf("%d\n", db.getsum(a, b));
      } else {
        if (pos[op] == 0) {
          set<empty_seg>::iterator it = ch.begin();
          cpt.choose(it->l, it->r);
          pos[op] = (it->l + it->r + 1) >> 1;
          ch.erase(it);
        } else {
          cpt.release(pos[op]);
          pos[op] = 0;
        }
      }
    }
  }
  return 0;
}
