# very similar to problem 30

fac = lambda n:reduce(lambda a,b:a*(b+1),range(n),1)
facts = dict([(str(n), fac(n)) for n in xrange(0,10)])

def is_good(num):
    strnum = str(num)
    digit_sum = 0
    for c in strnum:
        digit_sum += facts[c]
    return num == digit_sum

def find_limit():
    max_digit = facts['9']
    digits = 1
    while True:
        smallest = 10**(digits-1) # smallest n-digit number is 1x10**(n-1)
        if max_digit * digits < smallest:
            break
        digits += 1
    # since the number can be at most n-1 digits long, multiply the max digit value by n-1
    return max_digit*(digits-1)

limit = find_limit()

sum = 0
for i in xrange(3,limit):
    if is_good(i):
        sum += i
print sum