#D9147. Twin Trees Bros.

    ID: 7602 Type: Default 3000ms 268MiB

Twin Trees Bros.

Twin Trees Bros.

Twin Trees Bros.

To meet the demand of ICPC (International Cacao Plantation Consortium), you have to check whether two given trees are twins or not.

Example of two trees in the three-dimensional space.

The term tree in the graph theory means a connected graph where the number of edges is one less than the number of nodes. ICPC, in addition, gives three-dimensional grid points as the locations of the tree nodes. Their definition of two trees being twins is that, there exists a geometric transformation function which gives a one-to-one mapping of all the nodes of one tree to the nodes of the other such that for each edge of one tree, there exists an edge in the other tree connecting the corresponding nodes. The geometric transformation should be a combination of the following transformations:

  • translations, in which coordinate values are added with some constants,
  • uniform scaling with positive scale factors, in which all three coordinate values are multiplied by the same positive constant, and
  • rotations of any amounts around either xx-, yy-, and zz-axes.

Note that two trees can be twins in more than one way, that is, with different correspondences of nodes.

Write a program that decides whether two trees are twins or not and outputs the number of different node correspondences.

Hereinafter, transformations will be described in the right-handed xyzxyz-coordinate system.

Trees in the sample inputs 1 through 4 are shown in the following figures. The numbers in the figures are the node numbers defined below.

For the sample input 1, each node of the red tree is mapped to the corresponding node of the blue tree by the transformation that translates (3,0,0)(-3, 0, 0), rotates π/2-\pi / 2 around the zz-axis, rotates π/4\pi / 4 around the xx-axis, and finally scales by 2\sqrt{2}. By this mapping, nodes #1, #2, and #3 of the red tree at (0,0,0)(0, 0, 0), (1,0,0)(1, 0, 0), and (3,0,0)(3, 0, 0) correspond to nodes #6, #5, and #4 of the blue tree at (0,3,3)(0, 3, 3), (0,2,2)(0, 2, 2), and (0,0,0)(0, 0, 0), respectively. This is the only possible correspondence of the twin trees.

For the sample input 2, red nodes #1, #2, #3, and #4 can be mapped to blue nodes #6, #5, #7, and #8. Another node correspondence exists that maps nodes #1, #2, #3, and #4 to #6, #5, #8, and #7.

For the sample input 3, the two trees are not twins. There exist transformations that map nodes of one tree to distinct nodes of the other, but the edge connections do not agree.

For the sample input 4, there is no transformation that maps nodes of one tree to those of the other.

Input

The input consists of a single test case of the following format.

nn x1x_1 y1y_1 z1z_1 . . . xnx_n yny_n znz_n u1u_1 v1v_1 . . . un1u_{n−1} vn1v_{n−1} xn+1x_{n+1} yn+1y_{n+1} zn+1z_{n+1} . . . x2nx_{2n} y2ny_{2n} z2nz_{2n} unu_n vnv_n . . . u2n2u_{2n−2} v2n2v_{2n−2}

The input describes two trees. The first line contains an integer nn representing the number of nodes of each tree (3n2003 \leq n \leq 200). Descriptions of two trees follow.

Description of a tree consists of nn lines that give the vertex positions and n1n - 1 lines that show the connection relation of the vertices.

Nodes are numbered 11 through nn for the first tree, and n+1n + 1 through 2n2n for the second tree.

The triplet (xi,yi,zi)(x_i, y_i, z_i) gives the coordinates of the node numbered ii. xix_i, yiy_i, and ziz_i are integers in the range between 1000-1000 and 10001000, inclusive. Nodes of a single tree have distinct coordinates.

The pair of integers (uj,vj)(u_j , v_j ) means that an edge exists between nodes numbered uju_j and vjv_j (ujvju_j \ne v_j). 1ujn1 \leq u_j \leq n and 1vjn1 \leq v_j \leq n hold for 1jn11 \leq j \leq n - 1, and n+1uj2nn + 1 \leq u_j \leq 2n and n+1vj2nn + 1 \leq v_j \leq 2n hold for nj2n2n \leq j \leq 2n - 2.

Output

Output the number of different node correspondences if two trees are twins. Output a zero, otherwise.

Sample Input 1

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

Sample Output 1

1

Sample Input 2

4 1 0 0 2 0 0 2 1 0 2 -1 0 1 2 2 3 2 4 0 1 1 0 0 0 0 2 0 0 0 2 5 6 5 7 5 8

Sample Output 2

2

Example

Input

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

Output

1

inputFormat

outputFormat

outputs the number of different node correspondences.

Hereinafter, transformations will be described in the right-handed xyzxyz-coordinate system.

Trees in the sample inputs 1 through 4 are shown in the following figures. The numbers in the figures are the node numbers defined below.

For the sample input 1, each node of the red tree is mapped to the corresponding node of the blue tree by the transformation that translates (3,0,0)(-3, 0, 0), rotates π/2-\pi / 2 around the zz-axis, rotates π/4\pi / 4 around the xx-axis, and finally scales by 2\sqrt{2}. By this mapping, nodes #1, #2, and #3 of the red tree at (0,0,0)(0, 0, 0), (1,0,0)(1, 0, 0), and (3,0,0)(3, 0, 0) correspond to nodes #6, #5, and #4 of the blue tree at (0,3,3)(0, 3, 3), (0,2,2)(0, 2, 2), and (0,0,0)(0, 0, 0), respectively. This is the only possible correspondence of the twin trees.

For the sample input 2, red nodes #1, #2, #3, and #4 can be mapped to blue nodes #6, #5, #7, and #8. Another node correspondence exists that maps nodes #1, #2, #3, and #4 to #6, #5, #8, and #7.

For the sample input 3, the two trees are not twins. There exist transformations that map nodes of one tree to distinct nodes of the other, but the edge connections do not agree.

For the sample input 4, there is no transformation that maps nodes of one tree to those of the other.

Input

The input consists of a single test case of the following format.

nn x1x_1 y1y_1 z1z_1 . . . xnx_n yny_n znz_n u1u_1 v1v_1 . . . un1u_{n−1} vn1v_{n−1} xn+1x_{n+1} yn+1y_{n+1} zn+1z_{n+1} . . . x2nx_{2n} y2ny_{2n} z2nz_{2n} unu_n vnv_n . . . u2n2u_{2n−2} v2n2v_{2n−2}

The input describes two trees. The first line contains an integer nn representing the number of nodes of each tree (3n2003 \leq n \leq 200). Descriptions of two trees follow.

Description of a tree consists of nn lines that give the vertex positions and n1n - 1 lines that show the connection relation of the vertices.

Nodes are numbered 11 through nn for the first tree, and n+1n + 1 through 2n2n for the second tree.

The triplet (xi,yi,zi)(x_i, y_i, z_i) gives the coordinates of the node numbered ii. xix_i, yiy_i, and ziz_i are integers in the range between 1000-1000 and 10001000, inclusive. Nodes of a single tree have distinct coordinates.

The pair of integers (uj,vj)(u_j , v_j ) means that an edge exists between nodes numbered uju_j and vjv_j (ujvju_j \ne v_j). 1ujn1 \leq u_j \leq n and 1vjn1 \leq v_j \leq n hold for 1jn11 \leq j \leq n - 1, and n+1uj2nn + 1 \leq u_j \leq 2n and n+1vj2nn + 1 \leq v_j \leq 2n hold for nj2n2n \leq j \leq 2n - 2.

Output

Output the number of different node correspondences if two trees are twins. Output a zero, otherwise.

Sample Input 1

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

Sample Output 1

1

Sample Input 2

4 1 0 0 2 0 0 2 1 0 2 -1 0 1 2 2 3 2 4 0 1 1 0 0 0 0 2 0 0 0 2 5 6 5 7 5 8

Sample Output 2

2

Example

Input

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

Output

1

样例

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