beecrowd | 3030

# Jumping Transformers

By Microsoft Serbia

Timelimit: 4

You, the mighty Blackout, are standing in the upper-left (0, 0) corner of NxM matrix. You must move either right or down each second.

There are K transformers jumping around the matrix in the following way. Each transformer starts jumping from position (x, y), at time t, and jumps to the next position each second. The x-axes grows downwards, and y-axes grows to the right. The order of jumping positions is defined as (x, y), (x + d, yd), (x + d, y), (x, y + d), and is periodic. Before time t transformer is not in the matrix.

You want to arrive to the bottom-right corner (N − 1, M − 1), while slaying transformers and losing the least possible amount of energy. When you meet the transformer (or more of them) in the matrix field, you must kill them all, and you lose the sum of the energy amounts required to kill each transformer.

After the transformer is killed, he of course stops jumping, falls into the abyss and leaves the matrix world. Output minimum possible amount of energy wasted.

## Input

In the first line, integers N ,M (1 ≤ N, M ≤ 500), representing size of the matrix, and K (0 ≤ K ≤ 5 . 105 ), the number of jumping transformers.

In next K lines, for each transformer, numbers x, y, d (d ≥ 1), t (0 ≤ tN +M −2), and e (0 ≤ e ≤ 109 ), representing starting coordinates of transformer, jumping positions distance in pattern described above, time when transformer starts jumping, and energy required to kill it.

It is guaranteed that all 4 of jumping points of the transformers are within matrix coordinates

## Output

Print single integer, the minimum possible amount of energy wasted, for Blackout to arrive at bottom-right corner.

 Input Sample Output Sample 3 3 5 0 1 1 0 7 1 1 1 0 7 1 1 1 0 10 1 1 1 2 2 0 1 1 2 3 9