import java.util.*;
import java.math.BigInteger;

public class Euler221 {
    static long isqrt(long n) {
        if (n < 0)
            return 0;
        long r = (long) Math.sqrt(n);
        while ((r + 1) * (r + 1) <= n)
            r++;
        while (r * r > n)
            r--;
        return r;
    }

    static class PrimeTable {
        List<Long> primes = new ArrayList<>();
        long nextCandidate = 3;

        PrimeTable() {
            primes.add(2L);
        }

        void ensureUpTo(long limit) {
            while (primes.get(primes.size() - 1) < limit) {
                boolean isPrime = true;
                for (long p : primes) {
                    if (p * p > nextCandidate)
                        break;
                    if (nextCandidate % p == 0) {
                        isPrime = false;
                        break;
                    }
                }
                if (isPrime)
                    primes.add(nextCandidate);
                nextCandidate += 2;
            }
        }
    }

    static class Factor {
        long p;
        int exp;

        Factor(long p, int exp) {
            this.p = p;
            this.exp = exp;
        }
    }

    static void buildDivisors(List<Factor> factors, int idx, long current, long limit, List<Long> out) {
        if (idx == factors.size()) {
            out.add(current);
            return;
        }

        long p = factors.get(idx).p;
        int exp = factors.get(idx).exp;
        long power = 1;

        for (int e = 0; e <= exp; ++e) {
            if (current > limit / power)
                break;
            buildDivisors(factors, idx + 1, current * power, limit, out);
            if (e == exp || power > limit / p)
                break;
            power *= p;
        }
    }

    public static String solve() {
        int target = 150000;
        PriorityQueue<BigInteger> heap = new PriorityQueue<>(Collections.reverseOrder());
        HashSet<BigInteger> active = new HashSet<>();
        PrimeTable table = new PrimeTable();

        long p = 1;
        while (true) {
            long n = p * p + 1;
            long root = isqrt(n);
            table.ensureUpTo(root + 1);

            List<Factor> factors = new ArrayList<>();
            long rem = n;
            for (long q : table.primes) {
                if (q * q > rem)
                    break;
                if (rem % q != 0)
                    continue;
                int count = 0;
                do {
                    rem /= q;
                    count++;
                } while (rem % q == 0);
                factors.add(new Factor(q, count));
            }
            if (rem > 1) {
                factors.add(new Factor(rem, 1));
            }

            List<Long> divisors = new ArrayList<>();
            buildDivisors(factors, 0, 1, root, divisors);

            for (long d : divisors) {
                long e = n / d;
                BigInteger bp = BigInteger.valueOf(p);
                BigInteger bd = BigInteger.valueOf(d);
                BigInteger be = BigInteger.valueOf(e);
                BigInteger value = bp.multiply(bp.add(bd)).multiply(bp.add(be));

                if (heap.size() < target) {
                    if (active.add(value)) {
                        heap.offer(value);
                    }
                    continue;
                }

                BigInteger currentMax = heap.peek();
                if (value.compareTo(currentMax) >= 0)
                    continue;
                if (active.contains(value))
                    continue;

                BigInteger removed = heap.poll();
                active.remove(removed);

                heap.offer(value);
                active.add(value);
            }

            if (heap.size() == target) {
                long nextP = p + 1;
                BigInteger bNext = BigInteger.valueOf(nextP);
                BigInteger bNextP1 = BigInteger.valueOf(nextP + 1);
                BigInteger lowerBound = bNext.multiply(bNextP1).multiply(bNextP1);

                if (lowerBound.compareTo(heap.peek()) > 0) {
                    return heap.peek().toString();
                }
            }

            p++;
        }
    }

    public static void main(String[] args) {
        System.out.println(solve());
    }
}
