#include <stdio.h>
#include <string>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#define MAX 50

using namespace std;

template <typename T>
int len(T args) { return args.size(); }

vector<string> split(string &s, const char &c)
{
    string buffer{""};

    vector<string> resultado;

    for (char caractere : s)
    {
        if (caractere != c)
        {
            buffer += caractere;
        }
        else
        {
            resultado.push_back(buffer);
            buffer = "";
        }
    }
    if (buffer != "")
    {
        resultado.push_back(buffer);
        buffer = "";
    }

    return resultado;
}

bool Comparar(string dig, string pal)
{
    if (dig.length() > pal.length())
    {
        return false;
    }
    int cont = 0;
    char temp[1][MAX] = {"pal"};

    //   vector<string> temp = split(pal, '');
    //   int length = len(temp);

    cout
        << "Tamanho vetor" << length << "\n";

    for (int x = 0; x < dig.length(); x++)
    {
        for (int i = 0; i < length; i++)
        {
            if (dig.at(x) == temp[i].at(0))
            {
                cout << "Digito: " << dig.at(x) << "\n";
                cout << "Temp: " << temp[i].at(0) << "\n";
                cont++;
                temp[i] = "#";
                break;
            }
        }
    }

    return cont ==
           dig.length();
}

int main(int argc, char **argv)
{
    string linha;

    while (true)
    {
        getline(cin, linha);

        if (linha == "0")
        {
            break;
        }

        vector<string> temp = split(linha, ' ');

        int n = 0;

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

        cout << n << "\n";
        return 0;
    }
}
