Cow Routing

No attempts yetTime limit1sMemory limit256 MB

Problem

Bessie the cow is tired of the cold winter on her farm and plans to fly somewhere warmer for a vacation. Only one airline, Air Bovinia, is willing to sell tickets to cows, and its tickets have a slightly unusual structure.

Air Bovinia owns NN planes. Each plane flies a fixed "route" made of two or more cities. For example, one plane might fly a route that starts at city 1, then goes to city 5, then city 2, and finally city 8. No city appears twice on the same route. If Bessie decides to use a route, she can board at any city on that route and get off at any city that comes later on the same route. She does not have to board at the first city, and she does not have to get off at the last city. Each route has a fixed cost, and she pays that cost in full if she uses any part of the route, no matter how many cities she passes.

Bessie wants the cheapest way to get from her farm in city AA to her destination, city BB. She does not want a complicated itinerary, so she uses only one route. Find the minimum cost she has to pay.

Input

The first line contains AA, BB, and NN, separated by spaces. (1N5001 \le N \le 500, 1A,B100001 \le A, B \le 10000, ABA \ne B)

The next 2N2N lines describe the routes, two lines per route. The first line of a route contains the cost of using that route (an integer from 11 to 10001000) and the number of cities on the route (an integer from 11 to 500500). The second line lists the cities on that route in flight order. Each city is an integer from 11 to 1000010000.

Output

Print the minimum cost of a single route Bessie can use to travel from city AA to city BB. If there is no such route, print -1.

Note

Even when using two routes in sequence would be cheaper, Bessie may use only one route. Only a route on which AA appears before BB counts as a candidate.