#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef unsigned int ui32;
const long long INFLL = 1e18;
using namespace std;
const short MAXN = 15000;
class Coord {
public:
vector<short> vec;
Coord(short x) {
vec.push_back(x);
}
Coord(const vector<short> &vec) : vec(move(vec)) {}
bool operator==(const Coord &lhs) {
if (vec.size() != lhs.vec.size()) {
return false;
}
for (int i = 0; i < (int)vec.size(); i++) {
if (vec[i] != lhs.vec[i]) {
return false;
}
}
return true;
}
Coord operator+(const Coord &lhs) {
vector<short> res;
int l = 0, r = 0;
while (l < (int)vec.size() && r < (int)lhs.vec.size()) {
short nw;
if (vec[l] == lhs.vec[r]) {
nw = vec[l] + 1;
++l;
++r;
} else if (vec[l] < lhs.vec[r]) {
nw = vec[l];
++l;
} else {
nw = lhs.vec[r];
++r;
}
if (res.empty() || res.back() != nw) {
res.push_back(nw);
} else {
++res.back();
}
}
while (l < (int)vec.size()) {
short nw = vec[l];
++l;
if (res.empty() || res.back() != nw) {
res.push_back(nw);
} else {
++res.back();
}
}
while (r < (int)lhs.vec.size()) {
short nw = lhs.vec[r];
++r;
if (res.empty() || res.back() != nw) {
res.push_back(nw);
} else {
++res.back();
}
}
return Coord(res);
}
bool operator<=(const Coord &lhs) {
int l = (int)vec.size() - 1;
int r = (int)lhs.vec.size() - 1;
while (l >= 0 && r >= 0) {
if (vec[l] != lhs.vec[r]) {
return vec[l] < lhs.vec[r];
}
--l;
--r;
}
return l == -1;
}
bool operator>=(const Coord &lhs) {
int l = (int)vec.size() - 1;
int r = (int)lhs.vec.size() - 1;
while (l >= 0 && r >= 0) {
if (vec[l] != lhs.vec[r]) {
return vec[l] > lhs.vec[r];
}
--l;
--r;
}
return r == -1;
}
void dec() {
for (auto &x : vec) {
if (x > 0) {
--x;
}
}
}
};
struct rect {
Coord x1, y1, x2, y2;
};
vector<rect> vec;
int cnt;
void go(Coord &x1, Coord &y1, Coord &x2, Coord &y2) {
int x;
cin >> x;
if (x == 0) {
return;
} else if (x == 1) {
++cnt;
vec.push_back({ x1, y1, x2, y2 });
} else {
Coord midX = (x1 + x2);
midX.dec();
Coord midY = (y1 + y2);
midY.dec();
go(x1, midY, midX, y2);
go(midX, midY, x2, y2);
go(midX, y1, x2, midY);
go(x1, y1, midX, midY);
}
}
int col[MAXN + 1], p[MAXN + 1], sz[MAXN + 1];
int get(int x) {
return x == p[x] ? x : p[x] = get(p[x]);
}
void Union(int a, int b) {
a = get(a);
b = get(b);
if (a != b) {
if (sz[a] < sz[b]) {
swap(a, b);
}
sz[a] += sz[b];
p[b] = a;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("input.txt", "w", stdout);
Coord x1(0), y1(0), x2(MAXN), y2(MAXN);
go(x1, y1, x2, y2);
int ans = (int)vec.size();
for (int i = 0; i < ans; i++) {
p[i] = i;
sz[i] = 1;
}
for (int i = 0; i < (int)vec.size(); i++) {
/*for (auto x : vec[i].x1.vec) {
cout << x << " ";
}
cout << "\n";
for (auto x : vec[i].y1.vec) {
cout << x << " ";
}
cout << "\n";
for (auto x : vec[i].x2.vec) {
cout << x << " ";
}
cout << "\n";
for (auto x : vec[i].y2.vec) {
cout << x << " ";
}
cout << "\n\n\n";*/
for (int j = i - 1; j >= 0; j--) {
if (vec[j].y1 == vec[i].y2) {
if (!(vec[j].x2 <= vec[i].x1 || vec[j].x1 >= vec[i].x2)) {
if (get(i) != get(j)) {
--ans;
Union(i, j);
continue;
}
}
}
if (vec[j].y2 == vec[i].y1) {
if (!(vec[j].x2 <= vec[i].x1 || vec[j].x1 >= vec[i].x2)) {
if (get(i) != get(j)) {
--ans;
Union(i, j);
continue;
}
}
}
if (vec[j].x2 == vec[i].x1) {
if (!(vec[j].y2 <= vec[i].y1 || vec[j].y1 >= vec[i].y2)) {
if (get(i) != get(j)) {
--ans;
Union(i, j);
continue;
}
}
}
if (vec[j].x1 == vec[i].x2) {
if (!(vec[j].y2 <= vec[i].y1 || vec[j].y1 >= vec[i].y2)) {
if (get(i) != get(j)) {
--ans;
Union(i, j);
continue;
}
}
}
}
}
cout << ans << "\n";
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiB1bnNpZ25lZCBsb25nIGxvbmcgdWxsOwp0eXBlZGVmIGxvbmcgZG91YmxlIGxkOwp0eXBlZGVmIHVuc2lnbmVkIGludCB1aTMyOwpjb25zdCBsb25nIGxvbmcgSU5GTEwgPSAxZTE4OwoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNvbnN0IHNob3J0IE1BWE4gPSAxNTAwMDsKCmNsYXNzIENvb3JkIHsKcHVibGljOgogICAgdmVjdG9yPHNob3J0PiB2ZWM7CgogICAgQ29vcmQoc2hvcnQgeCkgewogICAgICAgIHZlYy5wdXNoX2JhY2soeCk7CiAgICB9CgogICAgQ29vcmQoY29uc3QgdmVjdG9yPHNob3J0PiAmdmVjKSA6IHZlYyhtb3ZlKHZlYykpIHt9CgogICAgYm9vbCBvcGVyYXRvcj09KGNvbnN0IENvb3JkICZsaHMpIHsKICAgICAgICBpZiAodmVjLnNpemUoKSAhPSBsaHMudmVjLnNpemUoKSkgewogICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgfQogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgKGludCl2ZWMuc2l6ZSgpOyBpKyspIHsKICAgICAgICAgICAgaWYgKHZlY1tpXSAhPSBsaHMudmVjW2ldKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIHRydWU7CiAgICB9CgogICAgQ29vcmQgb3BlcmF0b3IrKGNvbnN0IENvb3JkICZsaHMpIHsKICAgICAgICB2ZWN0b3I8c2hvcnQ+IHJlczsKICAgICAgICBpbnQgbCA9IDAsIHIgPSAwOwoKICAgICAgICB3aGlsZSAobCA8IChpbnQpdmVjLnNpemUoKSAmJiByIDwgKGludClsaHMudmVjLnNpemUoKSkgewogICAgICAgICAgICBzaG9ydCBudzsKICAgICAgICAgICAgaWYgKHZlY1tsXSA9PSBsaHMudmVjW3JdKSB7CiAgICAgICAgICAgICAgICBudyA9IHZlY1tsXSArIDE7CiAgICAgICAgICAgICAgICArK2w7CiAgICAgICAgICAgICAgICArK3I7CiAgICAgICAgICAgIH0gZWxzZSBpZiAodmVjW2xdIDwgbGhzLnZlY1tyXSkgewogICAgICAgICAgICAgICAgbncgPSB2ZWNbbF07CiAgICAgICAgICAgICAgICArK2w7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBudyA9IGxocy52ZWNbcl07CiAgICAgICAgICAgICAgICArK3I7CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIGlmIChyZXMuZW1wdHkoKSB8fCByZXMuYmFjaygpICE9IG53KSB7CiAgICAgICAgICAgICAgICByZXMucHVzaF9iYWNrKG53KTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICsrcmVzLmJhY2soKTsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgd2hpbGUgKGwgPCAoaW50KXZlYy5zaXplKCkpIHsKICAgICAgICAgICAgc2hvcnQgbncgPSB2ZWNbbF07CiAgICAgICAgICAgICsrbDsKCiAgICAgICAgICAgIGlmIChyZXMuZW1wdHkoKSB8fCByZXMuYmFjaygpICE9IG53KSB7CiAgICAgICAgICAgICAgICByZXMucHVzaF9iYWNrKG53KTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICsrcmVzLmJhY2soKTsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgd2hpbGUgKHIgPCAoaW50KWxocy52ZWMuc2l6ZSgpKSB7CiAgICAgICAgICAgIHNob3J0IG53ID0gbGhzLnZlY1tyXTsKICAgICAgICAgICAgKytyOwoKICAgICAgICAgICAgaWYgKHJlcy5lbXB0eSgpIHx8IHJlcy5iYWNrKCkgIT0gbncpIHsKICAgICAgICAgICAgICAgIHJlcy5wdXNoX2JhY2sobncpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgKytyZXMuYmFjaygpOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICByZXR1cm4gQ29vcmQocmVzKTsKICAgIH0KCiAgICBib29sIG9wZXJhdG9yPD0oY29uc3QgQ29vcmQgJmxocykgewogICAgICAgIGludCBsID0gKGludCl2ZWMuc2l6ZSgpIC0gMTsKICAgICAgICBpbnQgciA9IChpbnQpbGhzLnZlYy5zaXplKCkgLSAxOwoKICAgICAgICB3aGlsZSAobCA+PSAwICYmIHIgPj0gMCkgewogICAgICAgICAgICBpZiAodmVjW2xdICE9IGxocy52ZWNbcl0pIHsKICAgICAgICAgICAgICAgIHJldHVybiB2ZWNbbF0gPCBsaHMudmVjW3JdOwogICAgICAgICAgICB9CiAgICAgICAgICAgIC0tbDsKICAgICAgICAgICAgLS1yOwogICAgICAgIH0KCiAgICAgICAgcmV0dXJuIGwgPT0gLTE7CiAgICB9CgogICAgYm9vbCBvcGVyYXRvcj49KGNvbnN0IENvb3JkICZsaHMpIHsKICAgICAgICBpbnQgbCA9IChpbnQpdmVjLnNpemUoKSAtIDE7CiAgICAgICAgaW50IHIgPSAoaW50KWxocy52ZWMuc2l6ZSgpIC0gMTsKCiAgICAgICAgd2hpbGUgKGwgPj0gMCAmJiByID49IDApIHsKICAgICAgICAgICAgaWYgKHZlY1tsXSAhPSBsaHMudmVjW3JdKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gdmVjW2xdID4gbGhzLnZlY1tyXTsKICAgICAgICAgICAgfQogICAgICAgICAgICAtLWw7CiAgICAgICAgICAgIC0tcjsKICAgICAgICB9CgogICAgICAgIHJldHVybiByID09IC0xOwogICAgfQoKICAgIHZvaWQgZGVjKCkgewogICAgICAgIGZvciAoYXV0byAmeCA6IHZlYykgewogICAgICAgICAgICBpZiAoeCA+IDApIHsKICAgICAgICAgICAgICAgIC0teDsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfTsKCnN0cnVjdCByZWN0IHsKICAgIENvb3JkIHgxLCB5MSwgeDIsIHkyOwp9OwoKdmVjdG9yPHJlY3Q+IHZlYzsKaW50IGNudDsKCnZvaWQgZ28oQ29vcmQgJngxLCBDb29yZCAmeTEsIENvb3JkICZ4MiwgQ29vcmQgJnkyKSB7CiAgICBpbnQgeDsKICAgIGNpbiA+PiB4OwogICAgaWYgKHggPT0gMCkgewogICAgICAgIHJldHVybjsKICAgIH0gZWxzZSBpZiAoeCA9PSAxKSB7CiAgICAgICAgKytjbnQ7CiAgICAgICAgdmVjLnB1c2hfYmFjayh7IHgxLCB5MSwgeDIsIHkyIH0pOwogICAgfSBlbHNlIHsKICAgICAgICBDb29yZCBtaWRYID0gKHgxICsgeDIpOwogICAgICAgIG1pZFguZGVjKCk7CiAgICAgICAgQ29vcmQgbWlkWSA9ICh5MSArIHkyKTsKICAgICAgICBtaWRZLmRlYygpOwoKICAgICAgICBnbyh4MSwgbWlkWSwgbWlkWCwgeTIpOwogICAgICAgIGdvKG1pZFgsIG1pZFksIHgyLCB5Mik7CiAgICAgICAgZ28obWlkWCwgeTEsIHgyLCBtaWRZKTsKICAgICAgICBnbyh4MSwgeTEsIG1pZFgsIG1pZFkpOwogICAgfQp9CgppbnQgY29sW01BWE4gKyAxXSwgcFtNQVhOICsgMV0sIHN6W01BWE4gKyAxXTsKCmludCBnZXQoaW50IHgpIHsKICAgIHJldHVybiB4ID09IHBbeF0gPyB4IDogcFt4XSA9IGdldChwW3hdKTsKfQoKdm9pZCBVbmlvbihpbnQgYSwgaW50IGIpIHsKICAgIGEgPSBnZXQoYSk7CiAgICBiID0gZ2V0KGIpOwoKICAgIGlmIChhICE9IGIpIHsKICAgICAgICBpZiAoc3pbYV0gPCBzeltiXSkgewogICAgICAgICAgICBzd2FwKGEsIGIpOwogICAgICAgIH0KICAgICAgICBzelthXSArPSBzeltiXTsKICAgICAgICBwW2JdID0gYTsKICAgIH0KfQoKCmludCBtYWluKCkgewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKDApOwogICAgLy9mcmVvcGVuKCJpbnB1dC50eHQiLCAiciIsIHN0ZGluKTsKICAgIC8vZnJlb3BlbigiaW5wdXQudHh0IiwgInciLCBzdGRvdXQpOwoKICAgIENvb3JkIHgxKDApLCB5MSgwKSwgeDIoTUFYTiksIHkyKE1BWE4pOwogICAgZ28oeDEsIHkxLCB4MiwgeTIpOwoKICAgIGludCBhbnMgPSAoaW50KXZlYy5zaXplKCk7CgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBhbnM7IGkrKykgewogICAgICAgIHBbaV0gPSBpOwogICAgICAgIHN6W2ldID0gMTsKICAgIH0KCiAgICBmb3IgKGludCBpID0gMDsgaSA8IChpbnQpdmVjLnNpemUoKTsgaSsrKSB7CiAgICAgICAgLypmb3IgKGF1dG8geCA6IHZlY1tpXS54MS52ZWMpIHsKICAgICAgICAgICAgY291dCA8PCB4IDw8ICIgIjsKICAgICAgICB9CiAgICAgICAgY291dCA8PCAiXG4iOwogICAgICAgIGZvciAoYXV0byB4IDogdmVjW2ldLnkxLnZlYykgewogICAgICAgICAgICBjb3V0IDw8IHggPDwgIiAiOwogICAgICAgIH0KICAgICAgICBjb3V0IDw8ICJcbiI7CiAgICAgICAgZm9yIChhdXRvIHggOiB2ZWNbaV0ueDIudmVjKSB7CiAgICAgICAgICAgIGNvdXQgPDwgeCA8PCAiICI7CiAgICAgICAgfQogICAgICAgIGNvdXQgPDwgIlxuIjsKICAgICAgICBmb3IgKGF1dG8geCA6IHZlY1tpXS55Mi52ZWMpIHsKICAgICAgICAgICAgY291dCA8PCB4IDw8ICIgIjsKICAgICAgICB9CiAgICAgICAgY291dCA8PCAiXG5cblxuIjsqLwoKICAgICAgICBmb3IgKGludCBqID0gaSAtIDE7IGogPj0gMDsgai0tKSB7CiAgICAgICAgICAgIGlmICh2ZWNbal0ueTEgPT0gdmVjW2ldLnkyKSB7CiAgICAgICAgICAgICAgICBpZiAoISh2ZWNbal0ueDIgPD0gdmVjW2ldLngxIHx8IHZlY1tqXS54MSA+PSB2ZWNbaV0ueDIpKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGdldChpKSAhPSBnZXQoaikpIHsKICAgICAgICAgICAgICAgICAgICAgICAgLS1hbnM7CiAgICAgICAgICAgICAgICAgICAgICAgIFVuaW9uKGksIGopOwogICAgICAgICAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHZlY1tqXS55MiA9PSB2ZWNbaV0ueTEpIHsKICAgICAgICAgICAgICAgIGlmICghKHZlY1tqXS54MiA8PSB2ZWNbaV0ueDEgfHwgdmVjW2pdLngxID49IHZlY1tpXS54MikpIHsKICAgICAgICAgICAgICAgICAgICBpZiAoZ2V0KGkpICE9IGdldChqKSkgewogICAgICAgICAgICAgICAgICAgICAgICAtLWFuczsKICAgICAgICAgICAgICAgICAgICAgICAgVW5pb24oaSwgaik7CiAgICAgICAgICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAodmVjW2pdLngyID09IHZlY1tpXS54MSkgewogICAgICAgICAgICAgICAgaWYgKCEodmVjW2pdLnkyIDw9IHZlY1tpXS55MSB8fCB2ZWNbal0ueTEgPj0gdmVjW2ldLnkyKSkgewogICAgICAgICAgICAgICAgICAgIGlmIChnZXQoaSkgIT0gZ2V0KGopKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIC0tYW5zOwogICAgICAgICAgICAgICAgICAgICAgICBVbmlvbihpLCBqKTsKICAgICAgICAgICAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmICh2ZWNbal0ueDEgPT0gdmVjW2ldLngyKSB7CiAgICAgICAgICAgICAgICBpZiAoISh2ZWNbal0ueTIgPD0gdmVjW2ldLnkxIHx8IHZlY1tqXS55MSA+PSB2ZWNbaV0ueTIpKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGdldChpKSAhPSBnZXQoaikpIHsKICAgICAgICAgICAgICAgICAgICAgICAgLS1hbnM7CiAgICAgICAgICAgICAgICAgICAgICAgIFVuaW9uKGksIGopOwogICAgICAgICAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgogICAgY291dCA8PCBhbnMgPDwgIlxuIjsKICAgIHJldHVybiAwOwp9