// Source code from laekov at noip 2015 day1 #define PRID “landlords” #include #include #include

using namespace std;

const int maxn = 17;

int t, n, a[maxn], ans, c;

int readCard() { int x, y; scanf("%d%d", &x, &y); if (x == 0) return 12 + y; else if (x == 1) return 11; else if (x == 2) return 12; else return x - 3; }

void checkCtn(int, int, int, int);

void DFS(int cu, int lm) { if (lm == 13) { if (a[13] || a[14]) ans = min(ans, cu + 1); else ans = min(ans, cu); return; } if (cu + c < ans) ans = cu + c; if (cu >= ans) return; if (!a[lm]) { DFS(cu, lm + 1); return; } if (a[lm] >= 1) checkCtn(lm, 1, 5, cu); if (a[lm] >= 2) checkCtn(lm, 2, 3, cu); if (a[lm] >= 3) checkCtn(lm, 3, 2, cu); if (a[lm] >= 4) { c -= 4; a[lm] -= 4; for (int i = lm; i < 15; ++ i) { if (a[i] >= 2) { a[i] -= 2, c -= 2; DFS(cu + 1, lm); a[i] += 2, c += 2; for (int j = i + 1; j < 15; ++ j) if (a[j] >= 2) { a[i] -= 2, a[j] -= 2, c -= 4; DFS(cu + 1, lm); a[i] += 2, a[j] += 2, c += 4; } } if (a[i] >= 1) { for (int j = i + 1; j < 15; ++ j) if (a[j] >= 1) { a[i] -= 1, a[j] -= 1, c -= 2; DFS(cu + 1, lm); a[i] += 1, a[j] += 1, c += 2; } } } DFS(cu + 1, lm); c += 4; a[lm] += 4; } if (a[lm] >= 3) { c -= 3; a[lm] -= 3; for (int i = lm + 1; i < 15; ++ i) { if (a[i] >= 2) { a[i] -= 2, c -= 2; DFS(cu + 1, lm); a[i] += 2, c += 2; } if (a[i] >= 1) { a[i] -= 1, c -= 1; DFS(cu + 1, lm); a[i] += 1, c += 1; } } DFS(cu + 1, lm); c += 3; a[lm] += 3; } if (a[lm] >= 2) { c -= 2; a[lm] -= 2; for (int i = lm + 1; i < 13; ++ i) { if (a[i] >= 3) { a[i] -= 3, c -= 3; DFS(cu + 1, lm); a[i] += 3, c += 3; } if (a[i] >= 4) { a[i] -= 4, c -= 4; for (int j = lm + 1; j < 13; ++ j) if (a[j] >= 2) { a[j] -= 2, c -= 2; DFS(cu + 1, lm); a[j] += 2, c += 2; } DFS(cu + 1, lm); a[i] += 4, c += 4; } } DFS(cu + 1, lm); c += 2; a[lm] += 2; } if (a[lm] >= 1) { c -= 1; a[lm] -= 1; for (int i = lm + 1; i < 13; ++ i) { if (a[i] >= 3) { a[i] -= 3, c -= 3; DFS(cu + 1, lm); a[i] += 3, c += 3; } if (a[i] >= 4) { a[i] -= 4, c -= 4; for (int j = lm + 1; j < 13; ++ j) if (a[j] >= 1) { a[j] -= 1, c -= 1; DFS(cu + 1, lm); a[j] += 1, c += 1; } a[i] += 4, c += 4; } } DFS(cu + 1, lm); c += 1; a[lm] += 1; } }

void checkCtn(int lm, int cnt, int lt, int cu) { if (lm + lt - 1 >= 12) return; int ep(lm); for (ep = lm; ep < 12; ++ ep) { if (a[ep] < cnt) break; c -= cnt; a[ep] -= cnt; if (ep - lm + 1 >= lt) DFS(cu + 1, lm); } for (int i = lm; i < ep; ++ i) a[i] += cnt; c += (ep - lm) * cnt; }

int main() { freopen(PRID “.in”, “r”, stdin); freopen(PRID “.out”, “w”, stdout);

scanf("%d%d", &t, &n);
while (t --) {
	memset(a, 0, sizeof(a));
	c = n;
	for (int i = 0; i < n; ++ i)
		++ a[readCard()];
	ans = n;
	DFS(0, 0);
	printf("%d\n", ans);
}

}