// Arup Guha
// 9/17/2015
// Solution to 2015 January USACO Bronze Problem: Cow Routing (Ver A)

import java.util.*;
import java.io.*;

public class cowroute {

	final public static int NO_ROUTE = 1001;

	public static void main(String[] args) throws Exception {

		Scanner stdin = new Scanner(new File("cowroute.in"));

		// Get route particulars.
		int v1 = stdin.nextInt();
		int v2 = stdin.nextInt();
		int numRoutes = stdin.nextInt();
		int res = NO_ROUTE;

		// Go through each route.
		for (int i=0; i<numRoutes; i++) {

			// Get cost and route length.
			int cost = stdin.nextInt();
			int n = stdin.nextInt();
			boolean seenV1 = false;
			boolean seenV2 = false;

			// Mark if we see v1, followed by v2.
			for (int j=0; j<n; j++) {
				int loc = stdin.nextInt();
				if (loc == v1) seenV1 = true;
				if (loc == v2 && seenV1) seenV2 = true;
			}

			// Update if a valid route.
			if (seenV2) res = Math.min(res, cost);
		}

		PrintWriter out = new PrintWriter(new File("cowroute.out"));

		// Output result.
		if (res != NO_ROUTE) 	out.println(res);
		else					out.println(-1);
		out.close();
	}
}