#P1270I. Xor on Figures
Xor on Figures
Description
There is given an integer and a grid with some numbers written in its cells, cell initially contains number . Grid is considered to be a torus, that is, the cell to the right of is , the cell below the is There is also given a lattice figure , consisting of cells, where is odd. doesn't have to be connected.
We can perform the following operation: place at some position on the grid. (Only translations are allowed, rotations and reflections are prohibited). Now choose any nonnegative integer . After that, for each cell , covered by , replace by , where denotes the bitwise XOR operation.
More formally, let be given by cells . Then you can do the following operation: choose any with , any nonnegative integer , and for every from to replace number in the cell with .
Our goal is to make all the numbers equal to . Can we achieve it? If we can, find the smallest number of operations in which it is possible to do this.
The first line contains a single integer ().
The -th of the next lines contains integers () — initial values in the -th row of the grid.
The next line contains a single integer (, is odd) — number of cells of figure.
-th of next lines contains two integers and (), describing the position of the -th cell of the figure.
It is guaranteed that all cells are different, but it is not guaranteed that the figure is connected.
If it is impossible to make all numbers in the grid equal to with these operations, output .
Otherwise, output a single integer — the minimal number of operations needed to do this. It can be shown that if it is possible to make all numbers equal , it is possible to do so in less than operations.
Input
The first line contains a single integer ().
The -th of the next lines contains integers () — initial values in the -th row of the grid.
The next line contains a single integer (, is odd) — number of cells of figure.
-th of next lines contains two integers and (), describing the position of the -th cell of the figure.
It is guaranteed that all cells are different, but it is not guaranteed that the figure is connected.
Output
If it is impossible to make all numbers in the grid equal to with these operations, output .
Otherwise, output a single integer — the minimal number of operations needed to do this. It can be shown that if it is possible to make all numbers equal , it is possible to do so in less than operations.
Note
The figure and the operations for the example are shown above:
