/* http://projecteuler.net/
 *
 * Problem 206
 * Find the unique positive integer whose square has the form
 * 1_2_3_4_5_6_7_8_9_0, where each “_” is a single digit.
 *
 * Solution by Melkor (Filip Niksic, fniksic@gmail.com)
 *
 **/

#include <iostream>

using namespace std;

bool check(long long n) {
    static const int digit[] = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1};
    for (int i = 0; i < 10; ++i) {
	if (n % 10 != digit[i])
	    return false;
	n /= 100;
    }
    return true;
}

int main() {
    // A little precalculation gives limits for the loop. Also,
    // it's easy to observe that the number in question ends with
    // 30 or 70.
    for (long long n = 1010101000; n < 1389026624; n += 100)
	if (check((n + 30) * (n + 30))) {
	    cout << n + 30 << endl;
	    break;
	}
	else if (check((n + 70) * (n + 70))) {
	    cout << n + 70 << endl;
	    break;
	}
	else
	    ;
    return 0;
}

