#P1270I. Xor on Figures

    ID: 3850 Type: RemoteJudge 5000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 10 Uploaded By: Tags>constructive algorithmsfftmath*3500

Xor on Figures

Description

There is given an integer kk and a grid 2k×2k2^k \times 2^k with some numbers written in its cells, cell (i,j)(i, j) initially contains number aija_{ij}. Grid is considered to be a torus, that is, the cell to the right of (i,2k)(i, 2^k) is (i,1)(i, 1), the cell below the (2k,i)(2^k, i) is (1,i)(1, i) There is also given a lattice figure FF, consisting of tt cells, where tt is odd. FF doesn't have to be connected.

We can perform the following operation: place FF at some position on the grid. (Only translations are allowed, rotations and reflections are prohibited). Now choose any nonnegative integer pp. After that, for each cell (i,j)(i, j), covered by FF, replace aija_{ij} by aijpa_{ij}\oplus p, where \oplus denotes the bitwise XOR operation.

More formally, let FF be given by cells (x1,y1),(x2,y2),,(xt,yt)(x_1, y_1), (x_2, y_2), \dots, (x_t, y_t). Then you can do the following operation: choose any x,yx, y with 1x,y2k1\le x, y \le 2^k, any nonnegative integer pp, and for every ii from 11 to nn replace number in the cell (((x+xi1)mod2k)+1,((y+yi1)mod2k)+1)(((x + x_i - 1)\bmod 2^k) + 1, ((y + y_i - 1)\bmod 2^k) + 1) with a((x+xi1)mod2k)+1,((y+yi1)mod2k)+1pa_{((x + x_i - 1)\bmod 2^k) + 1, ((y + y_i - 1)\bmod 2^k) + 1}\oplus p.

Our goal is to make all the numbers equal to 00. 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 kk (1k91 \le k \le 9).

The ii-th of the next 2k2^k lines contains 2k2^k integers ai1,ai2,,ai2ka_{i1}, a_{i2}, \dots, a_{i2^k} (0aij<2600 \le a_{ij} < 2^{60}) — initial values in the ii-th row of the grid.

The next line contains a single integer tt (1tmin(99,4k)1\le t \le \min(99, 4^k), tt is odd) — number of cells of figure.

ii-th of next tt lines contains two integers xix_i and yiy_i (1xi,yi2k1 \le x_i, y_i \le 2^k), describing the position of the ii-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 00 with these operations, output 1-1.

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 00, it is possible to do so in less than 101810^{18} operations.

Input

The first line contains a single integer kk (1k91 \le k \le 9).

The ii-th of the next 2k2^k lines contains 2k2^k integers ai1,ai2,,ai2ka_{i1}, a_{i2}, \dots, a_{i2^k} (0aij<2600 \le a_{ij} < 2^{60}) — initial values in the ii-th row of the grid.

The next line contains a single integer tt (1tmin(99,4k)1\le t \le \min(99, 4^k), tt is odd) — number of cells of figure.

ii-th of next tt lines contains two integers xix_i and yiy_i (1xi,yi2k1 \le x_i, y_i \le 2^k), describing the position of the ii-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 00 with these operations, output 1-1.

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 00, it is possible to do so in less than 101810^{18} operations.

Sample Input 1

2
5 5 5 5
2 6 2 3
0 0 2 0
0 0 0 0
5
1 1
1 2
1 3
1 4
2 4

Sample Output 1

3

Note

The figure and the operations for the example are shown above: