In the event of technical difficulties with Szkopuł, please contact us via email at [email protected].

If you would like to talk about tasks, solutions or technical problems, please visit our Discord servers. They are moderated by the community, but members of the support team are also active there.

We are given containers, where . At the beginning all of them are full of water. The liter capacity of the -th container is a natural number satisfying inequalities .

Three kinds of moves can be made:

- Pouring the whole content of one container into another. This move can be made unless there is too little room in the second container.
- Filling up one container with part of the water from another one.
- Pouring away the whole content of one container into a drain.

Write a program that:

- reads form the standard input the number of containers , the capacity of each container and the requested final amount of water in each container,
- verifies, whether there exist a series of moves which leads to the requested final situation, and if there is one, the program computes the minimal number of moves leading to the requested situation,
- writes the result into the standard output.
The result should be the minimal number of moves leading to the requested final situation,
or one word
`NIE`(“no” in Polish) if there is no such a sequence of moves.

In the first line of the standard input one positive integer is written (), this is the number of containers. There are positive integers written in the second line. These are the capacities of the containers (the -th integer denotes the capacity if the -th container, ). In the third line of the input file there are written numbers. These are the requested final volumes of water in the containers (the -th integer denotes the requested final volume of water in the -th container, ). All integers in the second and the third line are separated by single spaces.

If it is not possible to result in requested final situation making only allowed moves,
your program should write only one word `NIE` to the standard output,
otherwise only one integer equal to the minimal number of moves which lead to the requested final situation should be written.

For the input data:

3 3 5 5 0 0 4

the correct result is:

6

whereas for the input data:

2 20 25 10 16

the correct output is:

NIE

*Task author: Piotr Chrzastowski-Wachtel.*