using System;
using System.Numerics;
 
namespace HackIt {
    class MainClass {
        public static void Main (string[] args) {
 
            int[] seqA = { 467241,453296,7285  };
            int[] seqB = { 583009,187016,710625  };
            int[] seqC = { 71253,967534,747065  };
            int[] seqD = { 857025,256,996065  };
 
            int a = solveSeq ('A', seqA);
            int b = solveSeq ('B', seqB);
            int c = solveSeq ('C', seqC);
            int d = solveSeq ('D', seqD);
 
            Console.WriteLine ("\nResult = " + (a + b + c + d) + "\n\n");
 
        }
 
        public static int solveSeq(char name, int[] seq) {
 
            Console.WriteLine ("\nSeq " + name + "\n");
 
            for (int i = 0; i < 3; i++) {
 
                int k1_real = 0;
                int k2_real = 0;
                int k_found = 0;
 
                BigInteger lf = new BigInteger(seq[i]);
 
                for (int k1 = 1000; k1 < 1200; k1++) {
                    for (int k2 = 1000; k2 < 1200; k2++) {
 
                        BigInteger x = y(i + 1, k1, k2);
 
                        if (x.Equals(lf)) {
                            k1_real = k1;
                            k2_real = k2;
 
                            k_found++;
                        }
                    }
                }
 
                if (k_found == 1) {
 
                    Console.WriteLine (" k1 = " + k1_real);
                    Console.WriteLine (" k2 = " + k2_real);
 
                    BigInteger result = y (4, k1_real, k2_real);
 
                    return (int) result;
                }
            }
 
 
            return 0;
        }
 
        public static BigInteger y(int x, int k1, int k2) {
            return BigInteger.ModPow(x + 2, k1, (k1 - 2) * (k2 - 2));
        }
    }
}