#include <bits/stdc++.h>
using namespace std;
struct Segment {
  int st, ed;
  Segment(int st, int ed) : st(st), ed(ed) {}
};
bool operator<(const Segment &A, const Segment &B) {
  if (A.ed - A.st != B.ed - B.st) {
    return A.ed - A.st > B.ed - B.st;
  }
  return A.st > B.st;
}
map<int, int> C;
int n, q;
inline int lowbit(int x) { return x & (-x); }
void insert(int x, int val) {
  while (x <= n) {
    C[x] += val;
    x += lowbit(x);
  }
}
int query(int x) {
  int ans = 0;
  while (x > 0) {
    ans += C[x];
    x -= lowbit(x);
  }
  return ans;
}
int query(int x, int y) { return query(y) - query(x - 1); }
int main() {
  for (; ~scanf("%d%d", &n, &q);) {
    C.clear();
    set<Segment> iset;
    iset.insert(Segment(1, n));
    set<int> pt;
    pt.insert(0);
    pt.insert(n + 1);
    map<int, int> mp;
    int p, st, ed;
    while (q--) {
      scanf("%d", &p);
      if (p == 0) {
        scanf("%d%d", &st, &ed);
        printf("%d\n", query(st, ed));
      } else {
        if (mp[p] == 0) {
          set<Segment>::iterator it = iset.begin();
          st = it->st, ed = it->ed;
          iset.erase(it);
          int m = (st + ed + 1) / 2;
          mp[p] = m;
          pt.insert(m);
          insert(m, 1);
          if (st <= m - 1) {
            iset.insert(Segment(st, m - 1));
          }
          if (m + 1 <= ed) {
            iset.insert(Segment(m + 1, ed));
          }
        } else {
          int m = mp[p];
          mp[p] = 0;
          set<int>::iterator left, right;
          left = pt.lower_bound(m);
          left--;
          right = pt.upper_bound(m);
          pt.erase(m);
          insert(m, -1);
          st = (*left) + 1;
          ed = (*right) - 1;
          if (st <= m - 1) {
            iset.erase(Segment(st, m - 1));
          }
          if (m + 1 <= ed) {
            iset.erase(Segment(m + 1, ed));
          }
          if (st <= ed) {
            iset.insert(Segment(st, ed));
          }
        }
      }
    }
  }
  return 0;
}
