#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : -x;
}
const int MAX = 3000000;
int n;
int m, o;
int tree[MAX];
int sl[MAX], sr[MAX], L[MAX], R[MAX];
map<int, int> pr, all;
set<pair<int, pair<int, int> > > FREE;
int del(int l, int r) {
  pr.erase(l);
  pr.erase(r);
  FREE.erase(make_pair(r - l + 1, make_pair(l, r)));
  return 0;
}
int add(int l, int r) {
  if (l <= r) {
    pr[l] = r;
    pr[r] = l;
    FREE.insert(make_pair(r - l + 1, make_pair(l, r)));
  }
  return 0;
}
pair<int, int> get(int x) {
  if (pr.count(x)) {
    int y = pr[x];
    if (x > y) swap(x, y);
    return make_pair(x, y);
  }
  return make_pair(-1, -1);
}
int upd(int x, int y, int z) {
  if (L[x] > y || R[x] < y) return 0;
  if (L[x] == R[x]) {
    tree[x] += z;
    return 0;
  }
  int s = (L[x] + R[x]) / 2;
  if (y <= s) {
    if (sl[x] == -1) {
      sl[x] = o;
      L[o] = L[x];
      R[o] = s;
      sl[o] = sr[o] = -1;
      tree[o] = 0;
      o++;
    }
    upd(sl[x], y, z);
  } else {
    if (sr[x] == -1) {
      sr[x] = o;
      L[o] = s + 1;
      R[o] = R[x];
      sl[o] = sr[o] = -1;
      tree[o] = 0;
      o++;
    }
    upd(sr[x], y, z);
  }
  tree[x] += z;
  return 0;
}
int get(int x, int l, int r) {
  if (L[x] > r || R[x] < l) return 0;
  if (L[x] >= l && R[x] <= r) return tree[x];
  int res = 0;
  if (sl[x] != -1) res += get(sl[x], l, r);
  if (sr[x] != -1) res += get(sr[x], l, r);
  return res;
}
int main() {
  scanf("%d%d", &m, &n);
  o = 1;
  L[0] = 1;
  R[0] = m;
  sl[0] = sr[0] = -1;
  tree[0] = 0;
  FREE.insert(make_pair(m, make_pair(1, m)));
  pr[1] = m;
  pr[m] = 1;
  for (int i = 0; i < n; i++) {
    int x, y, l, r;
    scanf("%d", &x);
    if (x == 0) {
      scanf("%d%d", &l, &r);
      printf("%d\n", get(0, l, r));
    } else {
      if (all.count(x)) {
        y = all[x];
        upd(0, y, -1);
        all.erase(x);
        pair<int, int> al = get(y - 1);
        pair<int, int> ar = get(y + 1);
        l = r = y;
        if (al.first != -1) {
          del(al.first, al.second);
          l = al.first;
        }
        if (ar.first != -1) {
          del(ar.first, ar.second);
          r = ar.second;
        }
        add(l, r);
      } else {
        pair<int, pair<int, int> > cur = *FREE.rbegin();
        l = cur.second.first;
        r = cur.second.second;
        del(l, r);
        int y = (l + r + 1) / 2;
        upd(0, y, 1);
        add(l, y - 1);
        add(y + 1, r);
        all[x] = y;
      }
    }
  }
  return 0;
}
