#include "insects.h"
#include <bits/stdc++.h>
using namespace std;
int n;
vector<bool> ins;
void rem(int i) {
if (ins[i]) move_outside(i);
ins[i] = false;
}
void add(int i) {
if (!ins[i]) move_inside(i);
ins[i] = true;
}
void dbg(vector<int> a) {
cout << "DBG: ";
for (auto x: a) cout << x << " ";
cout << "\n";
}
int min_cardinality(int N) {
n = N;
ins.assign(n, false);
vector<bool> ok(n, false);
int types = 0;
vector<int> cands;
for (int i = 0; i < n; i++) {
add(i);
int x = press_button();
if (x > 1) {
rem(i);
}
else {
types++;
cands.push_back(i);
ok[i] = true;
}
}
if (types == 1) {
return n;
}
//dbg(cands);
vector<int> cnt(types, 1);
if (types * types <= n) {
for (int i = 0; i < n; i++) {
if (ok[i]) continue;
int lo = 0, hi = types-1;
int finIdx = -1;
add(i);
while (lo <= hi) {
int mid = (lo + hi) / 2;
for (int j = 0; j < n; j++) {
if (j != i) rem(j);
}
for (int j = 0; j <= mid; j++) {
add(cands[j]);
}
int tot = press_button();
//dbg({i, tot});
if (tot == 2) {
hi = mid-1;
finIdx = mid;
}
else {
lo = mid+1;
assert(tot == 1);
}
}
assert(finIdx != -1);
cnt[finIdx]++;
rem(i);
}
int ans = cnt[0];
for (int i = 1; i < types; i++) ans = min(ans, cnt[i]);
return ans;
}
int lo = 1, hi = n / types;
int fin = n / types + 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
for (int i = 0; i < n; i++) {
rem(i);
}
int haveIn = 0;
for (int i = 0; i < n; i++) {
add(i);
haveIn++;
if (press_button() > mid) {
rem(i);
haveIn--;
}
}
int should = types * mid;
//dbg({mid, should});
if (haveIn < should) {
hi = mid-1;
}
else {
fin = mid;
lo = mid+1;
}
}
return fin;
}