public class Euler142 {
    static boolean isSquare(long x) {
        if (x < 0)
            return false;
        long r = (long) Math.sqrt(x);
        if (r * r == x)
            return true;
        r++;
        return r * r == x;
    }

    public static void main(String[] args) {
        int limit = 5000;
        for (int i = 4; i <= limit; i++) {
            long a = (long) i * i;
            for (int j = 3; j < i; j++) {
                long c = (long) j * j;
                long f = a - c;
                if (f <= 0 || !isSquare(f))
                    continue;
                int kstart = (j % 2 == 0) ? 2 : 1;
                for (int k = kstart; k < j; k += 2) {
                    long d = (long) k * k;
                    long e = a - d;
                    long b = c - e;
                    if (b <= 0 || e <= 0 || !isSquare(b) || !isSquare(e))
                        continue;
                    long x = (d + c) / 2, y = (e + f) / 2, z = (c - d) / 2;
                    if (x > y && y > z && z > 0 &&
                            isSquare(x + y) && isSquare(x - y) &&
                            isSquare(x + z) && isSquare(x - z) &&
                            isSquare(y + z) && isSquare(y - z)) {
                        System.out.println(x + y + z);
                        return;
                    }
                }
            }
        }
    }
}
