Exponent Exchange

아직 제출이 없습니다시간 제한1초메모리 제한1024 MB

문제

Alice and Bob are playing a cooperative game. They hold bpb^p dollars between them, for given integers bb and pp. Alice initially holds xx dollars, and Bob holds bpxb^p - x dollars. Alice and Bob want to consolidate their money, so one person holds all the money.

In each transaction, one player can choose to send the other player byb^y dollars, for some integer yy with 0y<p0 \le y < p. But each player wants to initiate as few transactions as possible. They are willing to cooperate such that the player that initiates the most transactions (the busiest player), initiates as few as possible.

Alice and Bob want to know the fewest number of transactions that the busiest player needs to initiate to complete the transfer.

입력

The first line of input contains two integers bb (2b1002 \le b \le 100) and pp (2p10002\le p\le 1000), where bb is the base, and pp is the number of digits.

The next line contains pp integers x_p1,x_p2,,x_0x\_{p-1}, x\_{p-2}, \ldots, x\_0, separated by spaces, with 0x_i\<b0\le x\_i\<b and 0\<x_p10\<x\_{p-1}. These are the base-bb digits of the value of xx, with the most significant digit first. Specifically, x=_0i\<pbix_ix=\sum\_{0\le i\<p} b^i x\_i. Note that they are given in order from the highest power to the lowest. For example, in the sample, 4 2 7 8 6 with b=10b=10 represents the base 1010 number 42,78642\\,786.

출력

Output a single integer, which is the minimum number of transactions the busiest player must initiate to transfer all the money to either Alice or Bob.