n = input()
if '3' in n or '6' in n or '9' in n:
print(f'淘淘不会数{n}')
else:
ls = []
for i in range(1, int(n) + 1):
if '3' not in str(i) and '6' not in str(i) and '9' not in str(i):
ls.append(i)
print(sum(ls))
12-17