#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
int qq[MAXN][3];
map<int, int> num;
map<int, int> pleft;
map<int, int> pright;
map<int, int> used;
int tt[4 * 3 * MAXN];
void update(int v, int tl, int tr, int pos, int val) {
  if (tl == tr) {
    tt[v] += val;
    return;
  }
  int tm = (tl + tr) >> 1;
  if (pos <= tm) {
    update(2 * v + 1, tl, tm, pos, val);
  } else {
    update(2 * v + 2, tm + 1, tr, pos, val);
  }
  tt[v] = tt[2 * v + 1] + tt[2 * v + 2];
}
int get_sum(int v, int tl, int tr, int l, int r) {
  if (l > r) return 0;
  if (tl == l && tr == r) {
    return tt[v];
  }
  int tm = (tl + tr) >> 1;
  return get_sum(2 * v + 1, tl, tm, l, min(r, tm)) +
         get_sum(2 * v + 2, tm + 1, tr, max(l, tm + 1), r);
}
void solve() {
  set<pair<int, int> > st;
  int n, q;
  scanf("%d%d", &n, &q);
  st.insert(make_pair(-n, -1));
  pleft[n] = 1;
  pright[1] = n;
  int v;
  for (int i = 0; i < q; i++) {
    scanf("%d", &v);
    if (!v) {
      scanf("%d%d", &qq[i][1], &qq[i][2]);
    } else {
      qq[i][0] = v;
      if (num.find(v) != num.end()) {
        if (pright.find(num[v] + 1) != pright.end()) {
          if (pleft.find(num[v] - 1) != pleft.end()) {
            int l = pleft[num[v] - 1];
            int r = pright[num[v] + 1];
            st.erase(make_pair(-(num[v] - l), -l));
            st.erase(make_pair(-(r - num[v]), -(num[v] + 1)));
            st.insert(make_pair(-(r - l + 1), -l));
            pleft.erase(num[v] - 1);
            pright.erase(num[v] + 1);
            pright[l] = r;
            pleft[r] = l;
          } else {
            int r = pright[num[v] + 1];
            st.erase(make_pair(-(r - num[v]), -(num[v] + 1)));
            st.insert(make_pair(-(r - num[v] + 1), -num[v]));
            pright.erase(num[v] + 1);
            pleft[r] = num[v];
            pright[num[v]] = r;
          }
        } else {
          if (pleft.find(num[v] - 1) != pleft.end()) {
            int l = pleft[num[v] - 1];
            st.erase(make_pair(-(num[v] - l), -l));
            st.insert(make_pair(-(num[v] - l + 1), -l));
            pleft.erase(num[v] - 1);
            pright[l] = num[v];
            pleft[num[v]] = l;
          } else {
            st.insert(make_pair(-1, -num[v]));
            pleft[num[v]] = num[v];
            pright[num[v]] = num[v];
          }
        }
        num.erase(v);
      } else {
        pair<int, int> p = *st.begin();
        st.erase(st.begin());
        int len = -p.first;
        int lf = -p.second;
        pleft.erase(lf + len - 1);
        pright.erase(lf);
        num[v] = lf + len / 2;
        if (num[v] > lf) {
          pright[lf] = num[v] - 1;
          pleft[num[v] - 1] = lf;
          st.insert(make_pair(-(num[v] - lf), -lf));
        }
        if (lf + len - 1 > num[v]) {
          pright[num[v] + 1] = lf + len - 1;
          pleft[lf + len - 1] = num[v] + 1;
          st.insert(make_pair(-(lf + len - 1 - num[v] - 1 + 1), -(num[v] + 1)));
        }
        qq[i][1] = num[v];
        used[num[v]] = 1;
        if (0) cout << i << " " << num[v] << endl;
      }
    }
  }
  for (int i = 0; i < q; i++) {
    if (!qq[i][0]) {
      used[qq[i][1]] = 1;
      used[qq[i][2]] = 1;
    }
  }
  int top = 0;
  map<int, int>::iterator it;
  for (it = used.begin(); it != used.end(); it++) {
    it->second = top++;
  }
  int cnt = 1;
  while (cnt < top) cnt <<= 1;
  for (int i = 0; i < q; i++) {
    if (qq[i][0]) {
      num.erase(qq[i][0]);
    }
  }
  for (int i = 0; i < q; i++) {
    if (qq[i][0]) {
      v = qq[i][0];
      if (num.find(v) != num.end()) {
        update(0, cnt - 1, 2 * cnt - 2, cnt - 1 + used[num[v]], -1);
        num.erase(v);
      } else {
        num[v] = qq[i][1];
        update(0, cnt - 1, 2 * cnt - 2, cnt - 1 + used[num[v]], 1);
      }
    } else {
      int sum = get_sum(0, cnt - 1, 2 * cnt - 2, cnt - 1 + used[qq[i][1]],
                        cnt - 1 + used[qq[i][2]]);
      printf("%d\n", sum);
    }
  }
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
