# -*- coding:utf-8 -*-

"""
Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:

Triangle	 	Tn=n(n+1)/2	 	1, 3, 6, 10, 15, ...
Pentagonal	 	Pn=n(3n−1)/2	 	1, 5, 12, 22, 35, ...
Hexagonal	 	Hn=n(2n−1)	 	1, 6, 15, 28, 45, ...
It can be verified that T285 = P165 = H143 = 40755.

Find the next triangle number that is also pentagonal and hexagonal.

"""

def triangle_set(limit):
	s = set([])
	for n in xrange(1,limit + 1):
		s.add( n * ( n + 1) / 2)
	return s

def pentagonal_set(limit):
	s = set([])
	for n in xrange(1,limit + 1):
		s.add( n * ( 3 * n - 1) / 2)
	return s

def hexagonal_set(limit):
	s = set([])
	for n in xrange(1,limit + 1):
		s.add( n * ( 2 * n - 1))
	return s
	

if __name__ == "__main__":
	limit = 100000
	t = triangle_set(limit)
	p = pentagonal_set(limit)
	h = hexagonal_set(limit)
	l = list(t)
	l.sort()
	for i in l[285:]:
		if i in p and i in h:
			print i
			break
# Answer:1533776805
