import math
import time
import random
# Testing factorials
# [10008, 5780, 5743, 5782, 5807, 5726, 5746, 5729, 5862, 5783]
# [9806, 5881, 5721, 5776, 5686, 5837, 5814, 5751, 5940, 5763]
# [9924, 5869, 5836, 5710, 5727, 5892, 5676, 5820, 5730, 5795]
# [9903, 5888, 5842, 5741, 5769, 5818, 5748, 5739, 5742, 5793]
# [9840, 5860, 5701, 5843, 5875, 5761, 5790, 5764, 5737, 5816]
# [9867, 5824, 5715, 5766, 5793, 5872, 5801, 5718, 5874, 5761]
# [10076, 5806, 5681, 5785, 5767, 5753, 5869, 5765, 5721, 5777]
# [9811, 5761, 5734, 5893, 5802, 5891, 5861, 5779, 5654, 5818]
# [9956, 5626, 5798, 5762, 5951, 5729, 5854, 5869, 5700, 5763]
# [9857, 5765, 5832, 5814, 5595, 5785, 5729, 5912, 5820, 5903]
# [9862, 5749, 5828, 5773, 5819, 5859, 5862, 5733, 5800, 5736]
# [9841, 5711, 5825, 5798, 5836, 5749, 5896, 5742, 5778, 5849]
# [10014, 5845, 5824, 5818, 5730, 5757, 5745, 5825, 5792, 5679]
# [9862, 5773, 5845, 5770, 5772, 5844, 5732, 5746, 5851, 5839]
# [9801, 5763, 5927, 5751, 5914, 5825, 5822, 5713, 5749, 5777]
# [9912, 5912, 5709, 5846, 5889, 5698, 5794, 5709, 5788, 5793]
# [9880, 5816, 5862, 5729, 5745, 5838, 5743, 5793, 5801, 5848]
# [9969, 5733, 5816, 5728, 5893, 5691, 5741, 5851, 5802, 5839]
# [9913, 5806, 5836, 5841, 5874, 5777, 5713, 5767, 5771, 5769]
# [9832, 5827, 5799, 5721, 5774, 5956, 5762, 5680, 5862, 5858]
# [9894, 5648, 5905, 5615, 5756, 5744, 5850, 5880, 5896, 5888]
# [9851, 5932, 5808, 5795, 5804, 5674, 5790, 5723, 5773, 5930]
# [9954, 5748, 5810, 5860, 5778, 5730, 5767, 5883, 5731, 5827]
# [9882, 5758, 5760, 5803, 5863, 5767, 5781, 5748, 5854, 5877]
# [9917, 5890, 5649, 5858, 5843, 5737, 5881, 5794, 5775, 5753]
# [9939, 5923, 5729, 5833, 5792, 5688, 5809, 5753, 5724, 5911]
# [9965, 5825, 5919, 5792, 5728, 5736, 5806, 5861, 5741, 5736]
# [10033, 5766, 5863, 5642, 5735, 5827, 5983, 5682, 5745, 5838]
# [9890, 6096, 5724, 5735, 5832, 5752, 5702, 5725, 5829, 5833]
# [9837, 5771, 5801, 5836, 5810, 5713, 5686, 5948, 5874, 5846]
# [9889, 5724, 5888, 5791, 5822, 5835, 5838, 5744, 5783, 5812]
# [9872, 5663, 5876, 5822, 5907, 5759, 5723, 5753, 5808, 5947]
# [9940, 5854, 5754, 5887, 5682, 5616, 5843, 5867, 5772, 5920]
# [9820, 5894, 5859, 5820, 5894, 5893, 5672, 5726, 5789, 5772]
# [10029, 5690, 5852, 5860, 5751, 5831, 5796, 5851, 5780, 5707]
# [1308, 658, 687, 684, 694, 636, 695, 685, 672, 693]
# [5154, 2884, 2973, 2972, 2901, 2993, 2980, 2941, 2961, 2923]#
# [9832, 5827, 5799, 5721, 5774, 5956, 5762, 5680, 5862, 5858]
numbers = input("").split(" ")
numbers = [int(i) for i in numbers]
sum = 0
for i in range(0, len(numbers)):
sum += numbers[i]
answer = 0
runs = 0
# 1st pass at 1k
while len(str(answer)) < sum:
answer = math.factorial(runs)
runs += 1000
# For some reason it always overshoots by 1k on high values
if sum > 20000:
runs -= 1000
# 2nd pass at 100
while len(str(answer)) > sum:
answer = math.factorial(runs)
runs -= 100
if runs < 0:
runs = 0
break
# 3rd pass at 10
while len(str(answer)) < sum:
answer = math.factorial(runs)
runs += 10
# Final pass
while True:
answer = math.factorial(runs)
if len(str(answer)) == sum:
numbersInAnswer = [0,0,0,0,0,0,0,0,0,0]
for value in str(answer):
numbersInAnswer[int(value)] += 1
if numbersInAnswer == numbers:
print(runs)
break
# Move the delta up and down to find the exact number
if len(str(answer)) > sum:
runs -= 1
else:
runs += 1
# Add randomness to fix getting stuck in "ditches"
randNumber = random.randint(-10, 10)
while runs + randNumber < 0:
randNumber = random.randint(-10, 10)
runs += randNumber
# Printing the time taken in seconds
# print(time.process_time())