static bool Turn3(int i, int j, int k, Paralepiped P, char[] color)
        {
            //проверяет на наличие 3 цветов и поворачивает относительно их
            if (P.TestColor(3, new char[] { color[i], color[j], color[k] }) == true)
            {
                if (P.GetColor()[3] == color[i] || P.GetColor()[2] == color[i]) P.TurnHorisontal();
                while (P.GetColor()[i] != color[i])
                    P.TurnVertical();
                while (P.GetColor()[j] != color[j])
                    P.TurnHorisontal();

                return true;
            }
            return false;
        }
 static bool Turn2(int i, int j, Paralepiped P, char[] color)
 {
     //проверяет на наличие 2 цветов и поворачивает относительно их
     if (P.TestColor(2, new char[] { color[i], color[j] }) == true)
         if (i == 0 || i == 1)
         {
             if (P.GetColor()[3] == color[i] || P.GetColor()[2] == color[i])
                 P.TurnHorisontal();
             while (P.GetColor()[i] != color[i])
                 P.TurnVertical();
             while (P.GetColor()[j] != color[j])
                 P.TurnHorisontal();
             return true;
         }
         else
         {
             if (P.GetColor()[0] == color[i] || P.GetColor()[1] == color[i])
                 P.TurnVertical();
             while (P.GetColor()[i] != color[i])
                 P.TurnHorisontal();
             while (P.GetColor()[j] != color[j])
                 P.TurnVertical();
             return true;
         }
     return false;
 }
 static void Main(string[] args)
 {
     Paralepiped FulP = new Paralepiped();
     ISet<Paralepiped> SetP = new HashSet<Paralepiped>();
     ISet<Paralepiped> SetF = new HashSet<Paralepiped>();
     int N = ReadConsole(ref FulP, SetP);
     Algorithm(SetP, SetF, FulP);
     WriteConsole(SetF, N);
     Console.ReadLine();
 }
 static int ReadConsole(ref Paralepiped FulP, ISet<Paralepiped> SetP)
 {
     int N;
     string str = Console.ReadLine();
     string[] parse = str.Split(new char[] { ' ' });
     FulP = new Paralepiped(int.Parse(parse[0]), int.Parse(parse[1]), int.Parse(parse[2]), parse[3].ToCharArray(0, 6), 0);
     str = Console.ReadLine();
     N = int.Parse(str);
     for (int i = 0; i < N; i++)
     {
         str = Console.ReadLine();
         parse = str.Split(new char[] { ' ' });
         SetP.Add(new Paralepiped(int.Parse(parse[0]), int.Parse(parse[1]), int.Parse(parse[2]), parse[3].ToCharArray(0, 6), i + 1));
     }
     return N;
 }
        static void Algorithm(ISet<Paralepiped> SetP, ISet<Paralepiped> SetF, Paralepiped FulP)
        {
            char[] color = FulP.GetColor();
            //подсчитываем колличество паралепипедов на стыках сторон
            int n_d = 0, n_w = 0, n_h = 0;
            foreach (Paralepiped P in SetP)
            {
                if (P.TestColor(2, new char[] { color[0], color[2] }) == true)
                    n_h++;
                if (P.TestColor(2, new char[] { color[2], color[4] }) == true)
                    n_w++;
                if (P.TestColor(2, new char[] { color[0], color[4] }) == true)
                    n_d++;
            }
            int[] h_mas = new int[n_h];
            int[] d_mas = new int[n_d];
            int[] w_mas = new int[n_w];

            //-------------------------------------------------------------------------------------------------
            //устанавливаем паралепипеды с 3 цветами
            for (int i = 0; i < 2; i++)
                for (int j = 2; j < 4; j++)
                    for (int k = 4; k < 6; k++)
                        foreach (Paralepiped P in SetP)
                            if (Turn3(i, j, k, P, color) == true)
                            {
                                SetP.Remove(P);
                                P.SetXYZ((i == 0) ? 0 : FulP.GetW() - P.GetW(), (j == 2) ? 0 : FulP.GetD() - P.GetD(), (k == 4) ? 0 : FulP.GetH() - P.GetH());
                                w_mas[(i == 0) ? 0 : w_mas.Length - 1] = (i == 0) ? P.GetW() : FulP.GetW();
                                d_mas[(j == 2) ? 0 : d_mas.Length - 1] = (j == 2) ? P.GetD() : FulP.GetD();
                                h_mas[(k == 4) ? 0 : h_mas.Length - 1] = (k == 4) ? P.GetH() : FulP.GetH();
                                SetF.Add(P);
                                break;
                            }
            //------------------------------------------------------------------------------------------------
            U2(w_mas, d_mas, h_mas, SetP, SetF, color);
            U1(w_mas, d_mas, h_mas, SetP, SetF, color);
            //устанавливаем паралепипеды без цветов
            for (int i = 1; i < w_mas.Length - 1; i++)
                for (int j = 1; j < d_mas.Length - 1; j++)
                    for (int k = 1; k < h_mas.Length - 1; k++)
                        foreach (Paralepiped P in SetP)
                            if (P.TestSize(w_mas[i] - w_mas[i - 1], d_mas[j] - d_mas[j - 1], h_mas[k] - h_mas[k - 1]) == true)
                            {
                                SetP.Remove(P);
                                if (P.GetW() != w_mas[i] - w_mas[i - 1])
                                    if (P.GetH() == w_mas[i] - w_mas[i - 1])
                                        P.TurnVertical();
                                    else
                                        P.TurnHorisontal1();
                                if (P.GetH() != h_mas[k] - h_mas[k - 1])
                                    P.TurnHorisontal();
                                P.SetXYZ(w_mas[i - 1], d_mas[j - 1], h_mas[k - 1]);
                                SetF.Add(P);
                                break;
                            }
        }