Cthulhu decided to catch Scaygerboss. Scaygerboss found it out and is trying to hide in a pack of his scaygers. Each scayger except Scaygerboss is either a male or a female. Scaygerboss's gender is "other".
Scaygers are scattered on a two-dimensional map divided into cells. A scayger looks nerdy and loveable if it is staying in the same cell with exactly one scayger of a gender that is different from its own gender. Cthulhu will not be able to catch Scaygerboss if all the scaygers on the map look nerdy and loveable.
The scaygers can move around at different speeds. For each scayger, we are given the time it takes this scayger to move from a cell to an adjacent cell. Cells are adjacent if they share a common side. At any point of time, each cell that does not contain an obstacle can be occupied by an arbitrary number of scaygers. Scaygers cannot move to cells with obstacles.
Calculate minimal time in order to make all scaygers look nerdy and loveable if they move optimally toward this goal.
Output
Output the minimum possible time it takes to make all scaygers look nerdy and loveable or -1 if it is impossible.
Note
Consider the first sample test. The scaygers are hiding on a 4 by 4 map. Scaygerboss initially resides in the cell
(2,1) and can move between cells in 1 unit of time. There are also 2 male and 3 female scaygers on the map. One of the females initially is in the cell
(1,1), and all the other scaygers are in the cell
(2,1). All the scaygers move between cells in 2 units of time. If Scaygerboss and the female scayger from the cell
(1,1) move to the cell
(1,2), and a male and a female scayger from those residing in the cell
(2,1) move to the cell
(1,1), then all the scaygers will look nerdy and lovable in 2 units of time.