#include <bits/stdc++.h>
const bool debug = false;
using namespace std;
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
  out << '[' << p.first << ", " << p.second << ']';
  return out;
}
int n;
map<int, int> t;
int sum(int k) {
  int res = 0;
  for (; k >= 0; k = (k & (k + 1)) - 1) res += t[k];
  return res;
}
void add(int k, int a) {
  for (; k < n; k = (k | (k + 1))) t[k] += a;
}
int main() {
  int q;
  cin >> n >> q;
  set<pair<int, int>, greater<pair<int, int> > > B;
  B.insert(make_pair(n, 0));
  map<int, int> ves;
  map<int, pair<int, int> > pn;
  for (int i = 0; i < q; ++i) {
    int p, a, b;
    scanf("%d", &p);
    if (p == 0) {
      scanf("%d%d", &a, &b);
      if (a == 1)
        printf("%d\n", sum(b - 1));
      else
        printf("%d\n", sum(b - 1) - sum(a - 2));
    } else {
      if (ves.count(p) == 0 || ves[p] == -1) {
        int l = B.begin()->second;
        int r = B.begin()->second + B.begin()->first - 1;
        int c = (l + r + 1) / 2;
        if (debug) cout << "ves " << c << endl;
        B.erase(B.begin());
        B.insert(make_pair(c - l, l));
        B.insert(make_pair(r - c, c + 1));
        if (l >= 0) pn[l - 1].second = c - 1;
        if (r < n) pn[r + 1].first = c + 1;
        ves[p] = c;
        pn[c] = make_pair(l, r);
        add(c, 1);
      } else {
        int c = ves[p];
        int l = pn[c].first;
        int r = pn[c].second;
        ves[p] = -1;
        if (debug) cout << "UNves " << c << ' ' << l << ' ' << r << endl;
        add(c, -1);
        B.erase(make_pair((c - 1) - l + 1, l));
        B.erase(make_pair(r - (c + 1) + 1, c + 1));
        B.insert(make_pair(r - l + 1, l));
        if (l >= 0) pn[l - 1].second = r;
        if (r < n) pn[r + 1].first = l;
      }
    }
  }
  return 0;
}
