// Arup Guha
// 6/22/2012
// Solution to 2012 World Finals Problem K: Stacking Plates
import java.util.*;

class pair {

	public int value;
	public int numsplits;

	public pair(int a, int b) {
		value = a;
		numsplits = b;
	}

	public String toString() {
		return "(" + value + ", "+ numsplits + ") ";
	}
}

class pairs {

	public ArrayList<pair> list;

	public pairs() {
		list = new ArrayList<pair>();
	}
}

class mystack {

	public ArrayList<Integer> list;

	// Assumes a is in sorted order already.
	public mystack(int[] a) {
		list = new ArrayList<Integer>();

		list.add(a[0]);
		int last = a[0];
		for (int i=1; i<a.length; i++) {
			if (a[i] != last) {
				list.add(a[i]);
				last = a[i];
			}
		}
	}

	// Splits this stack by value (if value isn't in the stack...)
	// and chops off the upper half and returns it as a new stack.
	// returns null if no split occurred.
	public mystack split(int value) {

		int i = 0;
		while (i < list.size()-1) {

			// Look for split point.
			if (list.get(i) < value && value < list.get(i+1)) {

				int[] temp = new int[list.size()-i-1];
				for (int j=0; j<temp.length; j++)
					temp[j] = list.get(i+1+j);

				while (list.size() > i+1)
					list.remove(list.size()-1);

				return new mystack(temp);
			}
			else if (list.get(i) >= value)
				break;
			i++;
		}

		return null;
	}

	// Returns true if two stacks can't be placed without one being split.
	public boolean intersect(mystack other) {

		int n = this.list.size();
		int n2 = other.list.size();

		if (this.list.get(n-1) <= other.list.get(0))
			return false;
		if (other.list.get(n2-1) <= this.list.get(0))
			return false;

		return true;
	}
}

public class k {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);

		// Go through all of the cases.
		int loop = 1;
		while (stdin.hasNext()) {

			int n = stdin.nextInt();

			ArrayList<mystack> allstacks = new ArrayList<mystack>();

			boolean[] allvals = new boolean[10001];
			Arrays.fill(allvals, false);

			for (int i=0; i<n; i++) {

				int len = stdin.nextInt();
				int[] temp = new int[len];

				for (int j=0; j<len; j++) {
					temp[j] = stdin.nextInt();
					allvals[temp[j]] = true;
				}

				allstacks.add(new mystack(temp));
			}

			// Split all obvious ones right now.
			int numsplits = 0;

			for (int i=0; i<allvals.length; i++) {

				if (allvals[i]) {

					for (int j=0; j<allstacks.size(); j++) {

						mystack other = allstacks.get(j).split(i);

						if (other != null) {
							allstacks.add(other);
							numsplits++;
						}

					}
				}
			}

			
			// Get rid of all single stacks and all "clear" ones.
			int numremoved = 0, i = 0;
			
			while (i<allstacks.size()) {
				
				// Now take care of clear items.
				boolean takeout = true;
				for (int j=0; j<allstacks.size(); j++) {

					if (j == i) continue;

					if (allstacks.get(i).intersect(allstacks.get(j))) {
						takeout = false;
						break;
					}
				}

				if (takeout) {
					numremoved++;
					allstacks.remove(i);
					continue;
				}

				i++;
			}
			

			int cursize = allstacks.size();

			// Here is where I have to solve the difficult problem with DP.
			int extrasplits = solve(allstacks);

			if (extrasplits == 0)
				System.out.println("Case "+loop+": "+(numsplits + numremoved - 1));
			else
				System.out.println("Case "+loop+": "+(numsplits + numremoved - 1 + cursize + 2*extrasplits));

			loop++;
		}
	}

	// Prints output.
	public static void print(ArrayList<mystack> s) {

		for (int i=0; i<s.size(); i++) {

			System.out.print("Stack "+i+": ");
			for (int j=0; j<s.get(i).list.size(); j++)
				System.out.print(s.get(i).list.get(j)+" ");
			System.out.println();
		}
	}

	public static int solve(ArrayList<mystack> allstacks) {

		// Base case.
		if (allstacks.size() == 0) return 0;

		boolean[] haveit = new boolean[10001];
		for (int i=0; i<allstacks.size(); i++)
			for (int j=0; j<allstacks.get(i).list.size(); j++)
				haveit[allstacks.get(i).list.get(j)] = true;

		// Stores how many stacks have plate i.
		int n = 0;
		for (int i=0; i<haveit.length; i++)
			if (haveit[i])
				n++;

		// Just stores non-zero values.
		int[] values = new int[n];
		int j = 0;
		for (int i=0; i<haveit.length; i++) {
			if (haveit[i]) {
				values[j] = i;
				j++;
			}
		}

		int[] freq = new int[n];
		int[] start = new int[n];
		int[] end = new int[n];
		Arrays.fill(freq, 0);
		Arrays.fill(start, 0);
		Arrays.fill(end, 0);

		int[] indexes = new int[allstacks.size()];
		Arrays.fill(indexes, 0);

		// Fill freq etc.
		for (int i=0; i<values.length; i++) {
			for (j=0; j<allstacks.size(); j++) {

				int tmp = allstacks.get(j).list.size();

				if (indexes[j] < tmp && allstacks.get(j).list.get(indexes[j]) == values[i]) {
					freq[i]++;
					if (indexes[j] == 0)
						start[i]++;
					if (indexes[j] == allstacks.get(j).list.size()-1)
						end[i]++;
					indexes[j]++;
				}
			}
		}

		pairs[] dp = new pairs[n];

		Arrays.fill(indexes,0);

		for (int i=0; i<dp.length; i++)
			dp[i] = new pairs();

		for (int i=0; i<values.length; i++) {

			int saveval = -1, savej = -1;
			for (j=0; j<allstacks.size(); j++) {

				int tmp = allstacks.get(j).list.size();

				if (indexes[j] < tmp && allstacks.get(j).list.get(indexes[j]) == values[i]) {

					// j is which list I am in...pick this as the one that gets "extended"

					int ans = 0;

					if (i == 0)
						ans = freq[i] - 1;

					else {

						// everyone but j must be start. so take dp in dp list except j.
						// if there is no one but j, copy dp list.
						int min = 10000;

						for (int k=0; k<dp[i-1].list.size(); k++) {

							if (dp[i-1].list.get(k).value == j) {
								saveval = dp[i-1].list.get(k).numsplits;
								savej = j;
								continue;
							}

							int val = dp[i-1].list.get(k).numsplits;
							if (val < min)
								min = val;
						}

						ans = min + freq[i] - end[i];
						if (indexes[j] < tmp-1)
							ans--;

					}

					if (ans >= 0 && ans < 9999)
						dp[i].list.add(new pair(j, ans));
					indexes[j]++;
				}
			}

			if (freq[i] == 1 && saveval >= 0) {
				dp[i].list.add(new pair(savej, saveval));
			}

		} // end i

		// Get the best looking back at the dp array.
		int min = 10000;
		for (int k=0; k<dp[n-2].list.size(); k++)
			if (dp[n-2].list.get(k).numsplits < min)
				min = dp[n-2].list.get(k).numsplits;

		return min;
	}
}