The maximal number is 6n(n+1)(2n+1).
Call a n×n×1 box an x-box, an y-box, or a z-box according to its orientation.
Let N be the number of colors in a valid configuration. We start with the upper bound for N.
Let D1,D2 and D3 be the sets of colors which appear in the big cube exactly once, exactly twice, and at least thrice, respectively. Let Mi be the set of unit cubes whose colors are in Di, and denote ni=∣Mi∣.
Consider any x-box X, and let Y and Z be a y- and a z-box containing the same set of colors as X does. Let us first prove that
Claim: 4∣X∩M1∣+∣X∩M2∣≤3n+1.
Proof: We distinguish two cases.
Case 1: X∩M1=∅
A cube from X∩M1 should appear in all three boxes X, Y and Z (since that color only appears once), so it should lie in X∩Y∩Z. Thus, X∩M1=X∩Y∩Z, and thus ∣X∩M1∣=1.
Now consider the cubes in X∩M2. There are at most 2(n−1) of them lying in X∩Y or X∩Z (since the cube from X∩Y∩Z is in M1). Let a be a cube from X∩M2 that is NOT in Y or Z. Since a∈M2, there is exactly one other cube a′ sharing the color with a. But since a is in X but not in Y or Z, Y and Z must both contain a′, that is a′∈(Y∩Z)−X. In addition, since a→a′ is clearly injective, the number of cubes a above would not exceed ∣(Y∩Z)−X∣=n−1. Combining all of the above, we have
∣X∩M2∣≤∣X∩M2∩(Y∪Z)∣+∣X∩M2−(Y∪Z)∣≤2(n−1)+(n−1)=3(n−1).
Easy check shows that Claim holds.
Case 2: X∩M1=∅
In this case, the same argument applies with several changes. Indeed, X∩M2 now contains at most 2n−1 cubes from X∩Y or X∩Z. Any other cube a in X∩M2 corresponds to some a′∈Y∩Z, possibly with a′∈X, so there are at most ∣Y∩Z∣=n of them. All this results in ∣X∩M2∣≤(2n−1)+n=3n−1. Easy check shows that Claim holds.
Summing up Claim over all x-boxes X, we get
4n1+n2≤n(3n+1).
Obviously, we also have n1+n2+n3=n3.
Now we are prepared to estimate N. Due to the definition of Mi, we have ni≥i∣Di∣, so
N≤n1+2n2+3n3=3n1+n2+n3+64n1+n2≤3n3+63n2+n=6n(n+1)(2n+1).
It remains to present an example that reaches this upper bound. Color each set with a different color to provide the desired example.
(i) n sets of the form {(i,i,i)} (1≤i≤n);
(ii) C2n sets of the form {(i,j,j),(j,i,i)}, C2n sets of the form {(j,i,j),(i,j,i)}, and C2n sets of the form {(j,j,i),(i,i,j)} (1≤i<j≤n);
(iii) C3n sets of the form {(i,j,k),(j,k,i),(k,i,j)} with 1≤i<j<k≤n, and C3n sets of the form {(i,j,k),(j,k,i),(k,i,j)} with 1≤i<k<j≤n.