/* http://projecteuler.net/
 *
 * Problem 45
 * After 40755, what is the next triangle number that is also pentagonal
 * and hexagonal?
 *
 * Solution by Melkor (Filip Niksic, fniksic@gmail.com)
 *
 **/

#include <iostream>
#include <cmath>

using namespace std;

bool pentagonal(long long n) {
    long long m = static_cast<long long>(sqrtl(n * 2.0 / 3.0));
    for ( ; m*(3*m-1)/2 < n; ++m);
    if (m*(3*m-1)/2 == n)
	return true;
    else
	return false;
}

bool triangle(long long n) {
    long long m = static_cast<long long>(sqrtl(n * 2.0)) - 1;
    for ( ; m*(m+1)/2 < n; ++m);
    if (m*(m+1)/2 == n)
	return true;
    else
	return false;
}

int main() {
    for (long long k = 144; true; ++k)
	if (triangle(k*(2*k - 1)) && pentagonal(k*(2*k - 1))) {
	    cout << k*(2*k - 1) << endl;
	    break;
	}
    return 0;
}

