#include <bits/stdc++.h>
#define IOS std::ios::sync_with_stdio(0)
#define C std::cin.tie(0)
#define Co std::cout.tie(0)
typedef long long LL;
LL c, Q;
std::vector <LL> q;
int main() {
IOS; C; Co;
std::cin >> c >> Q;
while (Q--) {
LL op;
std::cin >> op;
if (op != 4) {
if (op == 1) {
LL x;
std::cin >> x;
for (LL i = 1; i <= x; ++i) q.push_back(i);
}
if (op == 2) {
LL y;
std::cin >> y;
if (q.size() >= y) q.erase(q.begin(), q.begin() + y);
else q.clear();
}
if (op == 3){
LL z;
std::cin >> z;
if (z - 1 < q.size()) std::cout << q[z - 1] << '\n';
}
}
else {
auto maxn = std::max_element(q.begin(), q.end());
std::cout << *maxn << '\n';
}
}
return 0;
}