import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

public class Euler799 {

    static class GaussInt {
        BigInteger x, y;

        GaussInt(BigInteger x, BigInteger y) {
            this.x = x;
            this.y = y;
        }

        GaussInt(long x, long y) {
            this.x = BigInteger.valueOf(x);
            this.y = BigInteger.valueOf(y);
        }
    }

    static GaussInt multiply(GaussInt a, GaussInt b) {
        BigInteger axbx = a.x.multiply(b.x);
        BigInteger ayby = a.y.multiply(b.y);
        BigInteger axby = a.x.multiply(b.y);
        BigInteger aybx = a.y.multiply(b.x);
        return new GaussInt(axbx.subtract(ayby), axby.add(aybx));
    }

    static GaussInt conjugate(GaussInt a) {
        return new GaussInt(a.x, a.y.negate());
    }

    static ArrayList<Integer> primeList(int limit) {
        boolean[] composite = new boolean[limit + 1];
        ArrayList<Integer> primes = new ArrayList<>();
        for (int i = 2; i <= limit; i++) {
            if (!composite[i]) {
                primes.add(i);
                if ((long) i * i <= limit) {
                    for (int j = i * i; j <= limit; j += i) {
                        composite[j] = true;
                    }
                }
            }
        }
        return primes;
    }

    static GaussInt sumOfTwoSquaresPrime(int p) {
        for (long x = 1; x * x <= p; x++) {
            long y2 = p - x * x;
            long y = (long) Math.sqrt(y2);
            if (y * y == y2) {
                return new GaussInt(x, y);
            }
        }
        return new GaussInt(0, 0);
    }

    static class Factor {
        int prime;
        int exp;

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

    static class Pair {
        long x, y;

        Pair(long x, long y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o)
                return true;
            if (o == null || getClass() != o.getClass())
                return false;
            Pair pair = (Pair) o;
            return x == pair.x && y == pair.y;
        }

        @Override
        public int hashCode() {
            return java.util.Objects.hash(x, y);
        }
    }

    static long factorWithPrimes(long value, ArrayList<Integer> primes, int maxPrime, ArrayList<Factor> factors) {
        factors.clear();
        for (int p : primes) {
            if (p > maxPrime)
                break;
            if (value % p != 0)
                continue;
            int e = 0;
            do {
                value /= p;
                e++;
            } while (value % p == 0);
            factors.add(new Factor(p, e));
        }
        return value;
    }

    static int countFromFactorization(ArrayList<Factor> factors, HashMap<Integer, GaussInt> gaussianPrimes) {
        ArrayList<GaussInt> reps = new ArrayList<>();
        reps.add(new GaussInt(1, 0));

        for (Factor f : factors) {
            GaussInt base = gaussianPrimes.get(f.prime);
            GaussInt[] powers = new GaussInt[f.exp + 1];
            powers[0] = new GaussInt(1, 0);
            for (int i = 1; i <= f.exp; i++) {
                powers[i] = multiply(powers[i - 1], base);
            }

            GaussInt[] terms = new GaussInt[f.exp + 1];
            for (int i = 0; i <= f.exp; i++) {
                terms[i] = multiply(powers[i], conjugate(powers[f.exp - i]));
            }

            ArrayList<GaussInt> nextReps = new ArrayList<>();
            for (GaussInt r : reps) {
                for (GaussInt t : terms) {
                    nextReps.add(multiply(r, t));
                }
            }
            reps = nextReps;
        }

        HashSet<Pair> uniq = new HashSet<>();
        for (GaussInt rep : reps) {
            BigInteger bx = rep.x.abs();
            BigInteger by = rep.y.abs();

            if (bx.equals(BigInteger.ZERO) || by.equals(BigInteger.ZERO))
                continue;

            BigInteger bxx = bx.min(by);
            BigInteger byy = bx.max(by);

            BigInteger six = BigInteger.valueOf(6);
            BigInteger five = BigInteger.valueOf(5);

            if (bxx.remainder(six).equals(five) && byy.remainder(six).equals(five)) {
                uniq.add(new Pair(bxx.longValueExact(), byy.longValueExact()));
            }
        }
        return uniq.size();
    }

    static long solveN() {
        int threshold = 100;
        int primeLimit = 400;
        ArrayList<Integer> primes = primeList(primeLimit);

        HashMap<Integer, GaussInt> gaussianPrimes = new HashMap<>();
        gaussianPrimes.put(2, new GaussInt(1, 1));

        for (int p : primes) {
            if (p == 2 || p % 4 == 3)
                continue;
            gaussianPrimes.put(p, sumOfTwoSquaresPrime(p));
        }

        ArrayList<Factor> factors = new ArrayList<>();

        for (long n = 1;; n++) {
            long k = 6 * n - 1;
            long value = k * k + 1;

            long rem = factorWithPrimes(value, primes, primeLimit, factors);
            if (rem != 1)
                continue;

            int bound = 1;
            for (Factor f : factors) {
                bound *= (f.exp + 1);
                if (bound > 2 * (threshold + 1))
                    break;
            }
            if (bound / 2 <= threshold)
                continue;

            int ways = countFromFactorization(factors, gaussianPrimes);
            if (ways > threshold) {
                return n * (3 * n - 1) / 2;
            }
        }
    }

    public static String solve() {
        return Long.toString(solveN());
    }

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