from math import factorial


def curio(n):
    sum = 0
    for i in str(n):
        sum += factorial(int(i))
    if sum % n == 0:
        return True
    return False


N = int(input())
sum = 0
for i in range(10, N):
    if curio(i):
        sum += i
print(sum)

dc812b844a6816e9096aae51ae4e4a196e210326
<mask token>

<mask token>


def curio(n):
    sum = 0
    for i in str(n):
        sum += factorial(int(i))
    if sum % n == 0:
        return True
    return False


<mask token>

<mask token>


def curio(n):
    sum = 0
    for i in str(n):
        sum += factorial(int(i))
    if sum % n == 0:
        return True
    return False


<mask token>
for i in range(10, N):
    if curio(i):
        sum += i
print(sum)

<mask token>


def curio(n):
    sum = 0
    for i in str(n):
        sum += factorial(int(i))
    if sum % n == 0:
        return True
    return False


N = int(input())
sum = 0
for i in range(10, N):
    if curio(i):
        sum += i
print(sum)

from math import factorial


def curio(n):
    sum = 0
    for i in str(n):
        sum += factorial(int(i))
    if sum % n == 0:
        return True
    return False


N = int(input())
sum = 0
for i in range(10, N):
    if curio(i):
        sum += i
print(sum)
