#include <bits/stdc++.h>
using namespace std;
const int maxT = 32 * 100000;
int n, Q, cnt;
map<int, int> pla;
struct range {
  int s, e;
  range(int s = -1, int e = -1) : s(s), e(e) {}
};
struct empcmp {
  inline bool operator()(const range &a1, const range &a2) const {
    return a1.s < a2.s;
  }
};
struct sizcmp {
  inline bool operator()(const range &a1, const range &a2) const {
    return (a1.e - a1.s != a2.e - a2.s) ? (a1.e - a1.s > a2.e - a2.s)
                                        : (a1.s > a2.s);
  }
};
set<range, empcmp> emp;
set<range, sizcmp> siz;
struct node {
  int s, e, sum;
  int l, r;
  node(int s = 0, int e = 0, int sum = 0, int l = -1, int r = -1)
      : s(s), e(e), sum(sum), l(l), r(r) {}
};
node t[maxT];
inline void INSERT(range a) {
  emp.insert(a);
  siz.insert(a);
}
inline void ERASE(range a) {
  emp.erase(a);
  siz.erase(a);
}
inline int newCoat() {
  range top = *siz.begin();
  ERASE(top);
  if (top.s == top.e) return top.s;
  if (top.e - top.s == 1) {
    INSERT(range(top.s, top.s));
    return top.e;
  }
  int select = (top.s + top.e + 1) / 2;
  INSERT(range(top.s, select - 1));
  INSERT(range(select + 1, top.e));
  return select;
}
inline void pickCoat(int pos) {
  if (emp.empty()) {
    INSERT(range(pos, pos));
    return;
  }
  set<range>::iterator it = emp.lower_bound(range(pos, pos));
  if (it == emp.begin()) {
    if (it->s == (pos + 1)) {
      range tmp = *it;
      ERASE(tmp);
      INSERT(range(pos, tmp.e));
    } else
      INSERT(range(pos, pos));
    return;
  }
  if (it == emp.end()) {
    it--;
    if (it->e == pos - 1) {
      range tmp = *it;
      ERASE(tmp);
      INSERT(range(tmp.s, pos));
    } else
      INSERT(range(pos, pos));
    return;
  }
  range next = *it;
  range prev = *(--it);
  int s = pos, e = pos;
  if (prev.e == pos - 1) {
    ERASE(prev);
    s = prev.s;
  }
  if (next.s == pos + 1) {
    ERASE(next);
    e = next.e;
  }
  INSERT(range(s, e));
}
inline int getSum(int root, int s, int e) {
  if (root == -1 || e < t[root].s || t[root].e < s) return 0;
  if (s <= t[root].s && t[root].e <= e) return t[root].sum;
  return getSum(t[root].l, s, e) + getSum(t[root].r, s, e);
}
inline void add(int root, int pos, int key) {
  t[root].sum += key;
  if (t[root].s == t[root].e) return;
  int mid = (t[root].s + t[root].e) / 2;
  if (pos <= mid) {
    if (t[root].l == -1) {
      t[t[root].l = ++cnt] = node(t[root].s, mid, 0);
    }
    add(t[root].l, pos, key);
  } else {
    if (t[root].r == -1) {
      t[t[root].r = ++cnt] = node(mid + 1, t[root].e, 0);
    }
    add(t[root].r, pos, key);
  }
}
int main() {
  scanf("%d%d", &n, &Q);
  INSERT(range(1, n));
  vector<range> que;
  t[++cnt] = node(1, n, 0);
  while (Q) {
    int ord;
    scanf("%d", &ord);
    if (ord == 0) {
      int s, e;
      scanf("%d%d", &s, &e);
      printf("%d\n", getSum(1, s, e));
      Q--;
      continue;
    }
    if (pla[ord] == 0)
      add(1, pla[ord] = newCoat(), 1);
    else {
      pickCoat(pla[ord]);
      add(1, pla[ord], -1);
      pla[ord] = 0;
    }
    Q--;
  }
  return 0;
}
