#include <bits/stdc++.h>
using namespace std;
int better(pair<int, int> pa, pair<int, int> pb) {
  int sz1 = pa.second - pa.first + 1;
  int sz2 = pb.second - pb.first + 1;
  if (sz1 != sz2) return sz1 > sz2;
  return pa.first > pb.first;
}
template <class CLowHigh, class CData, class CReturn = CData>
struct SegmentTree {
  CLowHigh low_, high_;
  bool isleaf_;
  int lv_;
  int rv_;
  int mv_;
  pair<int, int> mvseg_;
  int count_;
  int contains_;
  SegmentTree<CLowHigh, CData, CReturn>* left_;
  SegmentTree<CLowHigh, CData, CReturn>* right_;
  SegmentTree(CLowHigh low, CLowHigh high, int contains) {
    low_ = low;
    high_ = high;
    contains_ = contains;
    rv_ = lv_ = mv_ = high_ - low_ + 1;
    mvseg_ = make_pair(low_, high_);
    count_ = 0;
    left_ = right_ = 0;
    isleaf_ = 1;
    CalculateReturnValue();
  }
  ~SegmentTree() {
    if (left_ != 0) delete left_;
    if (right_ != 0) delete right_;
  }
  void CalculateReturnValue();
  void Insert(CLowHigh low, CLowHigh high, CData value) {
    if (low_ >= low && high_ <= high) {
      if (left_) delete left_;
      if (right_) delete right_;
      left_ = right_ = NULL;
      isleaf_ = 1;
      contains_ = value;
      CalculateReturnValue();
      return;
    }
    CLowHigh nleft = (high_ - low_) / 2;
    if (isleaf_) {
      left_ = new SegmentTree(low_, low_ + nleft, contains_);
      right_ = new SegmentTree(low_ + nleft + 1LL, high_, contains_);
      isleaf_ = 0;
    }
    if (low <= low_ + nleft) (*left_).Insert(low, high, value);
    if (high >= low_ + nleft + 1LL) (*right_).Insert(low, high, value);
    CalculateReturnValue();
  }
  CReturn GetCount(CLowHigh low, CLowHigh high) {
    if (low_ >= low && high_ <= high) return count_;
    if (low >= (*right_).low_) return (*right_).GetCount(low, high);
    if (high <= (*left_).high_) return (*left_).GetCount(low, high);
    return (*left_).GetCount(low, high) + (*right_).GetCount(low, high);
  }
  int size() { return high_ - low_ + 1; }
};
template <class CLowHigh, class CData, class CReturn>
void SegmentTree<CLowHigh, CData, CReturn>::CalculateReturnValue() {
  if (isleaf_) {
    if (contains_) {
      count_ = high_ - low_ + 1;
      lv_ = mv_ = rv_ = 0;
      mvseg_ = make_pair(high_ + 1, high_);
    } else {
      count_ = 0;
      lv_ = mv_ = rv_ = high_ - low_ + 1;
      mvseg_ = make_pair(low_, high_);
    }
  } else {
    count_ = left_->count_ + right_->count_;
    if (left_->mv_ == left_->size()) {
      lv_ = left_->size() + right_->lv_;
    } else {
      lv_ = left_->lv_;
    }
    if (right_->mv_ == right_->size()) {
      rv_ = right_->size() + left_->rv_;
    } else {
      rv_ = right_->rv_;
    }
    mvseg_ = left_->mvseg_;
    if (better(right_->mvseg_, mvseg_)) mvseg_ = right_->mvseg_;
    pair<int, int> midseg = make_pair(left_->high_ + 1 - left_->rv_,
                                      right_->low_ - 1 + right_->lv_);
    if (better(midseg, mvseg_)) mvseg_ = midseg;
    mv_ = mvseg_.second - mvseg_.first + 1;
  }
}
int main() {
  int n, q;
  cin >> n >> q;
  SegmentTree<int, int, int> st(0, n + 1, 0);
  st.Insert(0, 0, 1);
  st.Insert(n + 1, n + 1, 1);
  vector<pair<int, pair<int, int> > > reqs;
  for (int(i) = 0; (i) < (q); ++(i)) {
    int x;
    scanf("%d", &x);
    if (x == 0) {
      int a, b;
      scanf("%d%d", &a, &b);
      reqs.push_back(make_pair(x, make_pair(a, b)));
      st.Insert(a, a, 0);
      st.Insert(b, b, 0);
    } else {
      reqs.push_back(make_pair(x, make_pair(0, 0)));
    }
  }
  map<int, int> cnt;
  map<int, int> pos;
  for (int(i) = 0; (i) < (q); ++(i)) {
    if (reqs[i].first == 0) {
      cout << st.GetCount(reqs[i].second.first, reqs[i].second.second) << endl;
    } else {
      int jml = cnt[reqs[i].first];
      cnt[reqs[i].first] ^= 1;
      int p = pos[reqs[i].first];
      if (jml == 1) {
        st.Insert(p, p, 0);
      } else {
        pair<int, int> pii = st.mvseg_;
        int len = pii.second - pii.first + 1;
        int pin = pii.first + (len / 2);
        pos[reqs[i].first] = pin;
        st.Insert(pin, pin, jml ^ 1);
      }
    }
  }
  return 0;
}
