# 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.

import sys

tList = []
pList = []
hList = []

for i in range(286,100000):
    tr = i*(i+1)//2
    tList.append(tr)
for j in range(166,100000):
    pr = j*(3*j-1)//2
    pList.append(pr)
for k in range(144,100000):
    hr = k*(2*k-1)
    hList.append(hr)

for titem in tList:
    if titem in pList and titem in hList:
        print(titem)
        sys.exit()