An

sudoku puzzle is a grid consisting of

regions, and each region contains

cells. Hence an

sudoku puzzle contains

cells. Every integer from

to

occurs exactly once in each row, each column, and each region of an

sudoku puzzle.
Listing the integers in a row or a column starting from some direction as a sequence of length

,

is the first integer of the sequence, and X-sum is the sum of the first

integers of the sequence.
The above figure is a

sudoku puzzle with X-sums. The

-th row listed from right to left is

and the first integer

is

, so the X-sum of the

-th row from the direction right is

.
Given two positive integers

and

, a direction

, and an index

, you need to find the X-sum of the

-th row or

-th column from the direction

in
the lexicographically smallest 
sudoku.
Denoting

as the

-th row and the

-th column of a sudoku puzzle

, a sudoku puzzle

is lexicographically smaller than a sudoku puzzle

of the same size if there exists

and

satisfying that

, that

for all

, and that

for all

and

. You can find that the above is the lexicographically smallest

sudoku puzzle.