// Arup Guha
// 9/17/2015
// Solution to 2015 January USACO Bronze Problem: Cow Routing (Ver B)

import java.util.*;
import java.io.*;

public class cowroute2 {

	final public static int NO_RESULT = 10000;
	final public static int MAX_CITIES = 10001;

	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();

		// Just storing parallel arrays since I won't be sorting anything.
		int[][] routes = new int[numRoutes][];
		int[] costs = new int[numRoutes];
		int res = NO_RESULT;

		// Go through each route.
		for (int i=0; i<numRoutes; i++) {

			// Get all info for route i.
			costs[i] = stdin.nextInt();
			int n = stdin.nextInt();
			routes[i] = new int[n];
			for (int j=0; j<n; j++)
				routes[i][j] = stdin.nextInt();

			// Still get best answer for 1 path.
			if (solve(v1, v2, routes[i]))
				res = Math.min(res, costs[i]);
		}

		// index i stores best result to vertex i from v1.
		int[] bestFromV1 = new int[MAX_CITIES];
		Arrays.fill(bestFromV1, NO_RESULT);

		// Any time we see v1 in a route, update everything after it.
		for (int i=0; i<numRoutes; i++) {
			int j = 0;
			while (j < routes[i].length && routes[i][j] != v1) j++;
			j++;
			while (j < routes[i].length) {
				bestFromV1[routes[i][j]] = Math.min(bestFromV1[routes[i][j]], costs[i]);
				j++;
			}
		}

		// index i stores best cost from i to v2.
		int[] bestToV2 = new int[MAX_CITIES];
		Arrays.fill(bestToV2, NO_RESULT);

		// Any time we see v2 in a route, look before it and update costs from those places to v2.
		for (int i=0; i<numRoutes; i++) {

			// First see if v2 is here.
			int indexV2 = -1;
			for (int j=0; j<routes[i].length; j++) {
				if (routes[i][j] == v2) {
					indexV2 = j;
					break;
				}
			}

			// If so, this will reassign costs from other places to v2.
			for (int j=0; j<indexV2; j++)
				bestToV2[routes[i][j]] = Math.min(bestToV2[routes[i][j]], costs[i]);

		}

		// Now, update for paths of length 2, using any possible intermediate point.
		for (int i=0; i<MAX_CITIES; i++) {
			if (i == v1 || i == v2) continue;
			res = Math.min(res, bestFromV1[i]+bestToV2[i]);
		}

		PrintWriter out = new PrintWriter(new File("cowroute.out"));

		// Output result.
		if (res != NO_RESULT) 	out.println(res);
		else					out.println(-1);
		out.close();
	}

	public static boolean solve(int v1, int v2, int[] route) {

		boolean seenV1 = false;
		boolean seenV2 = false;

		// Mark if we see v1, followed by v2.
		for (int i=0; i<route.length; i++) {
			if (route[i] == v1) seenV1 = true;
			if (route[i] == v2 && seenV1) seenV2 = true;
		}

		// Return if this route has v1 followed by v2.
		return seenV2;
	}
}