import java.util.*;
import java.io.*;

public class FiniteAbelian {
	public static int threshold;
	public static int[][][][] values;
	public static int tries;
	public static int temp;
	public static int fails = 0;
	public static int curfail;
	public static int[][][][] res;
	public static int[][][][][][] conditions;
	public static Random rand = new Random();

	public static void main(String[] args) {
		//APKill(a,b,c) gives the necessary permutation on Z/aZxZ/bZxZ/cZ for Theorem 21.
		APKill(3, 3, 1);
		APKill(5, 5, 1);
		APKill(7, 7, 1);
		APKill(3, 3, 5);
		APKill(3, 3, 7);
		APKill(3, 5, 5);
		APKill(3, 7, 7);
		APKill(5, 5, 7);
		APKill(5, 7, 7);
		APKill(9, 3, 1);
		APKill(25, 5, 1);
		APKill(49, 7, 1);
	}

	public static int[][][][] getStartingPerm(int p, int q, int r) {
		int[][][][] result = new int[p][q][r][3];
		for (int i = 0; i < p; i++) {
			for (int j = 0; j < q; j++) {
				for (int k = 0; k < r; k++) {
					result[i][j][k][0] = f(i, p);
					result[i][j][k][1] = f(j, q);
					result[i][j][k][2] = f(k, r);
				}
			}

		}
		return result;
	}

	public static int f(int i, int p) {
		if (p == 1)
			return 0;
		if (p == 2)
			return 1 - i;
		if (p == 3)
			return (4 - i) % 3;
		if (p == 5) {
			if (i <= 2)
				return i;
			return 7 - i;
		}
		if (p == 7) {
			if (i <= 3)
				return i;
			return (i % 3) + 4;
		}
		if (p == 49) {
			return 7 * f(i / 7, 7) + f(i % 7, 7);
		}
		return i;

	}

	public static void APKill(int p, int q, int r) {
		threshold = p * q * r;
		tries = 0;
		fails = 0;
		res = getStartingPerm(p, q, r);
		conditions = new int[p][p][q][q][r][r];
		for (int a = 0; a < p; a++) {
			for (int b = 0; b < p; b++) {
				for (int c = 0; c < q; c++) {
					for (int d = 0; d < q; d++) {
						for (int e = 0; e < r; e++) {
							for (int f = 0; f < r; f++) {
								if (isAP(res[a][c][e], res[(a + b) % p][(c + d) % q][(e + f) % r],
										res[(a + 2 * b) % p][(c + 2 * d) % q][(e + 2 * f) % r], p, q, r)) {
									conditions[a][b][c][d][e][f] = 1;
									fails++;
								} else
									conditions[a][b][c][d][e][f] = 0;
							}
						}
					}
				}
			}
		}

		while (fails > 0) {
			curfail = fails;
			int t = rand.nextInt(p);
			int u = rand.nextInt(p);
			int v = rand.nextInt(q);
			int w = rand.nextInt(q);
			int x = rand.nextInt(r);
			int y = rand.nextInt(r);
			swap(t, u, v, w, x, y);
			update(t, u, v, w, x, y, p, q, r);
			if (fails >= curfail) {
				swap(t, u, v, w, x, y);
				update(t, u, v, w, x, y, p, q, r);
				tries++;

			}

			else {
				tries = 0;
			}
			if (tries > threshold) {
				threshold = p * q * r;
				tries = 0;
				fails = 0;
				res = getStartingPerm(p, q, r);
				conditions = new int[p][p][q][q][r][r];
				for (int a = 0; a < p; a++) {
					for (int b = 0; b < p; b++) {
						for (int c = 0; c < q; c++) {
							for (int d = 0; d < q; d++) {
								for (int e = 0; e < r; e++) {
									for (int f = 0; f < r; f++) {
										if (isAP(res[a][c][e], res[(a + b) % p][(c + d) % q][(e + f) % r],
												res[(a + 2 * b) % p][(c + 2 * d) % q][(e + 2 * f) % r], p, q, r)) {
											conditions[a][b][c][d][e][f] = 1;
											fails++;
										} else
											conditions[a][b][c][d][e][f] = 0;
									}
								}
							}
						}
					}
				}
			}
		}
		System.out.println("("+p+","+q+","+r+"): "+toString(res));
	}

	public static String toString(int[][][][] a) {
		String s = "\n";
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[0].length; j++) {
				for (int k = 0; k < a[0][0].length; k++) {
					s += ("(" + i + "," + j + "," + k + "): (" + a[i][j][k][0] + "," + a[i][j][k][1] + ","
							+ a[i][j][k][2] + ")");
				}
				s += "\n";
			}

		}
		return s;
	}

	public static void swap(int a, int b, int c, int d, int e, int f) {
		for (int i = 0; i <= 2; i++) {
			temp = res[a][c][e][i];
			res[a][c][e][i] = res[b][d][f][i];
			res[b][d][f][i] = temp;
		}

	}

	public static void update(int a, int b, int c, int d, int e, int f, int p, int q, int r) {
		for (int x = 0; x < p; x++) {
			for (int y = 0; y < q; y++) {
				for (int z = 0; z < r; z++) {

					if (isAP(res[b][d][f], res[(b + x) % p][(d + y) % q][(f + z) % r],
							res[(b + 2 * x) % p][(d + 2 * y) % q][(f + 2 * z) % r], p, q, r)) {
						if (conditions[b][x][d][y][f][z] == 0) {
							conditions[b][x][d][y][f][z] = 1;
							fails++;
						}

					} else {
						if (conditions[b][x][d][y][f][z] == 1) {
							conditions[b][x][d][y][f][z] = 0;
							fails--;
						}
					}
					if (isAP(res[(b - x + p) % p][(d - y + q) % q][(f - z + r) % r], res[b][d][f],
							res[(b + x) % p][(d + y) % q][(f + z) % r], p, q, r)) {
						if (conditions[(b - x + p) % p][x][(d - y + q) % q][y][(f - z + r) % r][z] == 0) {
							conditions[(b - x + p) % p][x][(d - y + q) % q][y][(f - z + r) % r][z] = 1;
							fails++;
						}

					} else {
						if (conditions[(b - x + p) % p][x][(d - y + q) % q][y][(f - z + r) % r][z] == 1) {
							conditions[(b - x + p) % p][x][(d - y + q) % q][y][(f - z + r) % r][z] = 0;
							fails--;
						}
					}
					if (isAP(res[(b - 2 * x + 2 * p) % p][(d - 2 * y + 2 * q) % q][(f - 2 * z + 2 * r) % r],
							res[(b - x + p) % p][(d - y + q) % q][(f - z + r) % r], res[b][d][f], p, q, r)) {
						if (conditions[(b - 2 * x + 2 * p) % p][x][(d - 2 * y + 2 * q) % q][y][(f - 2 * z + 2 * r)
								% r][z] == 0) {
							conditions[(b - 2 * x + 2 * p) % p][x][(d - 2 * y + 2 * q) % q][y][(f - 2 * z + 2 * r)
									% r][z] = 1;
							fails++;
						}

					} else {
						if (conditions[(b - 2 * x + 2 * p) % p][x][(d - 2 * y + 2 * q) % q][y][(f - 2 * z + 2 * r)
								% r][z] == 1) {
							conditions[(b - 2 * x + 2 * p) % p][x][(d - 2 * y + 2 * q) % q][y][(f - 2 * z + 2 * r)
									% r][z] = 0;
							fails--;
						}
					}

					if (isAP(res[a][c][e], res[(a + x) % p][(c + y) % q][(e + z) % r],
							res[(a + 2 * x) % p][(c + 2 * y) % q][(e + 2 * z) % r], p, q, r)) {
						if (conditions[a][x][c][y][e][z] == 0) {
							conditions[a][x][c][y][e][z] = 1;
							fails++;
						}

					} else {
						if (conditions[a][x][c][y][e][z] == 1) {
							conditions[a][x][c][y][e][z] = 0;
							fails--;
						}
					}
					if (isAP(res[(a - x + p) % p][(c - y + q) % q][(e - z + r) % r], res[a][c][e],
							res[(a + x) % p][(c + y) % q][(e + z) % r], p, q, r)) {
						if (conditions[(a - x + p) % p][x][(c - y + q) % q][y][(e - z + r) % r][z] == 0) {
							conditions[(a - x + p) % p][x][(c - y + q) % q][y][(e - z + r) % r][z] = 1;
							fails++;
						}

					} else {
						if (conditions[(a - x + p) % p][x][(c - y + q) % q][y][(e - z + r) % r][z] == 1) {
							conditions[(a - x + p) % p][x][(c - y + q) % q][y][(e - z + r) % r][z] = 0;
							fails--;
						}
					}
					if (isAP(res[(a - 2 * x + 2 * p) % p][(c - 2 * y + 2 * q) % q][(e - 2 * z + 2 * r) % r],
							res[(a - x + p) % p][(c - y + q) % q][(e - z + r) % r], res[a][c][e], p, q, r)) {
						if (conditions[(a - 2 * x + 2 * p) % p][x][(c - 2 * y + 2 * q) % q][y][(e - 2 * z + 2 * r)
								% r][z] == 0) {
							conditions[(a - 2 * x + 2 * p) % p][x][(c - 2 * y + 2 * q) % q][y][(e - 2 * z + 2 * r)
									% r][z] = 1;
							fails++;
						}

					} else {
						if (conditions[(a - 2 * x + 2 * p) % p][x][(c - 2 * y + 2 * q) % q][y][(e - 2 * z + 2 * r)
								% r][z] == 1) {
							conditions[(a - 2 * x + 2 * p) % p][x][(c - 2 * y + 2 * q) % q][y][(e - 2 * z + 2 * r)
									% r][z] = 0;
							fails--;
						}
					}

				}
			}
		}

	}

	public static boolean isAP(int[] x, int[] y, int[] z, int p, int q, int r) {
		if (x[0] == y[0] && x[1] == y[1] && x[2] == y[2])
			return false;
		if (modAP(x[0], y[0], z[0], p) == 0 && modAP(x[1], y[1], z[1], q) == 0 && modAP(x[2], y[2], z[2], r) == 0)
			return true;
		return false;
	}

	public static int modAP(int a, int b, int c, int n) {
		return (a + c - 2 * b + 2 * n) % n;
	}

	public static boolean isPrime(int a) {
		for (int i = 2; i * i <= a; i++) {
			if (a % i == 0)
				return false;
		}
		return true;
	}
}