#include <bits/stdc++.h>
using namespace std;
const int maxn = 1200000;
template <typename Type>
class SizeBalanceTree {
 public:
  void clear() {
    sz = 0;
    LC[0] = LC[0] = 0;
    SZ[0] = 0;
    root = 0;
  }
  int Size() { return SZ[root]; }
  bool empty() { return root == 0; }
  void Build(int s, int e) { Build(root, s, e); }
  bool Find(Type key) { return Find(root, key); }
  void Insert(Type key) { Insert(root, key); }
  void Delete(Type key) { Delete(root, key); }
  Type DeleteSelect(int k) { return DeleteSelect(root, k); }
  void DeleteSmall(Type key) { DeleteSmall(root, key); }
  int Rank(Type key) { return Rank(root, key); }
  Type Select(int k) { return Select(root, k); }
  Type pred(Type key) { return pred(root, key); }
  Type succ(Type key) { return succ(root, key); }
  Type getMin() {
    int temp = root;
    while (LC[temp]) temp = LC[temp];
    return K[temp];
  }
  Type getMax() {
    int temp = root;
    while (RC[temp]) temp = RC[temp];
    return K[temp];
  }
  Type DeleteMax() {
    int temp = root;
    if (RC[root] == 0) {
      root = LC[root];
      return K[temp];
    }
    while (RC[RC[temp]]) {
      SZ[temp]--;
      temp = RC[temp];
    }
    SZ[temp]--;
    Type ret = K[RC[temp]];
    RC[temp] = LC[RC[temp]];
    return ret;
  }
  Type DeleteMin() {
    int temp = root;
    if (LC[root] == 0) {
      root = RC[root];
      return K[temp];
    }
    while (LC[LC[temp]]) {
      SZ[temp]--;
      temp = LC[temp];
    }
    SZ[temp]--;
    Type ret = K[LC[temp]];
    LC[temp] = RC[LC[temp]];
    return ret;
  }

 private:
  int SZ[maxn];
  Type K[maxn];
  int LC[maxn];
  int RC[maxn];
  int root, sz;
  void Build(int &root, int s, int e) {
    if (s > e) return;
    int mid = (s + e) / 2;
    root = ++sz;
    K[root] = mid;
    LC[root] = 0;
    RC[root] = 0;
    SZ[root] = e - s + 1;
    if (s == e) return;
    Build(LC[root], s, mid - 1);
    Build(RC[root], mid + 1, e);
  }
  bool Find(int &root, Type key) {
    if (root == 0) {
      return false;
    } else if (key < K[root]) {
      return Find(LC[root], key);
    } else {
      return (K[root] == key || Find(RC[root], key));
    }
  }
  void Insert(int &root, Type key) {
    if (root == 0) {
      root = ++sz;
      LC[root] = RC[root] = 0;
      SZ[root] = 1;
      K[root] = key;
      return;
    }
    SZ[root]++;
    if (key < K[root]) {
      Insert(LC[root], key);
    } else {
      Insert(RC[root], key);
    }
    maintain(root, !(key < K[root]));
  }
  Type Delete(int &root, Type key) {
    SZ[root]--;
    if ((K[root] == key) || (key < K[root] && LC[root] == 0) ||
        (K[root] < key && RC[root] == 0)) {
      Type ret = K[root];
      if (LC[root] == 0 || RC[root] == 0) {
        root = LC[root] + RC[root];
      } else {
        K[root] = Delete(LC[root], K[root] + 1);
      }
      return ret;
    } else {
      if (key < K[root]) {
        return Delete(LC[root], key);
      } else {
        return Delete(RC[root], key);
      }
    }
  }
  void DeleteSmall(int &root, Type key) {
    if (root == 0) return;
    if (K[root] < key) {
      root = RC[root];
      DeleteSmall(root, key);
    } else {
      DeleteSmall(LC[root], key);
      SZ[root] = 1 + SZ[LC[root]] + SZ[RC[root]];
    }
  }
  int Rank(int &root, Type key) {
    if (!root) {
      return 1;
    } else if (key <= K[root]) {
      return Rank(LC[root], key);
    } else {
      return SZ[LC[root]] + 1 + Rank(RC[root], key);
    }
  }
  Type Select(int &root, int k) {
    if (SZ[LC[root]] + 1 == k) {
      return K[root];
    } else if (k > SZ[LC[root]]) {
      return Select(RC[root], k - 1 - SZ[LC[root]]);
    } else {
      return Select(LC[root], k);
    }
  }
  Type DeleteSelect(int &root, int k) {
    SZ[root]--;
    if (SZ[LC[root]] + 1 == k) {
      Type ret = K[root];
      if (LC[root] == 0 || RC[root] == 0) {
        root = LC[root] + RC[root];
      } else {
        K[root] = Delete(LC[root], K[root] + 1);
      }
      return ret;
    } else if (k > SZ[LC[root]]) {
      return DeleteSelect(RC[root], k - 1 - SZ[LC[root]]);
    } else {
      return DeleteSelect(LC[root], k);
    }
  }
  Type pred(int &root, Type key) {
    if (root == 0) {
      return key;
    } else if (key > K[root]) {
      Type ret = pred(RC[root], key);
      if (ret == key) return K[root];
      return ret;
    } else {
      return pred(LC[root], key);
    }
  }
  Type succ(int &root, Type key) {
    if (root == 0) {
      return key;
    } else if (K[root] > key) {
      Type ret = succ(LC[root], key);
      if (ret == key) return K[root];
      return ret;
    } else {
      return succ(RC[root], key);
    }
  }
  void LeftRotate(int &root) {
    int temp = RC[root];
    RC[root] = LC[temp];
    LC[temp] = root;
    SZ[temp] = SZ[root];
    SZ[root] = 1 + SZ[LC[root]] + SZ[RC[root]];
    root = temp;
  }
  void RightRotate(int &root) {
    int temp = LC[root];
    LC[root] = RC[temp];
    RC[temp] = root;
    SZ[temp] = SZ[root];
    SZ[root] = 1 + SZ[LC[root]] + SZ[RC[root]];
    root = temp;
  }
  void maintain(int &root, bool flag) {
    if (root == 0) return;
    if (!flag) {
      if (SZ[LC[LC[root]]] > SZ[RC[root]]) {
        RightRotate(root);
      } else if (SZ[RC[LC[root]]] > SZ[RC[root]]) {
        LeftRotate(LC[root]);
        RightRotate(root);
      } else {
        return;
      }
    } else {
      if (SZ[RC[RC[root]]] > SZ[LC[root]]) {
        LeftRotate(root);
      } else if (SZ[LC[RC[root]]] > SZ[LC[root]]) {
        RightRotate(RC[root]);
        LeftRotate(root);
      } else {
        return;
      }
    }
    maintain(LC[root], false);
    maintain(RC[root], true);
    maintain(root, false);
    maintain(root, true);
  }
};
SizeBalanceTree<int> sbt;
struct node {
  int l, r;
  int len() { return r - l + 1; }
  int mid() {
    int le = l + r;
    return (le + (le % 2)) / 2;
  }
  node() {}
  node(int _l, int _r) {
    l = _l;
    r = _r;
  }
  bool operator<(const node gg) const {
    if (r - l + 1 == gg.r - gg.l + 1) {
      return r > gg.r;
    }
    return r - l + 1 > gg.r - gg.l + 1;
  }
};
set<node> st;
int n, q;
map<int, int> mp;
map<int, int> mm;
int main() {
  int i, j, k;
  scanf("%d%d", &n, &q);
  st.insert(node(1, n));
  while (q--) {
    scanf("%d", &k);
    if (k) {
      if (mp.find(k) == mp.end()) {
        node tmp = *st.begin();
        int p = tmp.mid();
        mp[k] = p;
        st.erase(st.begin());
        if (tmp.l <= p - 1) st.insert(node(tmp.l, p - 1));
        if (p + 1 <= tmp.r) st.insert(node(p + 1, tmp.r));
        sbt.Insert(p);
      } else {
        int p = mp[k];
        mp.erase(k);
        int l = sbt.pred(p);
        if (l == p) l = 0;
        int r = sbt.succ(p);
        if (r == p) r = n + 1;
        sbt.Delete(p);
        if (l + 1 <= p - 1) st.erase(node(l + 1, p - 1));
        if (p + 1 <= r - 1) st.erase(node(p + 1, r - 1));
        st.insert(node(l + 1, r - 1));
      }
    } else {
      int x, y, l, r;
      scanf("%d%d", &x, &y);
      x--;
      if (sbt.Find(y)) {
        r = y;
      } else {
        r = sbt.pred(y);
      }
      if (sbt.Find(x)) {
        l = x;
      } else {
        l = sbt.pred(x);
      }
      if (sbt.Find(r)) {
        r = sbt.Rank(r);
      } else {
        r = 0;
      }
      if (sbt.Find(l)) {
        l = sbt.Rank(l);
      } else {
        l = 0;
      }
      int sum = r - l;
      printf("%d\n", sum);
    }
  }
  return 0;
}
