#K78897. Minimum Energy Frequencies for Planetary Travel

    ID: 35188 Type: Default 1000ms 256MiB

Minimum Energy Frequencies for Planetary Travel

Minimum Energy Frequencies for Planetary Travel

In the universe of Taco, interplanetary travel requires energy frequencies. The planets are arranged in a perfect square grid, where M is the total number of planets. The traveler starts at the top-left planet (planet 1) and must reach the bottom-right planet (planet M) by moving only to the right or downward.

The minimum number of energy frequencies required is equal to the number of moves along the shortest path. If we denote the side length of the grid as \(n = \sqrt{M}\), then the number of right moves is \(n-1\) and the number of down moves is \(n-1\). Therefore, the answer is given by the formula:

\(\text{Answer} = 2n - 2 = 2\sqrt{M} - 2\)

If the provided number of planets does not form a perfect square grid, the input is considered invalid.

inputFormat

The input is read from stdin and consists of three lines:

  1. The first line contains an integer M, representing the total number of planets.
  2. The second line contains an integer N, representing the number of unique energy frequencies.
  3. The third line contains N space-separated integers representing the energy frequencies.

Note: The list of energy frequencies is provided but does not affect the result.

outputFormat

If M forms a perfect square grid, output a single integer on stdout representing the minimum number of energy frequencies required, computed as \(2\sqrt{M} - 2\). If M is not a perfect square, output the string Invalid.

## sample
4
3
1 2 3
2

</p>