Problem 2562. Juggling

A siteswap pattern consist of a list of nonnegative integers being instructions for a juggler.

While juggling, he throws up maximum one ball at the time at given by the pattern height (and then catches it after given periods). 0 means "missing", 1 is passing ball between hands, 2 is keeping ball for a moment (no toss), 3 is cascade toss, etc. (see Wikipedia)

After one full cycle pattern is looped and can be performed continuously. This allows simple patterns to be very short, i.e. classical pattern for three balls noted as 333333... is written by only one 3.

For example pattern 51 uses three balls like that:

and pattern 3 looks like this:

In this problem we will consider such defined patterns. Catching and throwing up to one ball at a time is allowed. Given a vector of integers, determine if it is a valid pattern and return number of balls required (it's an average of pattern) or NaN instead.