#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) { return a > b ? a : b; }
const int NN = 55;
const long long MaxVal = 2222222222LL;
map<long long, int> tree;
map<long long, long long> Map;
set<long long> used;
set<long long> Set;
struct coat {
  long long lo, up;
  coat(){};
  coat(long long A, long long B) { lo = A, up = B; }
  friend bool operator<(const coat &A, const coat &B) {
    if (A.up - A.lo != B.up - B.lo) return A.up - A.lo > B.up - B.lo;
    return A.lo > B.lo;
  }
};
set<coat> Max;
long long read(long long idx) {
  int sum = 0;
  while (idx > 0) {
    sum += tree[idx];
    idx -= (idx & -idx);
  }
  return sum;
}
void update(long long idx, int val) {
  while (idx <= MaxVal) {
    tree[idx] += val;
    idx += (idx & -idx);
  }
}
string s;
int main() {
  int n, q;
  cin >> n >> q;
  Max.insert(coat(0, n + 1));
  Set.insert(0);
  Set.insert(n + 1);
  while (q--) {
    int x;
    cin >> x;
    if (x == 0) {
      int A, B;
      cin >> A >> B;
      cout << read(B) - read(A - 1) << endl;
    } else {
      if (used.find(x) != used.end()) {
        used.erase(x);
        long long mid = Map[x];
        set<long long>::iterator Iter = Set.lower_bound(mid);
        Iter--;
        if (Iter == Set.end()) Iter++;
        long long be = (*Iter);
        ++Iter;
        ++Iter;
        if (Iter == Set.end()) Iter--;
        long long en = (*Iter);
        update(mid, -1);
        Max.erase(coat(be, mid));
        Max.erase(coat(mid, en));
        if (be + 1 <= en - 1) Max.insert(coat(be, en));
        Set.erase(mid);
      } else {
        coat r = (*Max.begin());
        pair<long long, long long> p = make_pair(r.lo, r.up);
        Max.erase(Max.begin());
        long long mid = p.first + (p.second - p.first - 1) / 2 + 1;
        if (mid > n) mid = n;
        if (p.first + 1 <= mid - 1) Max.insert(coat(p.first, mid));
        if (mid + 1 <= p.second - 1) Max.insert(coat(mid, p.second));
        update(mid, +1);
        used.insert(x);
        Set.insert(mid);
        Map[x] = mid;
      }
    }
  }
  return 0;
}
