#include <bits/stdc++.h>
using namespace std;
struct qry {
  int o, l, r;
} q[100100];
map<int, int> hook, person, mp;
struct data {
  int sz, st, ed;
};
bool operator<(data a, data b) {
  if (a.sz == b.sz) return a.ed > b.ed;
  return a.sz > b.sz;
}
set<data> S;
data pack(int x, int y, int z) {
  data tmp;
  tmp.sz = x;
  tmp.st = y;
  tmp.ed = z;
  return tmp;
}
vector<int> v;
int tree[1000100];
void up(int x, int v) {
  while (x < 1000000) {
    tree[x] += v;
    x += (x & -x);
  }
}
int read(int x) {
  int sum = 0;
  while (x) {
    sum += tree[x];
    x -= (x & -x);
  }
  return sum;
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  S.insert(pack(n, 1, n));
  hook[1] = hook[n] = n;
  for (int qq = 1; qq <= m; qq++) {
    scanf("%d", &q[qq].o);
    if (!q[qq].o) {
      scanf("%d %d", &q[qq].l, &q[qq].r);
      v.push_back(q[qq].l), v.push_back(q[qq].r);
    } else {
      if (!person[q[qq].o]) {
        data tt = *S.begin();
        int len = (tt.ed - tt.st + 1);
        int ind = tt.st + len / 2;
        hook[ind] = -1;
        int l = len / 2, r = len / 2;
        if (!(len & 1)) r--;
        if (ind > tt.st) hook[tt.st] = hook[ind - 1] = l;
        if (ind < tt.ed) hook[ind + 1] = hook[tt.ed] = r;
        S.erase(S.begin());
        if (ind > tt.st) S.insert(pack(l, tt.st, ind - 1));
        if (ind < tt.ed) S.insert(pack(r, ind + 1, tt.ed));
        person[q[qq].o] = ind;
        v.push_back(ind);
        q[qq].l = 1, q[qq].r = ind;
      } else {
        int hispos = person[q[qq].o];
        v.push_back(hispos);
        q[qq].l = 0, q[qq].r = hispos;
        person[q[qq].o] = 0;
        if (hook[hispos - 1] > 0 && hook[hispos + 1] > 0) {
          hook[hispos] = 0;
          int l = hook[hispos - 1];
          int r = hook[hispos + 1];
          hook[hispos - l] = l + r + 1;
          hook[hispos + r] = l + r + 1;
          S.erase(pack(l, hispos - l, hispos - 1));
          S.erase(pack(r, hispos + 1, hispos + r));
          S.insert(pack(l + r + 1, hispos - l, hispos + r));
        } else if (hook[hispos - 1] > 0) {
          int l = hook[hispos - 1];
          hook[hispos - l] = l + 1;
          hook[hispos] = l + 1;
          S.erase(pack(l, hispos - l, hispos - 1));
          S.insert(pack(l + 1, hispos - l, hispos));
        } else if (hook[hispos + 1] > 0) {
          int r = hook[hispos + 1];
          hook[hispos + r] = r + 1;
          hook[hispos] = r + 1;
          S.erase(pack(r, hispos + 1, hispos + r));
          S.insert(pack(r + 1, hispos, hispos + r));
        } else {
          hook[hispos] = 1;
          S.insert(pack(1, hispos, hispos));
        }
      }
    }
  }
  sort(v.begin(), v.end());
  int nsz = unique(v.begin(), v.end()) - v.begin();
  for (int i = 0; i < v.size(); i++) mp[v[i]] = i + 1;
  for (int i = 1; i <= m; i++) {
    if (!q[i].o) {
      printf("%d\n", read(mp[q[i].r]) - read(mp[q[i].l] - 1));
    } else {
      if (q[i].l)
        up(mp[q[i].r], 1);
      else
        up(mp[q[i].r], -1);
    }
  }
}
