#include <bits/stdc++.h>
using namespace std;
using namespace std;
template <class DATA, class QUERY>
struct sequence_tree {
 public:
  struct t_node {
    bool leaf;
    DATA value;
    QUERY query;
    int left_child, right_child;
    t_node(DATA value, QUERY query) {
      left_child = right_child = -1;
      leaf = true;
      this->value = value;
      this->query = query;
    }
  };
  vector<t_node> node;
  QUERY (*f_recursive)(QUERY, QUERY);
  QUERY (*f_leaf)(DATA, int, int);
  DATA default_value;
  void add_node(int l, int r) {
    t_node new_node(default_value, f_leaf(default_value, l, r));
    node.push_back(new_node);
  }
  void do_update(int p, int l, int r, int i, int j, DATA &v) {
    if (i <= l && j >= r) {
      node[p] = t_node(v, f_leaf(v, l, r));
    } else if (i <= r && j >= l) {
      int m = (l + r) / 2;
      if (node[p].leaf == true) {
        node[p].leaf = false;
        if (node[p].left_child == -1) {
          add_node(l, m);
          node[p].left_child = node.size() - 1;
        }
        if (node[p].right_child == -1) {
          add_node(m + 1, r);
          node[p].right_child = node.size() - 1;
        }
        node[node[p].left_child] =
            t_node(node[p].value, f_leaf(node[p].value, l, m));
        node[node[p].right_child] =
            t_node(node[p].value, f_leaf(node[p].value, m + 1, r));
      }
      do_update(node[p].left_child, l, m, i, j, v);
      do_update(node[p].right_child, m + 1, r, i, j, v);
      node[p].query = f_recursive(node[node[p].left_child].query,
                                  node[node[p].right_child].query);
    }
  }
  QUERY do_query(int p, int l, int r, int i, int j) {
    if (j < l || i > r) {
      return f_leaf(default_value, l, r);
    }
    if (i <= l && j >= r) {
      return node[p].query;
    }
    if (node[p].leaf) {
      return f_leaf(node[p].value, max(i, l), min(j, r));
    }
    int m = (l + r) / 2;
    if (j <= m) {
      return do_query(node[p].left_child, l, m, i, j);
    }
    if (i >= m + 1) {
      return do_query(node[p].right_child, m + 1, r, i, j);
    }
    return f_recursive(do_query(node[p].left_child, l, m, i, j),
                       do_query(node[p].right_child, m + 1, r, i, j));
  }
  int min_i, max_i;
  void reserve_number_of_node(int n) { node.reserve(n); }
  sequence_tree(int min_i, int max_i, DATA default_value,
                QUERY (*f_recursive)(QUERY, QUERY),
                QUERY (*f_leaf)(DATA, int, int)) {
    this->min_i = min_i;
    this->max_i = max_i;
    this->f_recursive = f_recursive;
    this->f_leaf = f_leaf;
    this->default_value = default_value;
    add_node(min_i, max_i);
  }
  void update(int i, int j, DATA v) { do_update(0, min_i, max_i, i, j, v); }
  QUERY query(int i, int j) { return do_query(0, min_i, max_i, i, j); }
  QUERY query_all() { return do_query(0, min_i, max_i, min_i, max_i); }
};
struct query {
  int p_left, p_right, left_zero, right_zero, left_best, right_best, count;
  query() {}
  query(int p_left, int p_right, int left_zero, int right_zero, int left_best,
        int right_best, int count) {
    this->p_left = p_left;
    this->p_right = p_right;
    this->left_zero = left_zero;
    this->right_zero = right_zero;
    this->left_best = left_best;
    this->right_best = right_best;
    this->count = count;
  }
};
bool better(int l1, int r1, int l2, int r2) {
  int len1 = r1 - l1 + 1;
  int len2 = r2 - l2 + 1;
  if (len1 > len2) return true;
  if (len1 < len2) return false;
  return (r1 > r2);
}
query f_recursive(query q_left, query q_right) {
  int l1 = q_left.left_best, r1 = q_left.right_best;
  int l2 = q_right.left_best, r2 = q_right.right_best;
  int l3 = q_left.p_right - q_left.right_zero + 1,
      r3 = q_right.p_left + q_right.left_zero - 1;
  int l = l1, r = r1;
  if (better(l2, r2, l, r)) {
    l = l2;
    r = r2;
  }
  if (better(l3, r3, l, r)) {
    l = l3;
    r = r3;
  }
  return query(q_left.p_left, q_right.p_right,
               q_left.left_zero == q_left.p_right - q_left.p_left + 1
                   ? (q_left.left_zero + q_right.left_zero)
                   : q_left.left_zero,
               q_right.right_zero == q_right.p_right - q_right.p_left + 1
                   ? (q_right.right_zero + q_left.right_zero)
                   : q_right.right_zero,
               l, r, q_left.count + q_right.count);
}
query f_leaf(int v, int l, int r) {
  return query(l, r, v == 0 ? r - l + 1 : 0, v == 0 ? r - l + 1 : 0,
               v == 0 ? l : 0, v == 0 ? r : -1, v == 1 ? (r - l + 1) : 0);
}
map<int, int> pos;
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  sequence_tree<int, query> tree(1, n, 0, f_recursive, f_leaf);
  tree.reserve_number_of_node(5500000);
  while (q--) {
    int t;
    scanf("%d", &t);
    if (t == 0) {
      int x, y;
      scanf("%d%d", &x, &y);
      printf("%d\n", tree.query(x, y).count);
    } else {
      if (pos.count(t)) {
        int p = pos[t];
        tree.update(p, p, 0);
        pos.erase(t);
      } else {
        query q = tree.query_all();
        int l = q.left_best;
        int r = q.right_best;
        int m = (l + r) / 2;
        if ((r - l + 1) % 2 == 0) {
          ++m;
        }
        tree.update(m, m, 1);
        pos[t] = m;
      }
    }
  }
  return 0;
}
