#include <bits/stdc++.h>
using namespace std;
const long long linf = 1e18 + 5;
const int mod = 1e9 + 7;
const int logN = 16 + 1;
const int inf = 1e9 + 0;
const int N = 1e5 + 5;
int n, q, x, l, r;
map<int, int> F, h;
void update(int x, int y) {
  for (; x <= n; x += x & -x) F[x] += y;
}
int query(int x) {
  int sum = 0;
  for (; x > 0; x -= x & -x) sum += F[x];
  return sum;
}
int main() {
  scanf("%d %d", &n, &q);
  set<pair<int, int>, greater<pair<int, int> > > S;
  set<pair<int, int> > S2;
  S.insert(make_pair(n, 1));
  S2.insert(make_pair(1, n));
  for (int i = 1; i <= q; i++) {
    scanf("%d", &x);
    if (x == 0) {
      scanf("%d %d", &l, &r);
      printf("%d\n", query(r) - query(l - 1));
    } else {
      if (h[x]) {
        update(h[x], -1);
        int left = h[x], right = h[x];
        h[x] = 0;
        set<pair<int, int> >::iterator it = S2.lower_bound(
                                           make_pair(left, right)),
                                       it2 = it;
        if (it != S.begin()) --it2;
        if (it->first == right + 1) {
          right = it->second;
          S.erase(S.find(make_pair(it->second - it->first + 1, it->first)));
          S2.erase(it);
        }
        if (it2->second == left - 1) {
          left = it2->first;
          S.erase(S.find(make_pair(it2->second - it2->first + 1, it2->first)));
          S2.erase(it2);
        }
        S.insert(make_pair(right - left + 1, left));
        S2.insert(make_pair(left, right));
      } else {
        pair<int, int> temp = *S.begin();
        S.erase(S.begin());
        S2.erase(S2.find(make_pair(temp.second, temp.first + temp.second - 1)));
        update(h[x] = temp.first / 2 + temp.second, 1);
        if (temp.second < h[x]) {
          S.insert(make_pair(h[x] - temp.second, temp.second));
          S2.insert(make_pair(temp.second, h[x] - 1));
        }
        if (h[x] < temp.first + temp.second - 1) {
          S.insert(make_pair(temp.second + temp.first - h[x] - 1, h[x] + 1));
          S2.insert(make_pair(h[x] + 1, temp.first + temp.second - 1));
        }
      }
    }
  }
  return 0;
}
