Under construction

Still being built. Things may change

A Complex Problem

Time limit3sMemory limit1024 MB

Summary
Given subset and proper-subset relations between complexity classes, find the minimum and maximum number of distinct classes that can appear.
Level

Medium6 of 10

Topics
Graph, Union-find, Topological sort
Solved
No attempts yet

Problem

There are many problems in computer science, and some are harder than others. Computer scientists group problems into complexity classes and analyze how these classes relate to each other.

A complexity class is simply a set of problems. For example, the complexity class P is the set of decision problems that can be solved by an algorithm whose running time is a polynomial function of the input size. Every problem in P also belongs to NP, the set of decision problems whose "yes" instances can be verified in polynomial time. It is not known whether every problem in NP is also in P, so P and NP could be one class or two distinct classes.

The Halting Problem is in ALL, the set of all decision problems, but it is not in R, the set of decision problems solvable by a Turing machine. So ALL and R are two distinct complexity classes.

Given a series of relations between complexity classes, find the minimum and maximum number of distinct complexity classes.

Input

The first line contains two integers 0≤M,N≤1050 \leq M, N \leq 10^5 with M+N>0M + N > 0, separated by a space.

Each of the next MM lines contains two distinct complexity classes AiA_i and BiB_i. Each line means Ai⊆BiA_i \subseteq B_i, so every problem in AiA_i is also in BiB_i.

Each of the next NN lines contains two distinct complexity classes AjA_j and BjB_j. Each line means Aj⊊BjA_j \subsetneq B_j, so every problem in AjA_j is also in BjB_j, and at least one problem in BjB_j is not in AjA_j.

A complexity class is written with one to eight English letters, in uppercase or lowercase. There is at most one relation between any two distinct classes, and the relations do not contradict each other.

Output

Print two integers separated by a space on one line: the minimum and the maximum number of distinct complexity classes that appear in the given relations.

Examples4

  1. Example 1

    Input
    1 0
    P NP
    
    Expected output
    1 2
    
  2. Example 2

    Input
    0 1
    R ALL
    
    Expected output
    2 2
    
  3. Example 3

    Input
    3 0
    QMIP MIPstar
    MIPstar RE
    RE QMIP
    
    Expected output
    1 1
    
  4. Example 4

    Input
    11 8
    NC P
    P BPP
    P coNP
    P NP
    BPP BQP
    coNP PSPACE
    BQP PSPACE
    NP PSPACE
    PSPACE EXPTIME
    EXPTIME NEXPTIME
    NEXPTIME EXPSPACE
    REG CFL
    CFL NC
    CFL CSL
    NC PSPACE
    CSL PSPACE
    EXPSPACE R
    R RE
    RE ALL
    
    Expected output
    7 16