package maratonacps2020;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
 * @author hdblo
 */
public class Anagrama {

    public static void main(String[] args) throws FileNotFoundException, IOException {

        PrintWriter saida = new PrintWriter("anagrama.sol");

        String linha = "";

        BufferedReader entrada;

        entrada = new BufferedReader(new InputStreamReader(System.in));

        String temp[];

        while ((linha = entrada.readLine()) != null) {

            if (linha.equals("0")) {
                break;
            }

            temp = linha.split(" ");
            int n = 0;

            for (int x = 1; x < temp.length; x++) {
                if (Comparar(temp[x], temp[0])) {
                    n++;
                }
            }

            System.out.println(n);
            saida.println(n);
        }

        entrada.close();
        saida.close();
    }

    static boolean Comparar(String dig, String pal) {
        if (dig.length() > pal.length()) {
            return false;
        }
        int cont = 0;
        String temp[] = pal.split("");
        for (int x = 0; x < dig.length(); x++) {
            for (int i = 0; i < temp.length; i++) {
                if (dig.charAt(x) == temp[i].charAt(0)) {
                    cont++;
                    temp[i] = "#";
                    break;
                }
            }
        }
        return cont == dig.length();
    }
}
