import java.util.*;

public class Euler348 {

    static long isqrt(long n) {
        long x = (long) Math.sqrt(n);
        while ((x + 1) * (x + 1) <= n && (x + 1) > 0)
            x++;
        while (x * x > n)
            x--;
        return x;
    }

    static long pow10(int exp) {
        long val = 1;
        for (int i = 0; i < exp; i++)
            val *= 10;
        return val;
    }

    static long makePalindrome(long prefix, boolean oddLength) {
        long tail = oddLength ? prefix / 10 : prefix;
        long palindrome = prefix;
        while (tail > 0) {
            palindrome = palindrome * 10 + (tail % 10);
            tail /= 10;
        }
        return palindrome;
    }

    static void ensureCubesUpTo(long n, List<Long> cubes, long[] nextBase) {
        while (true) {
            long cube = nextBase[0] * nextBase[0] * nextBase[0];
            if (cube + 4 > n)
                break;
            cubes.add(cube);
            nextBase[0]++;
        }
    }

    static int countRepresentations(long n, List<Long> cubes) {
        int count = 0;
        for (long cube : cubes) {
            if (cube + 4 > n)
                break;
            long remaining = n - cube;
            long root = isqrt(remaining);
            if (root >= 2 && root * root == remaining) {
                count++;
                if (count > 4)
                    break;
            }
        }
        return count;
    }

    public static String solve() {
        int targetCount = 5;
        List<Long> cubes = new ArrayList<>();
        long[] nextBase = { 2L };
        List<Long> found = new ArrayList<>();

        for (int length = 1; found.size() < targetCount; length++) {
            boolean oddLength = (length % 2) == 1;
            int prefixDigits = (length + 1) / 2;
            long begin = (length == 1) ? 1L : pow10(prefixDigits - 1);
            long end = pow10(prefixDigits) - 1L;

            for (long prefix = begin; prefix <= end; prefix++) {
                long palindrome = makePalindrome(prefix, oddLength);
                if (palindrome < 12L)
                    continue;
                ensureCubesUpTo(palindrome, cubes, nextBase);
                if (countRepresentations(palindrome, cubes) == 4) {
                    found.add(palindrome);
                    if (found.size() == targetCount)
                        break;
                }
            }
        }

        long sum = 0;
        for (long v : found)
            sum += v;
        return String.valueOf(sum);
    }

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