#include <bits/stdc++.h>
using namespace std;
struct Hook {
  Hook() { l = r = 0; }
  Hook(int ll, int rr) {
    l = ll;
    r = rr;
  }
  int l, r;
};
struct Node {
  int dis;
};
struct Node_comp {
  bool operator()(const Hook a, const Hook b) {
    if (a.r - a.l != b.r - b.l) return a.r - a.l > b.r - b.l;
    return a.r > b.r;
  }
};
map<int, int> pre;
map<int, int> suc;
map<int, int> iden;
set<Hook, Node_comp> hs;
set<Hook>::iterator its;
int N;
int f[110000], c[110000];
int query[110000][3];
int sum(int pos) {
  int res = 0;
  while (pos > 0) {
    res += c[pos];
    pos -= pos & -pos;
  }
  return res;
}
void insert(int pos, int val) {
  while (pos <= N) {
    c[pos] += val;
    pos += pos & -pos;
  }
}
int bs(int low, int high, int val) {
  while (low <= high) {
    int mid = (low + high) >> 1;
    if (f[mid] == val) return mid;
    if (f[mid] > val)
      high = mid - 1;
    else
      low = mid + 1;
  }
  return 0;
}
int bs1(int low, int high, int val) {
  int ret = high + 1;
  while (low <= high) {
    int mid = (low + high) >> 1;
    if (val <= f[mid])
      ret = mid, high = mid - 1;
    else
      low = mid + 1;
  }
  return ret;
}
int bs2(int low, int high, int val) {
  int ret = 0;
  while (low <= high) {
    int mid = (low + high) >> 1;
    if (val >= f[mid])
      ret = mid, low = mid + 1;
    else
      high = mid - 1;
  }
  return ret;
}
int main() {
  int n, q, total = 0;
  scanf("%d%d", &n, &q);
  hs.insert(Hook(1, n));
  pre[n + 1] = 0;
  suc[0] = n + 1;
  for (int i = 0; i < q; ++i) {
    scanf("%d", &query[i][0]);
    if (query[i][0] == 0)
      scanf("%d%d", &query[i][1], &query[i][2]);
    else {
      if (iden.find(query[i][0]) == iden.end()) {
        its = hs.begin();
        int l = its->l, r = its->r;
        int pos = (l + r + 1) / 2;
        iden[query[i][0]] = pos;
        pre[r + 1] = pos;
        pre[pos] = l - 1;
        suc[l - 1] = pos;
        suc[pos] = r + 1;
        hs.erase(hs.begin());
        if (pos - 1 >= l) hs.insert(Hook(l, pos - 1));
        if (pos + 1 <= r) hs.insert(Hook(pos + 1, r));
        query[i][1] = 0;
        query[i][2] = pos;
        f[++total] = pos;
      } else {
        int pos = iden[query[i][0]];
        int l = pre[pos], r = suc[pos];
        if (l + 1 <= pos - 1) hs.erase(Hook(l + 1, pos - 1));
        if (pos + 1 <= r - 1) hs.erase(Hook(pos + 1, r - 1));
        hs.insert(Hook(l + 1, r - 1));
        suc[l] = r;
        pre[r] = l;
        pre.erase(pos);
        suc.erase(pos);
        iden.erase(query[i][0]);
        query[i][1] = 1;
        query[i][2] = pos;
      }
    }
  }
  sort(f + 1, f + 1 + total);
  N = 1;
  for (int i = 2; i <= total; ++i)
    if (f[i] != f[i - 1]) f[++N] = f[i];
  if (total == 0) N = 0;
  ++N;
  memset(c, 0, sizeof(c));
  for (int i = 0; i < q; ++i) {
    if (query[i][0] != 0) {
      int pos = bs(1, N, query[i][2]);
      if (query[i][1] == 0)
        insert(pos, 1);
      else
        insert(pos, -1);
    } else {
      int l = bs1(1, N, query[i][1]);
      int r = bs2(1, N, query[i][2]);
      int res = r >= l ? sum(r) - sum(l - 1) : 0;
      printf("%d\n", res);
    }
  }
  return 0;
}
