408516: GYM103176 I Ice-cream Sampler
Description
[right] [/right]
Baskin Robbins is a ice-cream parlour brand. Most of the stores offer 31 different flavours, so that customers can try a different flavour every day in a month (note the 31 in the logo). However, the Baskin Robbins Brown in Cheongdam (Seoul, South Korea) offers 100 flavours.
Since there are too many flavours, they sell a ice-cream sampler which let customers try up to 10 flavours.
The flavours, numbered 1 to 100, are located around the counter. After taking your order at the Order point (orange cell), the staff have to scoop the ice-cream by walking to where the flavours are, one after another in any order. When all 10 scoops of ice cream are ready, the sampler is delivered to the Pick Up point (green cell). For some reason, the staff can only walk vertically and horizontally. Note that if two or more chosen flavours are located in the same cell, the staff can scoop all of them without moving. It is allowed to visit a point, including the Order and Pick Up point, more than once.
For example, if your ordered flavours 3, 13, 14, 16, 24, 39, 43, 46, 87, 99, one way that the staff can prepare your order is illustrated with the red arrows. The total walking distance is 37. In fact, this is one of the shortest paths. Given the 10 flavours chosen, can you help the staff minimize the walking distance?
InputThe input consists of 10 integers, between 1 and 100 inclusive, in a single line – the numbers of the 10 chosen flavours given in non-decreasing order. It's allowed for the customers to choose multiple scoops for the same flavour.
OutputOutput a single integer – the minimum walking distance needed to scoop all 10 flavours, starting from the Order point and ending at the Pick Up point.
ExamplesInput3 13 14 16 24 39 43 46 87 99Output
37Input
50 50 50 50 50 99 99 100 100 100Output
15Note
The first sample corresponds to the example in the problem statement.
In the second sample, the customer wants to have 5 scoops of flavour 50, 2 scoops of flavour 99 and 3 scoops of flavour 100.