#include <bits/stdc++.h>
using namespace std;
struct cmp {
  bool operator()(pair<int, pair<int, int> > a, pair<int, pair<int, int> > b) {
    return a > b;
  }
};
int n, q, x, l, r;
unordered_map<int, bool> is;
unordered_map<int, int> fen, loc;
set<pair<int, int> > st;
set<pair<int, pair<int, int> >, cmp> st_len;
void add(int dex, int val) {
  for (; dex <= n; dex += (dex & -dex)) fen[dex] += val;
}
int get(int dex) { return (dex ? fen[dex] + get(dex ^ (dex & -dex)) : 0); }
void ADD(pair<int, int> p) {
  st.insert(p);
  st_len.insert(pair<int, pair<int, int> >(p.second - p.first, p));
}
void DEL(pair<int, int> p) {
  st.erase(p);
  st_len.erase(pair<int, pair<int, int> >(p.second - p.first, p));
}
void add_emp(int x) {
  pair<int, int> p = (*st_len.begin()).second;
  DEL(p);
  int dex = (p.first - 1) + (p.second - p.first + 1) / 2 + 1;
  if (dex != p.first) ADD(pair<int, int>(p.first, dex - 1));
  if (dex != p.second) ADD(pair<int, int>(dex + 1, p.second));
  loc[x] = dex;
  add(dex, 1);
}
void del_emp(int x) {
  int dex = loc[x];
  add(dex, -1);
  if (st.lower_bound(pair<int, int>(dex, dex)) == st.end()) {
    pair<int, int> p = *(--st.lower_bound(pair<int, int>(dex, dex)));
    if (p.second + 1 == dex) {
      DEL(p);
      ADD(pair<int, int>(p.first, dex));
    } else
      ADD(pair<int, int>(dex, dex));
  } else if (st.lower_bound(pair<int, int>(dex, dex)) == st.begin()) {
    pair<int, int> p = *st.begin();
    if (p.first - 1 == dex) {
      DEL(p);
      ADD(pair<int, int>(dex, p.second));
    } else
      ADD(pair<int, int>(dex, dex));
  } else {
    pair<int, int> p1 = *(--st.lower_bound(pair<int, int>(dex, dex))),
                   p2 = *st.lower_bound(pair<int, int>(dex, dex));
    if (p1.second + 1 == p2.first - 1) {
      DEL(p1);
      DEL(p2);
      ADD(pair<int, int>(p1.first, p2.second));
    } else if (p1.second + 1 == dex)
      DEL(p1), ADD(pair<int, int>(p1.first, dex));
    else if (p2.first - 1 == dex)
      DEL(p2), ADD(pair<int, int>(dex, p2.second));
    else
      ADD(pair<int, int>(dex, dex));
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> q;
  ADD(pair<int, int>(1, n));
  while (q--) {
    cin >> x;
    if (x) {
      if (is[x])
        del_emp(x);
      else
        add_emp(x);
      is[x] ^= true;
    } else {
      cin >> l >> r;
      cout << get(r) - get(l - 1) << '\n';
    }
  }
  return 0;
}
