```#include <cassert>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <sstream>
#include <iterator>
#include <string>
#include <vector>
#include <set>
#include <list>
#include <map>
#include <algorithm>
#include <cstring>
using namespace std;

//#define printf(...);
#define L(qq,m) for (int qq=0;qq<(m);qq++)
#define L0(qq,ss,m) for (int qq=(ss);qq<(m);qq++)

// dp[w][h][s] = best stacking given area (w,h) and set of blocks s
// dp[w][h][{i}] = 1, single box i with orientation (w, h)
// dp[w'][h'][s + {i}] = dp[w][h][s] + 1, given box i and for each orientation
//   and (w', h') <= (w, h)

int n, t = 0;
int dp[21][21][1 << 11] = {};

struct xyz { int x, y, z; };
#define I(i) (1 << (i))
#define WH(x, y, a, b) { \
if ((x) > (y)) { a = (y); b = (x); } else { a = (x); b = (y) ; } }
#define WH_(x, y, a, b) { WH(x, y, a, b); dp[a][b][I(i)] = 1; }

inline int count(int s) {
int c = 0;
L(i, n) { c += (s & I(i)) != 0; }
return c;
}

inline vector<pair<int, int>> whs(xyz& xyz) {
vector<pair<int, int>> vv;
int x = xyz.x, y = xyz.y, z = xyz.z, w, h;
WH(x, y, w, h);
vv.push_back(make_pair(w, h));
WH(x, z, w, h);
vv.push_back(make_pair(w, h));
WH(y, z, w, h);
vv.push_back(make_pair(w, h));
return vv;
}

inline vector<pair<int, int>> prevs(int s) {
vector<pair<int, int>> vv;
L(i, n) { if ((s & I(i)) != 0) vv.push_back(make_pair(i, s & ~I(i))); }
return vv;
}

inline bool fits(int w, int h, int ww, int hh) {
return (w <= ww && h <= hh) || (w <= hh && h <= ww);
}

int main() {
while (cin >> n) {
if (n == 0) break;
// clear dp
memset(dp, -1, sizeof(dp));

vector<xyz> v(n);
L(i, n) {
int x, y, z;
cin >> x >> y >> z;
v[i] = {x, y, z};
}
int b = 1;
// base cases
L(i, n) {
xyz xyz = v[i];
int x = xyz.x, y = xyz.y, z = xyz.z, a, b;
WH_(x, y, a, b);
WH_(x, z, a, b);
WH_(y, z, a, b);
}
// layers
L0(i, 2, n+1) {
L(s, (1 << n) + 1) {
if (count(s) == i) {
for (auto& p : prevs(s)) {
int j = p.first, s1 = p.second;
xyz xyz = v[j];
for (auto& wh : whs(xyz)) {
int ww = wh.first, hh = wh.second, y = -1;
L0(w, 1, ww + 1) {
L0(h, w, hh + 1) {
if (dp[w][h][s1] == -1 || !fits(w, h, ww, hh)) continue;
y = max(y, 1 + dp[w][h][s1]);
}
}
b = max(b, dp[ww][hh][s] = max(dp[ww][hh][s], y));
}
}
}
}
}
cout << "Case " << (++t) << ": " << b << endl;
}
}
```