cho.sh
Notes
Loading...

Finding a Momentum Sequence

Time limit

1s

Memory limit

1024 MB

Problem

Call a sequence A={a1,⋯ ,an}A=\{a_1, \cdots, a_n\}A={a1​,⋯,an​} of length nnn a momentum sequence with momentum fAf_AfA​ if it satisfies all of the following conditions.

  • n≥3n \ge 3n≥3.
  • For every integer iii with 1≤i≤n1 \le i \le n1≤i≤n, aia_iai​ is an integer and 1≤ai<1091 \le a_i < 10^91≤ai​<109.
  • There exists a positive integer ddd such that ai+1=ai+da_{i+1}=a_i+dai+1​=ai​+d for every 1≤i≤n−21 \le i \le n-21≤i≤n−2. In other words, after removing the last term ana_nan​, the sequence {a1,⋯ ,an−1}\{a_1, \cdots, a_{n-1}\}{a1​,⋯,an−1​} is an arithmetic progression with positive integer common difference.
  • There exists an integer fA≥2f_A \ge 2fA​≥2 such that an=an−1⋅fAa_n=a_{n-1}\cdot f_Aan​=an−1​⋅fA​.

As an illustration, A={2,3,4,8}A=\{2, 3, 4, 8\}A={2,3,4,8} is a momentum sequence because it satisfies all conditions with d=1d=1d=1 and fA=2f_A=2fA​=2.

A momentum sequence {2,3,4,8}\{2, 3, 4, 8\}{2,3,4,8} gaining momentum

Given a string SSS consisting only of digits, determine whether there exists a momentum sequence A={a1,⋯ ,an}A=\{a_1, \cdots, a_n\}A={a1​,⋯,an​} such that SSS is exactly the concatenation of a1,⋯ ,ana_1, \cdots, a_na1​,⋯,an​ with no spaces.

When writing any aia_iai​ as a string, leading zeros are not allowed. If more than one valid sequence exists, find one with the smallest possible fAf_AfA​.

Input

The first line contains the string SSS. The length of SSS is at least 333 and at most 2 3482\,3482348, and every character of SSS is one of 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.

The first character of SSS is never 0.

Output

If there exists a momentum sequence AAA satisfying the conditions for SSS, print fAf_AfA​ as an integer. If more than one such AAA exists, print the smallest possible value of fAf_AfA​.

If no such momentum sequence exists, print 0.