Tura Mačkica

시간 제한0.5초메모리 제한2048 MB

요약
고양이가 없는 연결 도로 그래프와 방향이 있는 고양이 도로가 주어질 때, 모든 고양이 도로를 한 번씩만 지나고 어떤 도로도 다시 쓰지 않는 가장 짧은 닫힌 경로의 길이를 구한다.
난이도

어려움10점 중 8점

유형
그래프, 유니온 파인드, DFS, 그리디
정답자
아직 제출이 없습니다

문제

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 in each street there is at most one cat. It patrols the street by viciously attacking everyone who travels in one direction of that street, but from the people who are travelling in the opposite direction it demands pets before it lets them through. The City of Zagreb, aware of this circumstance, has made sure that the citizens can reach any park from any other park using only nn streets without cats.

The Tourist Center has decided to open a so called Cat Tour in Zagreb. The tours visitors will be able to pet every cat in Zagreb and return to the starting location so they can do it all over again. To make sure the tourists don’t get lost the tourist center will put up signs in each street telling them which street they shoud take next, so the Cat Tour cannot travel the same street twice (not even in the opposite directions). Obviously, tourists expect to pet every single cat, that no cat attacks them and that the tour is as short as possible.

Help the tourist center by finding the length of the shortest possible Cat Tour or say that it is not possible.

입력

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

The following nn lines contain pairs aa, bb (1≤a,b≤n1 ≤ a, b ≤ n) which describe the streets without cats. Note that it is possible that a=ba = b or that two or more streets connect the same parks.

The following mm lines contain pairs xx, yy (1≤x,y≤n1 ≤ x, y ≤ n) which describe the streets where a cat allows passage from xx to yy. Note that it is possible that two or more streets connect the same parks.

출력

In the first and only line, output the length of the shortest possible Cat Tour or “-1” if no Cat Tours exist.

힌트

Clarification of the first example:

The shortest Cat Tour is 3→5→33 → 5 → 3.

예제3

  1. 예제 1

    입력
    5 1
    3 1
    3 2
    3 4
    3 5
    2 4
    3 5
    
    예상 출력
    2
    
  2. 예제 2

    입력
    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
    
    예상 출력
    10
    
  3. 예제 3

    입력
    7 3
    4 1
    4 3
    6 4
    2 6
    5 2
    5 7
    4 4
    3 6
    4 1
    7 5
    
    예상 출력
    -1