#include <iostream>
#include <array>
#include <cmath>
#define PI 3.14159265358979323846
// Length of factorial
uint32_t StirlingAprox(uint32_t n)
{
return (uint32_t) std::floor( ((n+0.5) * std::log(n) - n + 0.5 * std::log(2*PI))/std::log(10) ) + 1;
}
int main()
{
std::array<uint32_t, 10> frequency = {};
uint32_t length = 0;
for(auto& i : frequency)
{
std::cin >> i;
length += i;
}
// Check special cases that share the same factorial length
std::array<uint32_t, 10> factorialSpecial = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0};
if(frequency == factorialSpecial)
{
std::cout << "2\n";
return 0;
}
factorialSpecial = {0, 0, 0, 0, 0, 0, 1, 0, 0, 0};
if(frequency == factorialSpecial)
{
std::cout << "3\n";
return 0;
}
factorialSpecial = {0, 0, 1, 0, 1, 0, 0, 0, 0, 0};
if(frequency == factorialSpecial)
{
std::cout << "4\n";
return 0;
}
factorialSpecial = {1, 1, 1, 0, 0, 0, 0, 0, 0, 0};
if(frequency == factorialSpecial)
{
std::cout << "5\n";
return 0;
}
factorialSpecial = {1, 0, 0, 0, 0, 1, 0, 7, 0, 0};
if(frequency == factorialSpecial)
{
std::cout << "6\n";
return 0;
}
factorialSpecial = {2, 0, 0, 0, 1, 1, 0, 0, 0, 0};
if(frequency == factorialSpecial)
{
std::cout << "7\n";
return 0;
}
// Use Stirling’s approximation to get a table of values during compile time and check if there is a hit
// Have to check until 10^5
std::array<uint32_t, 100000> lengths = {};
for(int i = 0; i < 100000; i++)
{
lengths[i] = StirlingAprox(i);
}
auto loc = std::find(lengths.begin(), lengths.end(), length);
if (loc != std::end(lengths))
{
std::cout << std::distance(lengths.begin(), loc) << "\n";
}
else
{
// Stirling’s approximation doesn't work anymore :C
std::cout << "-1\n";
}
}