#include <bits/stdc++.h>
using namespace std;
typedef struct {
  int pr, k;
  int v, s;
  int p, e, d;
} NODE;
const int TSZ = 1000000;
const int RND = 1000000000;
const int INF = 1000000000;
int root, size;
NODE tree[TSZ];
int newnode(int k, int v) {
  tree[size].pr = rand() % RND;
  tree[size].k = k;
  tree[size].v = v;
  tree[size].p = tree[size].e = tree[size].d = -1;
  return size++;
}
void right_rotation(int p) {
  int t = tree[p].e;
  tree[p].e = tree[t].d;
  if (tree[t].d != -1) tree[tree[t].d].p = p;
  tree[t].p = tree[p].p;
  if (tree[p].p != -1) {
    if (tree[tree[p].p].e == p)
      tree[tree[p].p].e = t;
    else
      tree[tree[p].p].d = t;
  }
  tree[t].d = p;
  tree[p].p = t;
}
void left_rotation(int p) {
  int t = tree[p].d;
  tree[p].d = tree[t].e;
  if (tree[t].e != -1) tree[tree[t].e].p = p;
  tree[t].p = tree[p].p;
  if (tree[p].p != -1) {
    if (tree[tree[p].p].e == p)
      tree[tree[p].p].e = t;
    else
      tree[tree[p].p].d = t;
  }
  tree[t].e = p;
  tree[p].p = t;
}
void repair(int t) {
  while (tree[t].p != -1) {
    int p = tree[t].p;
    if (tree[t].pr > tree[p].pr) {
      if (tree[p].e == t)
        right_rotation(p);
      else
        left_rotation(p);
      swap(t, p);
      tree[t].s = tree[t].v + ((tree[t].e) >= 0 ? tree[tree[t].e].s : 0) +
                  ((tree[t].d) >= 0 ? tree[tree[t].d].s : 0);
      tree[p].s = tree[p].v + ((tree[p].e) >= 0 ? tree[tree[p].e].s : 0) +
                  ((tree[p].d) >= 0 ? tree[tree[p].d].s : 0);
    } else
      tree[t].s = tree[t].v + ((tree[t].e) >= 0 ? tree[tree[t].e].s : 0) +
                  ((tree[t].d) >= 0 ? tree[tree[t].d].s : 0);
    t = p;
  }
  root = t;
  tree[t].s = tree[t].v + ((tree[t].e) >= 0 ? tree[tree[t].e].s : 0) +
              ((tree[t].d) >= 0 ? tree[tree[t].d].s : 0);
}
void update(int k, int v) {
  if (root == -1) {
    repair(root = newnode(k, v));
    return;
  }
  int t = root;
  while (k != tree[t].k) {
    if (k < tree[t].k) {
      if (tree[t].e == -1) break;
      t = tree[t].e;
    } else if (k > tree[t].k) {
      if (tree[t].d == -1) break;
      t = tree[t].d;
    }
  }
  if (k == tree[t].k) {
    tree[t].v = v;
    repair(t);
    return;
  }
  int nd = newnode(k, v);
  tree[nd].p = t;
  if (k < tree[t].k)
    tree[t].e = nd;
  else
    tree[t].d = nd;
  repair(nd);
}
int read(int n, int e, int d, int x, int y) {
  if (n == -1) return 0;
  if (d < x or y < e) return 0;
  if (x <= e and d <= y) return tree[n].s;
  int r = (x <= tree[n].k and tree[n].k <= y ? tree[n].v : 0);
  int a = read(tree[n].e, e, tree[n].k - 1, x, y);
  int b = read(tree[n].d, tree[n].k + 1, d, x, y);
  return r + a + b;
}
priority_queue<pair<int, pair<int, int> > > cua;
map<pair<int, pair<int, int> >, int> del;
pair<int, pair<int, int> > cua_pop() {
  while (true) {
    pair<int, pair<int, int> > p = cua.top();
    cua.pop();
    if (del[p] > 0)
      --del[p];
    else
      return p;
  }
}
int main() {
  root = -1;
  int n, q;
  cin >> n >> q;
  cua.push(pair<int, pair<int, int> >(n, pair<int, int>(1, n)));
  map<int, int> dins;
  set<int> usat;
  usat.insert(0);
  usat.insert(n + 1);
  while (q-- > 0) {
    int t;
    cin >> t;
    if (t == 0) {
      int a, b;
      cin >> a >> b;
      cout << read(root, -INF, INF, a, b) << endl;
    } else {
      if (dins.find(t) == dins.end()) {
        pair<int, pair<int, int> > pp = cua_pop();
        int e = pp.second.first, d = pp.second.second;
        int p = (e + d) / 2 + (e + d) % 2;
        dins[t] = p;
        int e1 = e, d1 = p - 1;
        int e2 = p + 1, d2 = d;
        int m1 = d1 - e1 + 1, m2 = d2 - e2 + 1;
        if (m1 > 0)
          cua.push(pair<int, pair<int, int> >(m1, pair<int, int>(e1, d1)));
        if (m2 > 0)
          cua.push(pair<int, pair<int, int> >(m2, pair<int, int>(e2, d2)));
        usat.insert(p);
        update(p, 1);
      } else {
        int p = dins[t];
        dins.erase(t);
        set<int>::iterator it = usat.find(p);
        set<int>::iterator it1 = it;
        --it1;
        set<int>::iterator it2 = it;
        ++it2;
        int e = *it1 + 1, d = *it2 - 1;
        int m = d - e + 1;
        cua.push(pair<int, pair<int, int> >(m, pair<int, int>(e, d)));
        int e1 = e, d1 = p - 1;
        int e2 = p + 1, d2 = d;
        int m1 = d1 - e1 + 1, m2 = d2 - e2 + 1;
        if (m1 > 0)
          ++del[pair<int, pair<int, int> >(m1, pair<int, int>(e1, d1))];
        if (m2 > 0)
          ++del[pair<int, pair<int, int> >(m2, pair<int, int>(e2, d2))];
        usat.erase(p);
        update(p, 0);
      }
    }
  }
}
