6. Given a positive integer $n \geqslant 2$. An $n \times n \times n$ cube is divided into $n^{3}$ unit cubes. Each unit cube is painted one color. For each $n \times n \times 1$ rectangular prism (in each of the three possible directions), consider the set of colors that appear in this rectangular prism (each color can only appear once), resulting in $3 n$ color sets, which are divided into three groups according to their direction. For any color set in a group, it also appears in each of the other two groups. Find the maximum number of colors (expressed in terms of $n$).
6. The maximum value is $\frac{n(n+1)(2 n+1)}{6}$.

Let the number of colors be $C$.
According to the direction of the shortest side, we call the $n \times n \times 1$ cuboids $x$-boxes, $y$-boxes, and $z$-boxes. Let $C_{1}$, $C_{2}$, and $C_{3}$ be the sets of colors that appear exactly once, exactly twice, and at least three times, respectively, in the $n \times n \times n$ large cube. The set of unit cubes of a color appearing in $C_{i}$ is denoted by $M_{i}$, and let $n_{i}=\left|M_{i}\right|(i=1,2,3)$.

For any $X$ in the $x$-box, let $Y$ and $Z$ be the $y$-box and $z$-box, respectively, such that $X$, $Y$, and $Z$ have the same color set.
First, we prove a lemma.
Lemma $\quad 4\left|X \cap M_{1}\right|+\left|X \cap M_{2}\right| \leqslant 3 n+1$.
Proof We consider two cases.
(1) If $X \cap M_{1} \neq \varnothing$, then there exists a unit cube in $X \cap M_{1}$ that also appears in $Y$ and $Z$. Thus, it belongs to $X \cap Y \cap Z$. This indicates that
$X \cap M_{1}=X \cap Y \cap Z$.
Therefore, $\left|X \cap M_{1}\right|=1$.
Next, consider the unit cubes in $X \cap M_{2}$.
Since the unit cubes in $X \cap Y \cap Z$ are in $M_{1}$, at most $2(n-1)$ unit cubes in $X \cap M_{2}$ can be in $X \cap Y$ or $X \cap Z$.

Let $a$ be a unit cube in $X \cap M_{2}$. Then there exists a unique unit cube $a^{\prime}$ different from $a$ with the same color, and $a^{\prime}$ belongs to $Y$ and $Z$, i.e., $a^{\prime} \in Y \cap Z$, but $a^{\prime} \notin X \cap Y \cap Z$.
Since $a \mapsto a^{\prime}$ is an injection, the number of such $a$ is at most
$|(Y \cap Z) \backslash X|=n-1$.
Thus, $\left|X \cap M_{2}\right| \leqslant 2(n-1)+(n-1)$
$=3(n-1)$.
Hence, $4\left|X \cap M_{1}\right|+\left|X \cap M_{2}\right|$
$\leqslant 4+3(n-1)=3 n+1$.
(2) If $X \cap M_{1}=\varnothing$, then $\left|X \cap M_{1}\right|=0$.
Similarly, $X \cap Y$ or $X \cap Z$ can contain at most $2 n-1$ unit cubes from $X \cap M_{2}$. The other unit cubes $a$ in $X \cap M_{2}$ correspond to some $a^{\prime} \in Y \cap Z$ (possibly $\left.a^{\prime} \in X\right)$, and there are at most $n$ such $a^{\prime}$.
Thus, $\left|X \cap M_{2}\right| \leqslant(2 n-1)+n=3 n-1$.
Hence, $4\left|X \cap M_{1}\right|+\left|X \cap M_{2}\right|$
$\leqslant 3 n-1<3 n+1$.
The lemma is proved.
For all $x$-boxes $X$, summing the inequality in the lemma yields
$4 n_{1}+n_{2} \leqslant n(3 n+1)$.
By the definition of $M_{i}$, we have
$$
\begin{array}{l}
n_{i} \geqslant i\left|C_{i}\right|(i=1,2,3), n_{1}+n_{2}+n_{3}=n^{3} . \\
\text { Then } C \leqslant n_{1}+\frac{n_{2}}{2}+\frac{n_{3}}{3} \\
=\frac{n_{1}+n_{2}+n_{3}}{3}+\frac{4 n_{1}+n_{2}}{6} \\
\leqslant \frac{n^{3}}{3}+\frac{3 n^{2}+n}{6}=\frac{n(n+1)(2 n+1)}{6} .
\end{array}
$$

Below, we provide an example to show that the equality in the above inequality holds, thus, $C_{\max }=\frac{n(n+1)(2 n+1)}{6}$.

Let the unit cubes of $n$ colors that appear alone be
$$
S_{i}=\{(i, i, i)\}(1 \leqslant i \leqslant n) ;
$$
the unit cubes of $3 \mathrm{C}_{n}^{2}$ colors that appear twice be
$$
\begin{array}{l}
D_{i, j}^{1}=\{(i, j, j),(j, i, i)\}, \\
D_{i, j}^{2}=\{(j, i, j),(i, j, i)\}, \\
D_{i, j}^{3}=\{(j, j, i),(i, i, j)\},
\end{array}
$$

where $1 \leqslant i<j \leqslant n$;
the unit cubes of $2 \mathrm{C}_{n}^{3}$ colors that appear three times be
$$
T_{i, j, k}=\{(i, j, k),(j, k, i),(k, i, j)\},
$$

where $1 \leqslant i<j<k \leqslant n$ or $1 \leqslant i<k<j \leqslant n$.
Thus, each $n \times n \times 1$ cuboid in each direction contains the same color set, and the number of colors is
$$
\begin{aligned}
& n+3 \times \frac{n(n-1)}{2}+2 \times \frac{n(n-1)(n-2)}{6} \\
= & \frac{n(n+1)(2 n+1)}{6} .
\end{aligned}
$$