import java.math.BigInteger;

public class Euler991 {
    private static final long DEFAULT_LIMIT = 10_000_000L;

    private static final class Options {
        long limit = DEFAULT_LIMIT;
        boolean runCheckpoints = true;
    }

    private static boolean parseLongAfterPrefix(String arg, String prefix, Options options) {
        if (!arg.startsWith(prefix)) {
            return false;
        }
        String tail = arg.substring(prefix.length());
        if (tail.isEmpty()) {
            throw new IllegalArgumentException("Invalid limit: " + arg);
        }
        long value = 0L;
        for (int i = 0; i < tail.length(); ++i) {
            char ch = tail.charAt(i);
            if (ch < '0' || ch > '9') {
                throw new IllegalArgumentException("Invalid limit: " + arg);
            }
            int digit = ch - '0';
            if (value > (Long.MAX_VALUE - digit) / 10L) {
                throw new IllegalArgumentException("Limit overflow: " + arg);
            }
            value = value * 10L + digit;
        }
        options.limit = value;
        return true;
    }

    private static Options parseArguments(String[] args) {
        Options options = new Options();
        for (String arg : args) {
            if ("--skip-checkpoints".equals(arg)) {
                options.runCheckpoints = false;
                continue;
            }
            if (parseLongAfterPrefix(arg, "--limit=", options)) {
                continue;
            }
            throw new IllegalArgumentException("Unknown argument: " + arg);
        }
        return options;
    }

    private static long isqrt(long n) {
        long low = 0L;
        long high = Math.min(n, 1L << 32);
        while (low < high) {
            long mid = low + (high - low + 1L) / 2L;
            if (mid <= n / mid) {
                low = mid;
            } else {
                high = mid - 1L;
            }
        }
        return low;
    }

    private static BigInteger arithmeticSeriesSum(long count) {
        return BigInteger.valueOf(count).multiply(BigInteger.valueOf(count + 1L)).divide(BigInteger.TWO);
    }

    private static BigInteger contribution(long limit, long baseSum) {
        long copies = limit / baseSum;
        return BigInteger.valueOf(baseSum).multiply(arithmeticSeriesSum(copies));
    }

    private static long gcd(long a, long b) {
        while (b != 0L) {
            long t = a % b;
            a = b;
            b = t;
        }
        return a;
    }

    private static BigInteger solve(long limit) {
        if (limit <= 0L) {
            return BigInteger.ZERO;
        }

        BigInteger total = BigInteger.ZERO;
        long maxN = isqrt(limit) + 2L;

        for (long n = 1L; n <= maxN; ++n) {
            for (long m = n + 1L; m <= 5L * n; m += 2L) {
                if (gcd(m, n) != 1L) {
                    continue;
                }

                long b = 5L * m * n - m * m - n * n;
                long c = m * m - 4L * m * n + n * n;
                if (b <= 0L || c <= 0L) {
                    continue;
                }

                long sumPlus = n * (5L * m - n);
                if (sumPlus <= limit) {
                    total = total.add(contribution(limit, sumPlus));
                }

                long aMinus = 4L * m * n - m * m;
                if (aMinus > 0L) {
                    long sumMinus = m * (5L * n - m);
                    if (sumMinus <= limit) {
                        total = total.add(contribution(limit, sumMinus));
                    }
                }
            }

            for (long m = n + 1L; m <= 2L * n; m += 2L) {
                if (gcd(m, n) != 1L) {
                    continue;
                }

                long b = 3L * m * m - 7L * n * n;
                long c = 2L * (3L * n * n - m * m);
                if (b <= 0L || c <= 0L) {
                    continue;
                }

                long sumPlus = 4L * m * m + 2L * m * n - 6L * n * n;
                if (sumPlus > 0L && sumPlus <= limit) {
                    total = total.add(contribution(limit, sumPlus));
                }

                long aMinus = 3L * m * m - 2L * m * n - 5L * n * n;
                if (aMinus > 0L) {
                    long sumMinus = 4L * m * m - 2L * m * n - 6L * n * n;
                    if (sumMinus > 0L && sumMinus <= limit) {
                        total = total.add(contribution(limit, sumMinus));
                    }
                }
            }
        }

        return total;
    }

    private static BigInteger bruteForce(long limit) {
        BigInteger total = BigInteger.ZERO;
        for (long a = 1L; a <= limit; ++a) {
            for (long b = 1L; a + b <= limit; ++b) {
                for (long c = 1L; a + b + c <= limit; ++c) {
                    long lhs = a * (a + c) + (b + c) * (b + c);
                    long rhs = 4L * (b + c) * (a + c);
                    if (lhs == rhs) {
                        total = total.add(BigInteger.valueOf(a + b + c));
                    }
                }
            }
        }
        return total;
    }

    private static void runCheckpoints() {
        if (!solve(50L).equals(bruteForce(50L))) {
            throw new IllegalStateException("Checkpoint failed for limit=50");
        }
        if (!solve(200L).equals(bruteForce(200L))) {
            throw new IllegalStateException("Checkpoint failed for limit=200");
        }
    }

    public static void main(String[] args) {
        try {
            Options options = parseArguments(args);
            if (options.runCheckpoints) {
                runCheckpoints();
            }
            System.out.println(solve(options.limit));
        } catch (IllegalArgumentException ex) {
            System.err.println(ex.getMessage());
            System.exit(1);
        } catch (IllegalStateException ex) {
            System.err.println(ex.getMessage());
            System.exit(2);
        }
    }
}
