#!/usr/bin/env python3
#encoding: windows-1252

# To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.

import sys

def Comparar(dig, pal):
    if len(dig) > len(pal):
        return False
    
    cont = 0
    temp = []

    for x in range(len(pal)):
        temp.append(pal[x])
        
    for x in range(len(dig)):
        for i in range(len(temp)):
            if dig[x] == temp[i][0]:
                cont += 1
                temp[i] = "#"
                break
    return cont == len(dig)
    
if __name__ == "__main__":
    while True:
        linha = input()

        if linha in '0':
            break

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

        for x in range(1,len(temp)):
            if Comparar(temp[x], temp[0]):
                n += 1

        print(n)

sys.exit()