class Euler47 {

    private static class Options {
        int consecutive = 4;
        int factors = 4;
        boolean runCheckpoints = true;
    }

    private static boolean parse_int_after_prefix(String arg, String prefix, int[] value) {
        if (!arg.startsWith(prefix)) {
            return false;
        }
        String tail = arg.substring(prefix.length());
        if (tail.isEmpty()) {
            return false;
        }

        int parsed = 0;
        for (int i = 0; i < tail.length(); i++) {
            char c = tail.charAt(i);
            if (c < '0' || c > '9') {
                return false;
            }
            parsed = parsed * 10 + (c - '0');
        }
        value[0] = parsed;
        return true;
    }

    private static boolean parseArguments(String[] args, Options options) {
        for (String arg : args) {
            if ("--skip-checkpoints".equals(arg)) {
                options.runCheckpoints = false;
                continue;
            }
            int[] temp = new int[1];
            if (parse_int_after_prefix(arg, "--consecutive=", temp)) {
                options.consecutive = temp[0];
                continue;
            }
            if (parse_int_after_prefix(arg, "--factors=", temp)) {
                options.factors = temp[0];
                continue;
            }

            System.err.println("Unknown argument: " + arg);
            return false;
        }

        return options.consecutive >= 1 && options.factors >= 1;
    }

    private static int distinctPrimeFactorCount(int n) {
        int count = 0;

        if ((n % 2) == 0) {
            ++count;
            while ((n % 2) == 0) {
                n /= 2;
            }
        }

        for (int p = 3; p <= n / p; p += 2) {
            if ((n % p) == 0) {
                ++count;
                while ((n % p) == 0) {
                    n /= p;
                }
            }
        }

        if (n > 1) {
            ++count;
        }

        return count;
    }

    private static int solve(int consecutive, int factors) {
        int run = 0;
        int runStart = 2;

        for (int n = 2; ; ++n) {
            if (distinctPrimeFactorCount(n) == factors) {
                if (run == 0) {
                    runStart = n;
                }
                ++run;
                if (run == consecutive) {
                    return runStart;
                }
            } else {
                run = 0;
            }
        }
    }

    private static boolean runCheckpoints() {
        if (distinctPrimeFactorCount(14) != 2) {
            System.err.println("Checkpoint failed for factor count of 14");
            return false;
        }
        if (solve(2, 2) != 14) {
            System.err.println("Checkpoint failed for consecutive=2 factors=2");
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        Options options = new Options();
        if (!parseArguments(args, options)) {
            System.exit(1);
        }
        if (options.runCheckpoints && !runCheckpoints()) {
            System.exit(2);
        }

        System.out.println(solve(options.consecutive, options.factors));
    }
}
