#include <bits/stdc++.h>
using namespace std;
namespace Treap {
struct node {
  int v;
  int pr, sz, fr;
  node *l, *r;
  static node* nil;
  node() {
    memset(this, 0, sizeof(*this));
    l = r = this;
  }
  node(int vv) : v(vv), pr(rand() + 1), l(nil), r(nil), sz(1), fr(1) {}
  inline void update() { sz = l->sz + r->sz + fr; }
};
node* node::nil = new node();
inline void rotate_right(node*& root) {
  node* t = root->l;
  root->l = t->r;
  t->r = root;
  root->update();
  t->update();
  root = t;
}
inline void rotate_left(node*& root) {
  node* t = root->r;
  root->r = t->l;
  t->l = root;
  root->update();
  t->update();
  root = t;
}
inline void balance(node*& root) {
  if (root->pr < root->l->pr)
    rotate_right(root);
  else if (root->pr < root->r->pr)
    rotate_left(root);
}
inline node* insert(node* root, int& v) {
  if (root == node::nil) return new node(v);
  if (root->v > v)
    root->l = insert(root->l, v);
  else if (root->v < v)
    root->r = insert(root->r, v);
  else {
    root->fr++, root->sz++;
    return root;
  }
  balance(root);
  root->update();
  return root;
}
inline node* erase(node* root, int& v) {
  if (root == node::nil) return node::nil;
  if (root->v < v)
    root->r = erase(root->r, v);
  else if (root->v > v)
    root->l = erase(root->l, v);
  else {
    if (root->fr == 1) {
      if (root->l == node::nil && root->r == node::nil) {
        delete root;
        return node::nil;
      }
      if (root->l == node::nil) {
        node* t = root->r;
        delete root;
        return t;
      }
      if (root->r == node::nil) {
        node* t = root->l;
        delete root;
        return t;
      }
      (root->l->pr > root->r->pr) ? rotate_right(root) : rotate_left(root);
      root = erase(root, v);
    } else {
      root->fr--, root->sz--;
      return root;
    }
  }
  root->update();
  return root;
}
inline int lower_bound(node* root, int v) {
  if (root == node::nil) return 0;
  if (root->v > v) return lower_bound(root->l, v);
  if (root->v == v) return root->l->sz;
  return lower_bound(root->r, v) + root->l->sz + root->fr;
}
inline int upper_bound(node* root, int v) {
  int res = 0;
  while (root != Treap::node::nil) {
    if (root->v > v) {
      root = root->l;
      continue;
    }
    if (root->v == v) return res + root->l->sz + root->fr;
    res += root->l->sz + root->fr;
    root = root->r;
  }
  return res;
}
}  // namespace Treap
Treap::node* root;
struct cmplarge {
  bool operator()(const pair<int, int>& a, const pair<int, int>& b) const {
    int d1 = a.second - a.first + 1;
    int d2 = b.second - b.first + 1;
    if (d1 == d2) return a.first > b.first;
    return d1 > d2;
  }
};
set<pair<int, int> > active;
set<pair<int, int>, cmplarge> large;
map<int, int> pos;
void hang(int x) {
  pair<int, int> oldint = *large.begin();
  large.erase(large.begin());
  active.erase(oldint);
  int npos = oldint.first + (oldint.second - oldint.first + 1) / 2;
  pair<int, int> np1(oldint.first, npos - 1), np2(npos + 1, oldint.second);
  pos[x] = npos;
  if (np1.first <= np1.second) active.insert(np1);
  if (np2.first <= np2.second) active.insert(np2);
  if (np1.first <= np1.second) large.insert(np1);
  if (np2.first <= np2.second) large.insert(np2);
  root = Treap::insert(root, npos);
}
void unhang(int x) {
  int oldpos = pos[x];
  set<pair<int, int> >::iterator it =
      active.lower_bound(make_pair(oldpos + 1, -1));
  set<pair<int, int> >::iterator it1 = it;
  if (it != active.begin()) it1--;
  pair<int, int> npos;
  int fl = 0, fl1 = 0;
  if (it->first != oldpos + 1)
    npos.second = oldpos;
  else
    npos.second = it->second, fl = 1;
  if (it1->second != oldpos - 1)
    npos.first = oldpos;
  else
    npos.first = it1->first, fl1 = 1;
  if (fl) large.erase(*it);
  if (fl1) large.erase(*it1);
  if (fl) active.erase(*it);
  if (fl1) active.erase(*it1);
  active.insert(npos);
  large.insert(npos);
  pos.erase(x);
  root = Treap::erase(root, oldpos);
}
int query(int st, int en) {
  int x = Treap::lower_bound(root, st);
  int y = Treap::upper_bound(root, en);
  return y - x;
}
int main() {
  int n, m, x, y;
  scanf("%d %d", &n, &m);
  active.insert(make_pair(1, n));
  large.insert(make_pair(1, n));
  root = Treap::node::nil;
  for (int i = (0); i < (m); ++i) {
    scanf("%d", &x);
    if (!x) {
      scanf("%d %d", &x, &y);
      printf("%d\n", query(x, y));
    } else {
      if (pos.count(x))
        unhang(x);
      else
        hang(x);
    }
  }
  return 0;
}
