Under construction

Still being built. Things may change

Cat Tour

Time limit0.5sMemory limit2048 MB

Summary
Find the shortest closed walk that crosses every one-way cat street in its direction and never reuses a street, or report -1.
Level

Hard8 of 10

Topics
Graph, Shortest path
Solved
No attempts yet

Problem

Everyone knows that Zagreb has nn parks, mm cats and n+mn + m streets which connect the parks. The cats are very territorial animals, so each street has at most one cat. A cat patrols its street by viciously attacking everyone who travels in one direction of that street, but it demands pets from people travelling in the opposite direction before letting them through. The City of Zagreb, aware of this, has made sure that citizens can reach any park from any other park using only nn streets without cats.

The Tourist Center has decided to open a Cat Tour in Zagreb. A tour starts at a park, pets every cat in Zagreb, and returns to the starting park, so tourists can repeat it. To keep tourists from getting lost, the Tourist Center puts up a sign in each street telling tourists which street to take next. Therefore a tour cannot use the same street twice, not even in opposite directions. Tourists expect to pet every cat, expect no cat to attack them, and want the tour to be as short as possible.

Help the Tourist Center by finding the length of the shortest possible Cat Tour, or report that no tour is possible.

Input

The first line contains integers nn and mm (1≤n≤2⋅1041 \le n \le 2 \cdot 10^4, 0≤m≤2⋅1040 \le m \le 2 \cdot 10^4), the number of parks and cats.

The next nn lines each contain a pair aa, bb (1≤a,b≤n1 \le a, b \le n) describing a street without a cat. A street may connect a park to itself, and several streets may connect the same pair of parks.

The next mm lines each contain a pair xx, yy (1≤x,y≤n1 \le x, y \le n) describing a street with a cat that allows passage from xx to yy. Several streets may connect the same pair of parks.

Output

Output the length of the shortest Cat Tour on one line, or −1-1 if no Cat Tour exists.

Hint

The shortest Cat Tour in the first example is 3 → 5 → 3.

Examples3

  1. Example 1

    Input
    5 1
    3 1
    3 2
    3 4
    3 5
    2 4
    3 5
    
    Expected output
    2
    
  2. Example 2

    Input
    6 7
    3 2
    5 3
    1 4
    6 1
    5 6
    4 2
    4 5
    1 2
    4 2
    2 6
    3 1
    1 6
    6 4
    
    Expected output
    10
    
  3. Example 3

    Input
    7 3
    4 1
    4 3
    6 4
    2 6
    5 2
    5 7
    4 4
    3 6
    4 1
    7 5
    
    Expected output
    -1