Cat Tour
Time limit0.5sMemory limit2048 MB
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 parks, cats and 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 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 and (, ), the number of parks and cats.
The next lines each contain a pair , () 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 lines each contain a pair , () describing a street with a cat that allows passage from to . Several streets may connect the same pair of parks.
Output
Output the length of the shortest Cat Tour on one line, or if no Cat Tour exists.
Hint
The shortest Cat Tour in the first example is 3 → 5 → 3.