Under construction

Still being built. Things may change

Exponent Exchange

Time limit1sMemory limit1024 MB

Summary
Alice and Bob must gather all b^p dollars with transfers of powers of b, and the task asks for the fewest transactions the busiest player needs.
Level

Medium7 of 10

Topics
Math, Greedy, Binary search
Solved
No attempts yet

Problem

Alice and Bob are playing a cooperative game. Together they hold bpb^p dollars, where bb and pp are given integers. Alice initially holds xx dollars, and Bob holds bp−xb^p - x dollars. Alice and Bob want to consolidate their money so that one person holds all of it.

In each transaction, one player can send the other player byb^y dollars, for some integer yy with 0≤y<p0 \le y < p. Each player wants to initiate as few transactions as possible. They cooperate so that the player who 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 must initiate to transfer all the money.

Input

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

The next line contains pp integers xp−1,xp−2,…,x0x_{p-1}, x_{p-2}, \ldots, x_0, separated by spaces, with 0≤xi<b0 \le x_i < b and 0<xp−10 < x_{p-1}. These are the base-bb digits of xx, given from the highest power to the lowest. That is, x=∑0≤i<pbixix = \sum_{0 \le i < p} b^i x_i. For example, with b=10b=10, the digits 4 2 7 8 6 represent the number 42786.

Output

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

Examples1

  1. Example 1

    Input
    10 5
    4 2 7 8 6
    
    Expected output
    7