11323 - Tile Sticking: Nightmare Version   

Description

Please refer to the 11251 - Tile Sticking problem for detail.  In short, you have infinite rectangle tiles of given sizes, and there is an empty area to be filled.  You are asked to compute the number of all possible outcomes.  But this problem considers the reflection(反射) and rotation(旋轉).

Example

There are two kinds of tiles: AA(1x2) and B(1x1)

The area to be filled: 2x2

There are 3 possible outcomes:

AA
AA
,

AA
BB
,

BB
BB
.

Input

The first line provides an integer, N, which is larger than or equal to 1 and less than 8, indicating the side of the square area that will be filled.

The second line provides an integer, K,  that is larger than 1 and less than 5, indicating the amount of types of tiles.

There will be two integers in each of the following K lines, indicating the dimensions of tiles.  All integers in this part are larger than 0 and less than 5.

Output

The number of all the possible outcome that can be made.

Note that reflection (up-down, left-right, diagnal) and rotation (90 degree, 180 degree, 270 degree) IS considered.  That's why the output of the example is 3 instead of 7.

(7? 2x two As, 4x A and two Bs, and 1x four Bs)

Sample Input  Download

Sample Output  Download

Tags




Discuss