Trucking Diesel

No attempts yetTime limit1sMemory limit128 MB

Problem

A diesel truck carries diesel from a start cell to a destination cell. The truck has no separate fuel tank: the cargo diesel is also the fuel. Find the maximum number of liters that can still be on the truck at the destination without running out along the way. If the destination cannot be reached with the starting fuel, print 1-1.

The terrain is an mm-by-nn grid of altitudes in meters. Each altitude is between 00 and 40004000 and is a multiple of 100100. The first row is the northern edge, the last row is the southern edge, the first column is the western edge, and the last column is the eastern edge. The truck starts at the north-west corner (0,0)(0, 0) and must reach the south-east corner (m1,n1)(m - 1, n - 1). In one step it may move to an adjacent cell to the east, south, or west. It cannot move north or diagonally.

Diesel weighs 1.01.0 kg per liter. A downhill or flat step costs 1010 liters. An uphill step costs 1010 liters plus 44 additional liters for every 100100 m of altitude gain and for every whole ton of total mass at the start of the step, where mass is rounded down to tons in 10001000 kg units. For example, if the mass at the start of a segment is 2601026010 kg and the altitude rises from 400400 m to 700700 m, the segment costs 10+3×4×26=32210 + 3 \times 4 \times 26 = 322 liters.

Before each step the truck may throw away an integer number of liters of diesel. The unladen truck weighs 88 tons (80008000 kg) and it starts with 2500025000 liters of diesel.

Input

The first line contains mm and nn. Each of the next mm lines contains nn altitudes. Every altitude is between 00 and 40004000 and is a multiple of 100100.

Output

Print the maximum number of liters that can be delivered. If the truck can arrive with an empty tank, print 00. If the destination cannot be reached, print 1-1.