12. (22 points) In a regular 2018-gon, a line segment is drawn between every pair of vertices, and each line segment is colored either red or blue. Find the minimum number of triangles in this figure whose three sides are all the same color.

保留源文本的换行和格式，翻译结果如下：

12. (22 points) In a regular 2018-gon, a line segment is drawn between every pair of vertices, and each line segment is colored either red or blue. Find the minimum number of triangles in this figure whose three sides are all the same color.
12. Let $N$ be the number of triangles in this graph with all three sides of the same color, and $M$ be the number of triangles with not all three sides of the same color, and $x_{i}$ be the number of red segments with the $i$-th vertex as an endpoint. Then
$$
\begin{array}{l}
M+N=\mathrm{C}_{2018}^{3}, \\
\sum_{i=1}^{2018} x_{i}\left(2017-x_{i}\right)=2 M .
\end{array}
$$

$N$ achieves its minimum value when and only when $x_{i}=1008$ or 1009:
$$
\mathrm{C}_{2018}^{3}-1009^{2} \times 1008=2 \mathrm{C}_{1009}^{3} .
$$

Thus, $N=2 \mathrm{C}_{1009}^{3}$ is achievable.
For example, color the segments $i \rightarrow i \pm j(\bmod 2018)(1 \leqslant i \leqslant 2018,1 \leqslant j \leqslant 504)$ red, and color the other segments blue.