#C8880. Minimum Instructions to Clean Grid

    ID: 52911 Type: Default 1000ms 256MiB

Minimum Instructions to Clean Grid

Minimum Instructions to Clean Grid

You are given a grid with m rows and n columns. A vacuum cleaner starts at the top-left cell (1, 1) and must finish at the bottom-right cell (m, n) while cleaning each cell exactly once. The vacuum cleaner can move to adjacent cells (up, down, left, right) but cannot revisit a cleaned cell.

The task is to determine the minimum number of instructions (moves) the vacuum cleaner must execute to clean the entire grid and reach the destination, or decide that it is impossible.

In the general case, if a valid path exists, the minimum number of moves is given by the formula: $$m \times n - 1$$, provided that both axes have at least 3 cells. For grids that do not meet these conditions, it may be impossible to find such a path.

inputFormat

The input consists of a single line containing two space-separated integers m and n where:

  • m is the number of rows,
  • n is the number of columns.

Constraints: m and n are positive integers.

outputFormat

Output a single integer representing the minimum number of instructions required to clean the grid and reach the bottom-right cell, or output -1 if it is impossible.

## sample
1 1
0