#include <bits/stdc++.h>
using namespace std;
const int N = 400005 * 2;
struct node {
  int L, R;
  node(int L, int R) : L(L), R(R) {}
};
struct pqnode {
  int size, L;
  node *n;
  pqnode(node *k) : size(k->R - k->L + 1), L(k->L), n(k) {}
  bool ok() const { return n->L != -1; }
  bool operator<(const pqnode &p) const {
    return size < p.size || (size == p.size && L < p.L);
  }
};
int n, q;
int qu[N], qi[N], qj[N];
node *tree;
int cmp[N], size;
map<int, node *> m;
map<int, int> in;
priority_queue<pqnode> pq;
int fen[N];
void add(int i, int x) {
  for (++i; i <= size; i += i & -i) fen[i] += x;
}
int sum(int i) {
  int s = 0;
  for (++i; i > 0; i -= i & -i) s += fen[i];
  return s;
}
int sum(int a, int b) { return sum(b) - sum(a - 1); }
int getid(int x) { return lower_bound(cmp, cmp + size, x) - cmp; }
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 0; i < q; ++i) {
    scanf("%d", &qu[i]);
    if (qu[i] == 0) {
      scanf("%d%d", qi + i, qj + i);
      cmp[size++] = qi[i];
      cmp[size++] = qj[i] + 1;
    }
  }
  cmp[size++] = 0;
  sort(cmp, cmp + size);
  size = unique(cmp, cmp + size) - cmp;
  node *base = new node(1, n);
  m[1] = base;
  pq.push(base);
  for (int z = 0; z < q; ++z) {
    int u = qu[z], i = qi[z], j = qj[z];
    if (u == 0) {
      int a = getid(i);
      int b = getid(j + 1);
      printf("%d\n", sum(a, b - 1));
    } else {
      if (!in.count(u)) {
        while (pq.top().ok() == false) {
          pq.pop();
        }
        pqnode p = pq.top();
        pq.pop();
        m.erase(p.L);
        int L = p.n->L;
        int R = p.n->R;
        delete p.n;
        int M = (L + R + 1) / 2;
        if (L <= M - 1) {
          node *left = new node(L, M - 1);
          m[L] = left;
          pq.push(left);
        }
        if (M + 1 <= R) {
          node *right = new node(M + 1, R);
          m[M + 1] = right;
          pq.push(right);
        }
        in[u] = M;
        int ub = getid(M + 1);
        add(ub - 1, 1);
      } else {
        int M = in[u];
        in.erase(u);
        int ub = getid(M + 1);
        add(ub - 1, -1);
        int L = M, R = M;
        map<int, node *>::iterator rit = m.lower_bound(M);
        map<int, node *>::iterator lit = rit;
        if (lit == m.begin())
          lit = m.end();
        else
          --lit;
        bool eraseRight = false;
        if (rit != m.end() && rit->first == M + 1) {
          R = rit->second->R;
          rit->second->L = -1;
          rit->second->R = -1;
          eraseRight = true;
        }
        bool eraseLeft = false;
        if (lit != m.end() && lit->second->R == M - 1) {
          L = lit->second->L;
          lit->second->L = -1;
          lit->second->R = -1;
          eraseLeft = true;
        }
        if (eraseLeft) m.erase(lit);
        if (eraseRight) m.erase(m.lower_bound(M));
        node *ext = new node(L, R);
        m[L] = (ext);
        pq.push(ext);
      }
    }
  }
}
