#include <bits/stdc++.h>
using namespace std;
struct Tline {
  int L, R;
  bool operator<(const Tline &a) const {
    if (R - L > a.R - a.L) return (true);
    if (R - L < a.R - a.L) return (false);
    return (R > a.R);
  }
};
const int maxn = 1000007;
int m, n, tot, sign;
int L[maxn], R[maxn];
map<Tline, int> Line;
map<int, int> num;
map<int, int> TN;
map<int, int> BIT;
void insert(int x, int val) {
  for (int i = x; i <= n; i += i & (-i)) BIT[i] += val;
}
int count(int x) {
  int res = 0;
  for (int i = x; i >= 1; i -= i & (-i))
    if (BIT.find(i) != BIT.end()) res += BIT[i];
  return (res);
}
void work() {
  scanf("%d %d", &n, &m);
  Tline Li;
  Li.L = 1;
  Li.R = n;
  Line[Li] = 0;
  int number, fir, lst;
  sign = 0;
  TN[1] = 1;
  TN[n] = 2;
  tot = 2;
  R[TN[1]] = n;
  L[TN[n]] = 1;
  for (int i = 1; i <= m; i++) {
    scanf("%d", &number);
    if (number > 0) {
      if (num.find(number) == num.end()) {
        Tline Li = Line.begin()->first, LL, RR;
        Line.erase(Line.begin());
        int mid = Li.L + (Li.R - Li.L + 1) / 2;
        num[number] = mid;
        if (TN.find(mid - 1) == TN.end()) TN[mid - 1] = ++tot;
        if (TN.find(mid + 1) == TN.end()) TN[mid + 1] = ++tot;
        LL.L = Li.L;
        LL.R = mid - 1;
        RR.L = mid + 1;
        RR.R = Li.R;
        R[TN[LL.L]] = LL.R;
        L[TN[RR.R]] = RR.L;
        L[TN[LL.R]] = LL.L;
        R[TN[RR.L]] = RR.R;
        Line[LL] = 0;
        Line[RR] = 0;
        insert(mid, 1);
      } else {
        int mid = num[number];
        num.erase(num.find(number));
        Tline LL, RR, Li;
        LL.R = mid - 1;
        RR.L = mid + 1;
        LL.L = L[TN[LL.R]];
        RR.R = R[TN[RR.L]];
        Li.L = LL.L;
        Li.R = RR.R;
        R[TN[Li.L]] = Li.R;
        L[TN[Li.R]] = Li.L;
        Line.erase(Line.find(LL));
        Line.erase(Line.find(RR));
        Line[Li] = 0;
        insert(mid, -1);
      }
    } else {
      scanf("%d %d", &fir, &lst);
      int res = count(lst) - count(fir - 1);
      printf("%d\n", res);
    }
  }
}
int main() { work(); }
