#include <bits/stdc++.h>
using namespace std;
const int dr[] = {0, -1, 0, 1, -1, 1, 1, -1};
const int dc[] = {1, 0, -1, 0, 1, 1, -1, -1};
const double eps = 1e-9;
template <class T>
void print(const vector<T> &v) {
  ostringstream os;
  for (int i = 0; i < (int)v.size(); ++i) {
    if (i) os << ' ';
    os << v[i];
  }
  cout << os.str() << endl;
}
template <class T>
int sz(const T &c) {
  return (int)c.size();
}
template <class T>
void srt(T &c) {
  sort(c.begin(), c.end());
}
template <class T>
void checkmin(T &a, T b) {
  if (b < a) a = b;
}
template <class T>
void checkmax(T &a, T b) {
  if (b > a) a = b;
}
template <class T>
T sqr(T x) {
  return x * x;
}
template <class T, class U>
T cast(U x) {
  ostringstream os;
  os << x;
  T res;
  istringstream is(os.str());
  is >> res;
  return res;
}
template <class T>
vector<T> split(string s, string x = " ") {
  vector<T> res;
  for (int i = 0; i < s.size(); i++) {
    string a;
    while (i < (int)s.size() && x.find(s[i]) == string::npos) a += s[i++];
    if (!a.empty()) res.push_back(cast<T>(a));
  }
  return res;
}
template <class T>
bool inside(T r, T c, T R, T C) {
  return r >= 0 && r < R && c >= 0 && c < C;
}
struct segment {
  int left, right, len;
  segment(int a, int b) : left(a), right(b), len(b - a) {}
};
bool operator<(const segment &a, const segment &b) {
  if (a.len != b.len) return a.len > b.len;
  return a.left > b.left;
}
map<int, int> t;
int n;
void add(int i, int value) {
  for (; i < n; i |= i + 1) t[i] += value;
}
int sum(int i) {
  int res = 0;
  for (; i >= 0; i -= -(i + 1) & (i + 1))
    if (t.count(i)) res += t[i];
  return res;
}
int sum(int a, int b) { return sum(b) - sum(a - 1); }
int main(int argc, char *argv[]) {
  int m;
  scanf("%d%d", &n, &m);
  set<segment> q;
  q.insert(segment(1, n));
  set<int> points;
  points.insert(0);
  points.insert(n + 1);
  map<int, int> pos;
  while (m--) {
    int x;
    scanf("%d", &x);
    if (x == 0) {
      int a, b;
      scanf("%d%d", &a, &b);
      printf("%d\n", sum(a - 1, b - 1));
    } else {
      if (pos[x] == 0) {
        segment s = *q.begin();
        q.erase(s);
        int mid = (s.left + s.right + 1) / 2;
        pos[x] = mid;
        points.insert(mid);
        add(mid - 1, 1);
        if (s.left <= mid - 1) q.insert(segment(s.left, mid - 1));
        if (mid + 1 <= s.right) q.insert(segment(mid + 1, s.right));
      } else {
        int mid = pos[x];
        set<int>::iterator it = points.find(mid);
        int pre = *--it;
        int nxt = *++ ++it;
        if (pre + 1 <= mid - 1) q.erase(segment(pre + 1, mid - 1));
        if (mid + 1 <= nxt - 1) q.erase(segment(mid + 1, nxt - 1));
        if (pre + 1 <= nxt - 1) q.insert(segment(pre + 1, nxt - 1));
        points.erase(mid);
        add(mid - 1, -1);
        pos[x] = 0;
      }
    }
  }
  return 0;
}
