public class Euler206 {
    public static void main(String[] args) {
        long low = (long) Math.ceil(Math.sqrt(1020304050607080900.0));
        long high = (long) Math.floor(Math.sqrt(1929394959697989990.0));
        while (low % 10 != 0)
            low++;
        for (long base = low; base <= high; base += 10) {
            int tail = (int) (base % 100);
            if (tail != 30 && tail != 70)
                continue;
            long sq = base * base;
            if (matchesPattern(sq)) {
                System.out.println(base);
                return;
            }
        }
    }

    static boolean matchesPattern(long sq) {
        if (sq % 10 != 0)
            return false;
        sq /= 100;
        for (int d = 9; d >= 1; d--) {
            if (sq % 10 != d)
                return false;
            sq /= 100;
        }
        return true;
    }
}
