Under construction

Still being built. Things may change

Cutting into Monotone Increasing Sequence

Time limit3sMemory limit1024 MB

Summary
Insert the fewest commas into a big integer so each piece is at most b, has no leading zero, and the pieces never decrease from left to right.
Level

Medium6 of 10

Topics
Dynamic programming, String, Math
Solved
No attempts yet

Problem

A monotone sequence is a sequence of numbers that either consistently increases or consistently decreases as you move along it. In a monotone increasing sequence, each term is greater than or equal to the term before it. For a sequence a1,…,ana_1, \dots, a_n, it is monotone increasing if and only if ai≤ai+1a_i \le a_{i+1} for every 1≤i<n1 \le i < n. For example, the sequence 1,2,2,4,51,2,2,4,5 is monotone increasing.

Monotone sequences appear in calculus and analysis. They make it easier to study how a function behaves over a range of values.

One of our problem setters likes big integers. The Taiwan Online Programming Contest has often included problems about them. This problem combines big integers with monotone increasing sequences. Given a big integer xx, insert commas between its digits so that the result is a monotone increasing sequence, subject to these constraints.

  • The last term is no more than bb.
  • Commas cannot be inserted before a zero.
  • The number of commas is minimized.

Let xx have kk digits, written x=d1d2⋯dkx = d_1 d_2 \cdots d_k. For example, with x=654321x=654321 and b=1000b=1000, inserting commas after d3d_3 and d5d_5 gives 6,54,3216,54,321.

Compute the minimum number of commas needed to turn xx into a monotone increasing sequence whose terms are all no more than bb. If it is impossible, print NO WAY.

Input

The input contains two non-negative integers xx and bb.

Output

Print the minimum number of commas needed to turn xx into a monotone increasing sequence whose terms are all no more than bb. If there is no way to do so, print NO WAY.

Constraints

  • x≤10100000x \le 10^{100000}
  • b<264b < 2^{64}
  • If x>0x > 0, xx has no leading zero.

Examples2

  1. Example 1

    Input
    654321 1000
    
    Expected output
    2
    
  2. Example 2

    Input
    654321 100
    
    Expected output
    NO WAY