#include <bits/stdc++.h>
using namespace std;
set<pair<int, int>, greater<pair<int, int> > > s;
map<int, pair<int, int> > con;
map<int, int> mp;
map<int, int> bit;
int n, q;
void update(int x, int val) {
  for (; x <= n; x += (x & -x)) {
    bit[x] += val;
  }
}
int query(int x) {
  int ret = 0;
  for (; x > 0; x -= (x & -x)) {
    ret += bit[x];
  }
  return ret;
}
int main() {
  scanf("%d %d", &n, &q);
  s.insert(make_pair(n, 1));
  con[1] = make_pair(n, 1);
  con[n] = make_pair(n, 1);
  con[0] = make_pair(-1, -1);
  con[n + 1] = make_pair(-1, -1);
  for (int i = 0; i < q; i++) {
    int x;
    scanf("%d", &x);
    if (!x) {
      int l, r;
      scanf("%d %d", &l, &r);
      int ans = query(r) - query(l - 1);
      printf("%d\n", ans);
    } else {
      if (!mp[x]) {
        pair<int, int> p = *s.begin();
        int l = p.second;
        int r = l + p.first - 1;
        int md;
        if ((l + r) % 2)
          md = (l + r + 1) / 2;
        else
          md = (l + r) / 2;
        update(md, 1);
        mp[x] = md;
        con[md] = make_pair(-1, -1);
        s.erase(p);
        if (l <= md - 1) {
          pair<int, int> don = make_pair(md - 1 - l + 1, l);
          s.insert(don);
          con[l] = don;
          con[md - 1] = don;
        } else {
          con[l] = make_pair(-1, -1);
        }
        if (r >= md + 1) {
          pair<int, int> don = make_pair(r - md - 1 + 1, md + 1);
          s.insert(don);
          con[r] = don;
          con[md + 1] = don;
        } else {
          con[r] = make_pair(-1, -1);
        }
      } else {
        int md = mp[x];
        update(md, -1);
        mp[x] = 0;
        int l1, r1, l2, r2;
        pair<int, int> don = con[md - 1];
        if (don.first == -1) {
          l1 = md;
          r1 = md;
        } else {
          l1 = don.second;
          r1 = l1 + don.first - 1;
          s.erase(s.find(pair<int, int>(r1 - l1 + 1, l1)));
        }
        don = con[md + 1];
        if (don.first == -1) {
          l2 = md;
          r2 = md;
        } else {
          l2 = md + 1;
          r2 = l2 + don.first - 1;
          s.erase(s.find(pair<int, int>(r2 - l2 + 1, l2)));
        }
        don = make_pair(r2 - l1 + 1, l1);
        s.insert(don);
        con[l1] = don;
        con[r2] = don;
      }
    }
  }
  return 0;
}
