#include <bits/stdc++.h>
using namespace std;
template <class T>
void show(T a, int n) {
  for (int i = 0; i < n; ++i) cout << a[i] << ' ';
  cout << endl;
}
template <class T>
void show(T a, int r, int l) {
  for (int i = 0; i < r; ++i) show(a[i], l);
  cout << endl;
}
const int N = 1000000;
const int M = 5000;
const int oo = 10000 * 10000 * 10;
int n, m;
struct Ask {
  int tag;
  int p;
  int io;
  int a, b;
} ask[N];
set<int> id_in;
map<int, int> ti;
map<int, int> idtop;
set<int> inp;
struct Node {
  Node(int a, int b, int c) : len(a), x(b), t(c) {}
  int len;
  int x;
  int t;
};
priority_queue<Node> pq;
bool operator<(Node a, Node b) {
  if (a.len != b.len) {
    return a.len < b.len;
  } else
    return a.x < b.x;
}
void com(int dex, int id) {
  int i, j, k;
  if (id_in.find(id) == id_in.end()) {
    id_in.insert(id);
    Node nd(0, 0, 0);
    while (1) {
      assert(pq.size() != 0);
      nd = pq.top();
      pq.pop();
      if (ti[nd.x] != nd.t) continue;
      int p = (nd.x * 2 + 2 + nd.len) / 2;
      ask[dex].p = p;
      ask[dex].io = 1;
      idtop[id] = p;
      inp.insert(p);
      int lf, ri;
      set<int>::iterator iter;
      iter = inp.find(p);
      --iter;
      lf = *iter;
      ++iter, ++iter;
      ri = *iter;
      ++ti[p];
      pq.push(Node(ri - p - 1, p, ti[p]));
      ++ti[lf];
      pq.push(Node(p - lf - 1, lf, ti[lf]));
      break;
    }
  } else {
    id_in.erase(id);
    int p = idtop[id];
    ask[dex].p = p;
    ask[dex].io = -1;
    ti[p]++;
    inp.erase(p);
    set<int>::iterator iter;
    iter = inp.lower_bound(p);
    int ri = *iter;
    int lf = *(--iter);
    ti[lf]++;
    pq.push(Node(ri - lf - 1, lf, ti[lf]));
  }
}
int tn = 0;
int a[N];
int val[N * 4 + 10] = {0};
void add(int i, int lo, int hi, int d, int v) {
  int mid = (lo + hi) / 2;
  if (lo + 1 != hi) {
    if (d < mid)
      add(i * 2, lo, mid, d, v);
    else
      add(i * 2 + 1, mid, hi, d, v);
    val[i] += v;
  } else {
    val[i] += v;
  }
}
int query(int i, int lo, int hi, int lf, int ri) {
  if (ri <= lo || lf >= hi) return 0;
  if (lf <= lo && ri >= hi) return val[i];
  int mid = (lo + hi) / 2;
  int lmin = query(i * 2, lo, mid, lf, ri);
  int rmin = query(i * 2 + 1, mid, hi, lf, ri);
  return lmin + rmin;
}
void solve() {
  int i, j, k;
  for (i = 0; i < m; ++i) {
    if (ask[i].tag == 1) {
      a[tn++] = ask[i].p;
    }
  }
  sort(a, a + tn);
  for (i = 0; i < m; ++i) {
    if (ask[i].tag == 1) ask[i].p = lower_bound(a, a + tn, ask[i].p) - a;
  }
  for (i = 0; i < m; ++i) {
    if (ask[i].tag == 0) {
      int lf = lower_bound(a, a + tn, ask[i].a) - a;
      int ri = upper_bound(a, a + tn, ask[i].b) - a;
      int ans = query(1, 0, tn + 10, lf, ri);
      printf("%d\n", ans);
    } else {
      add(1, 0, tn + 10, ask[i].p, ask[i].io);
    }
  }
}
int main() {
  int i, j, cas = 0;
  scanf("%d %d", &n, &m);
  pq.push(Node(n, 0, 1));
  inp.insert(0);
  inp.insert(n + 1);
  ti[0]++;
  ti[n + 1]++;
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    scanf("%d", &a);
    if (a == 0) {
      scanf("%d %d", &b, &c);
      ask[i].tag = 0;
      ask[i].a = b;
      ask[i].b = c;
    } else {
      ask[i].tag = 1;
      com(i, a);
    }
  }
  solve();
  return 0;
}
