HomeVolumesContestsSectionsForumsUsersPrintHelpAbout

Sections > Unsorted > problem:


Fleas

Section problems

• Variant 7
• Variant 8
• Variant 9
• А. Любовь «А»
• Автобус в Джал
• Sort People
• Баскетбольная команда
• Бириктирилген создук
• Fleas
• Бутун сандар массиви
• Babel tower
• Вирусы
• Выполнимость
• Гангстеры
• Деление длинного числа на короткое
• Divisors
• День рождения

Feedback

If you notice incorrect translations in Contester, please let author know.

Time limit 500/500/4000/4000 ms. Memory limit 65000/65000/65000/65000 Kb.

Problem description

There are Q (0 ≤ Q ≤ 10000) fleas on a rectangular field NxM (2 ≤ N, M ≤ 1000). And there is a dog sleeping somewhere on this field. Fleas are hungry like a horse and that's why they move on a field like chess knight jumping from one field to another (1 up 2 left, 1 down 2 left, 2 up 1 left, 2 down 1 left, 1 up 2 right, 1 down 2 right, 2 up 1 right, 2 down 1 right). Fleas can't start eating until all of them arrive to the place where dog sleeps because it won't be fair. They will never start eating, if at least one flea can't reach the dog. Calculate the minimum number of jumps that are required to reach dog for each flea and output their sum.

Input

Line 1: Five integers: N, M - fiels size; S, F - coordinates of dog; Q - number of fleas.

Line 2..Q+1: Coordinates of fleas. All fleas sit on different fields at the begining.

Output

Write minimum sum of jumps or -1 if there is a flea that can't reach the dog.

Example

stdin stdout

4 4 1 1 3
2 3
3 2
3 3

6

5 5 3 4 0 0
Для отправки решений необходимо выполнить вход.

www.contester.ru