Merging trees

Given a left-handed and a right-handed ternary tree, find the minimum number of vertices in a ternary tree that is a superposition of both.

Medium7TreeDynamic programmingRecursionNo attempts yetTime limit1sMemory limit512 MB

Problem

In computing, trees are drawn upside down: the root sits at the top and the leaves sit at the bottom. A tree is a data structure of NN vertices connected by N1N-1 edges so that you can walk from any vertex to any other vertex along the edges. In a rooted tree, every edge joins a parent vertex to a child vertex. Exactly one vertex has no parent, and that vertex is the root. Starting at the root and following edges from parent to child, you reach every other vertex of the tree.

In a ternary tree, each vertex takes up to three children, called left, center, and right. A left-handed ternary tree is a rooted ternary tree in which no vertex has a right child. A right-handed ternary tree is a rooted ternary tree in which no vertex has a left child. The root of a ternary tree is always a center vertex. The figure below shows a left-handed tree and a right-handed tree.

A superposition SS of a left-handed tree CC and a right-handed tree DD is a ternary tree that meets two conditions. First, the root of SS is the root of CC, or the root of DD, or the two roots laid on top of each other. Second, SS contains the structure of both trees. The figure below shows a few trees built by superposing the left-handed tree and the right-handed tree above.

In figure (a) the root is vertex xx of the right-handed tree, and the vertex pairs (a,y)(a, y) and (c,u)(c, u) are superposed. In figure (b) the root is vertex aa of the left-handed tree, and the pairs (d,x)(d, x), (e,y)(e, y), (f,u)(f, u) are superposed. In figure (c) the root is again vertex aa of the left-handed tree, and the pair (f,x)(f, x) is superposed.

You are given a left-handed tree and a right-handed tree. Find the smallest number of vertices a ternary tree needs in order to be a superposition of the two given trees.

Input

The first line contains an integer NN, the number of vertices of the left-handed tree. Vertices of this tree are numbered from 11 to NN, and vertex 11 is the root. Each of the next NN lines contains three integers II, LL, KK, meaning that the left child of vertex II is LL and its center child is KK.

The next line contains an integer MM, the number of vertices of the right-handed tree. Vertices of this tree are numbered from 11 to MM, and vertex 11 is the root. Each of the next MM lines contains three integers PP, QQ, RR, meaning that the center child of vertex PP is QQ and its right child is RR.

The value 00 means that the child in that slot does not exist. The lines describing vertices do not arrive in numerical order.

Constraints

  • 1N1041 \le N \le 10^4
  • 0I,L,KN0 \le I, L, K \le N
  • 1M1041 \le M \le 10^4
  • 0P,Q,RM0 \le P, Q, R \le M

Output

Print, on one line, the smallest number of vertices of a tree that is a superposition of the two trees given in the input.