/* http://projecteuler.net/
 *
 * Problem 46
 * What is the smallest odd composite that cannot be written as the sum
 * of a prime and twice a square?
 *
 * Solution by Melkor (Filip Niksic, fniksic@gmail.com)
 *
 **/

#include <iostream>
#include <bitset>
#include <vector>

using namespace std;

const int max_num = 2000000;

bitset<max_num> sieve;

void generate_primes() {
    sieve.set();
    sieve.reset(0);
    sieve.reset(1);

    for (int i = 2; i*i < max_num; ++i)
	if (sieve[i]) {
	    for (int j = i+i; j < max_num; j += i)
		sieve.reset(j);
	}
}

int find() {
    for (int i = 9; i < max_num; i += 2) {
	bool ok = true;
	for (int j = 0; 2*j*j < i; ++j)
	    if (sieve[i - 2*j*j]) {
		ok = false;
		break;
	    }
	if (ok)
	    return i;
    }
    return -1;
}

int main() {
    generate_primes();
    cout << find() << endl;
    return 0;
}

