// Arup Guha
// 1/21/2015
// Solution to 2005 MCPC Problem E: Time to Graduate

// Note: The data for this problem is extremely weak. My original solution that had no limit on number of classes in
//       a term and that had a critical bug in my best first search actually passed. (My bug was that I fixed the
//       distance to a subset of classes even if the edge building to it was length 2 instead of length 1.)
//       I have fixed everything that I thought was flawed and of course, the solution still passes the posted data.

import java.util.*;

public class e {

	public static int n;
	public static int max;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		n = stdin.nextInt();
		max = stdin.nextInt();
		// Go through all cases.
		while (n != -1) {

			// Store look up table of courses.
			HashMap<String,Integer> map = new HashMap<String,Integer>();
			for (int i=0; i<n; i++)
				map.put(stdin.next(), i);

			int[] info = new int[n];
			for (int i=0; i<n; i++) {

				// Get which class this row is about (annoying).
				int item = map.get(stdin.next());

				// Store term in mask - 1 = Fall, 2 = Spring, 3 = Both
				String term = stdin.next();
				int tMask = 1;
				if (term.equals("S")) tMask = 2;
				else if (term.equals("B")) tMask = 3;

				// Store prereqs as a mask.
				int prereqs = stdin.nextInt();
				int mask = 0;
				for (int j=0; j<prereqs; j++)
					mask = mask | (1 << (map.get(stdin.next())));

				// Store information about class item. (term+prereqs).
				info[item] = mask + (tMask << n);
			}

			// Set up BFS through courses.
			int[] distance = new int[1 << n];
			Arrays.fill(distance, -1);
			PriorityQueue<pair> q = new PriorityQueue<pair>();
			q.offer(new pair(0,0));

			// Run BFS - we know we'll always get out eventually...
			while (q.size() > 0) {

				// Get the next item. See if optimal. If not, go to next item. Otherwise, build off it.
				pair next = q.poll();
				if (distance[next.mask] == -1)
					distance[next.mask] = next.distance;
				else
					continue;

				// Enqueue neighbors.
				for (int i=next.mask+1; i<(1<<n); i++) {

					// Make sure we haven't been here before and that next is a subset of i,
					// and that we aren't taking too many classes in one term.
					if (distance[i] == -1 && (next.mask & i) == next.mask && Integer.bitCount(i) - Integer.bitCount(next.mask) <= max) {

						// See if we can get there in one term of classes (either next or two terms later)
						int terms = getTerms(next.mask, i, next.distance%2, info);
						if (terms != -1) q.offer(new pair(i, next.distance + terms));
					}
				}
			}

			// Output result.
			System.out.println("The minimum number of semesters required to graduate is "+distance[(1 <<n)-1]+".");

			// Get next case.
			n = stdin.nextInt();
			max = stdin.nextInt();
		}
	}

	// Returns 1 if we can get from curMask to nextMask in the next term, 2 if we can get there
	// by taking all necessary classes in the 2nd term after this one. -1 otherwise.
	public static int getTerms(int curMask, int nextMask, int term, int[] info) {

		int classes = nextMask - curMask;

		// Can't do in one term.
		if (Integer.bitCount(classes) > max) return -1;

		// Used to determine how many semesters we can do these classes.
		int semesters = 1;
		int cantdo = 0;

		// Can't do if each set of prereqs aren't met.
		for (int i=0; i<n; i++) {

			// A class we have to take
			if ((classes & (1 << i)) > 0) {
				int need = info[i] & ((1 << n)  - 1);
				if ((need & curMask) != need) return -1;
				if (((1 << (n+term)) & info[i]) == 0) cantdo++;
			}
		}

		// All classes can be taken next semester.
		if (cantdo == 0) return 1;

		// All classes must be taken 2 terms from now.
		if (cantdo == Integer.bitCount(classes)) return 2;

		// Can't do in a single term of classes.
		return -1;
	}
}

class pair implements Comparable<pair> {

	public int mask;
	public int distance;

	public pair(int m, int d) {
		mask = m;
		distance = d;
	}

	public int compareTo(pair other) {
		return this.distance - other.distance;
	}
}